Android gradle signConfigs来自本地文件,不允许从IDE运行配置

时间:2017-09-15 08:43:34

标签: android android-studio android-gradle

所以我想为Android中的任何发布版本创建一个签名配置。但是我希望能够通过点击运行按钮而不是去构建来运行它 - >生成签名的Apk。

我希望将密钥库的凭据和路径从应用程序中分离出来。所以我在我的项目应用程序文件夹中创建了一个属性。

让我们来看看到目前为止我做了什么:

gradle.properties文件如下所示:

myapp.properties=.signing/myapp_signing.properties

属性文件的内容如下所示:

keystore=/Users/Fred/Documents/mykey_ks.jks
keystore.alias_pw=abcd1234

然后我有以下内容实际从proerties文件中获取signConfig闭包中的信息:

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'
repositories {
    maven { url 'https://maven.fabric.io/public' }
}


if (project.hasProperty("myapp.properties")
        && new File(project.property("myapp.properties")).exists()) {

    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("myapp.properties"))))
    android {
        signingConfigs {
            release {
                storeFile file(props['keystore'])
                storePassword props['keystore.password']
            }
        }
    }
}

android {
    compileSdkVersion 26
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 26
//....... more android stuff below, should be ok to have two android closures right ?


}

现在,当我在选择ProdRelease后按下android studio上的运行按钮时,我收到一条警告,说我的apk没有签名且没有签名配置可用?为什么?

enter image description here

更新:我在build.gradle应用级文件中尝试了以下内容:

 buildTypes {
        release {

            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            releaseBuild()
        }
    }
//....
def releaseBuild(){
    if (project.hasProperty("pomelo.properties")
            && new File(project.property("pomelo.properties")).exists()) {

        println 'file does exist'
        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("myapp.properties"))))
        storeFile file(props['keystore'])
        storePassword props['keystore.password']
        keyAlias props['keystore.alias']
        keyPassword props['keystore.alias_password']
    }
}

1 个答案:

答案 0 :(得分:0)

可能应该像这样定义

signingConfigs {
    release {
      releaseBuild()
      .....
    }
}


def releaseBuild(){
if (project.hasProperty("myapp.properties")
        && new File(project.property("myapp.properties")).exists()) {

    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("myapp.properties"))))
    storeFile file(props['keystore'])
    storePassword props['keystore.password']
    }
}