Sign APK without putting keystore info in build.gradle

后端 未结 12 981
栀梦
栀梦 2020-11-28 17:51

I am trying to setup signing process so that keystore password and key password are not stored in the project\'s build.gradle file.

Cur

12条回答
  •  天涯浪人
    2020-11-28 18:11

    The nice thing about Groovy is that you can freely mix Java code, and it's pretty easy to read in a key/value file using java.util.Properties. Perhaps there's an even easier way using idiomatic Groovy, but Java is still pretty simple.

    Create a keystore.properties file (in this example, in the root directory of your project next to settings.gradle, though you can put it wherever you like:

    storePassword=...
    keyPassword=...
    keyAlias=...
    storeFile=...
    

    Add this to your build.gradle:

    allprojects {
        afterEvaluate { project ->
            def propsFile = rootProject.file('keystore.properties')
            def configName = 'release'
    
            if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
                def props = new Properties()
                props.load(new FileInputStream(propsFile))
                android.signingConfigs[configName].storeFile = file(props['storeFile'])
                android.signingConfigs[configName].storePassword = props['storePassword']
                android.signingConfigs[configName].keyAlias = props['keyAlias']
                android.signingConfigs[configName].keyPassword = props['keyPassword']
            }
        }
    }
    

提交回复
热议问题