Android Gradle - 从外部文件加载签名配置

asc*_*sco 13 android gradle android-studio build.gradle android-gradle-plugin

在Gradle for Android中,似乎是公共实践来定义发布版本的签名配置,如下所示:

android {
    signingConfigs {
        debug {
            storeFile file("debug.keystore")
        }

        myConfig {
            storeFile file("other.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }

    buildTypes {
        foo {
            debuggable true
            jniDebugBuild true
            signingConfig signingConfigs.myConfig
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

事实上,我想将我的build.gradle文件保留在版本控制中,并且在一些git服务器上没有很好的感觉我的密钥库的密码(我用于其他东西,愚蠢,我知道).

有没有办法从我的硬​​盘驱动器上的某个地方加载来自外部文件的signingConfig?

Gab*_*tti 28

我用的是这样的东西.

signing.properties我的应用程序根文件夹中有一个.

STORE_FILE=xxxx
STORE_PASSWORD=xxx
KEY_ALIAS=xxx
KEY_PASSWORD=xxx
Run Code Online (Sandbox Code Playgroud)

此文件未在版本控制下启用. 当然你可以改变文件夹.

然后在你的build.gradle你可以使用这样的东西:

 android {

        signingConfigs {
            release
        }

        buildTypes {
                release {
                    signingConfig signingConfigs.release
                }     
        }
    }

    def Properties props = new Properties()
    def propFile = file('../signing.properties')
    if (propFile.canRead()){
        props.load(new FileInputStream(propFile))

        if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
                props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {

            android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
            android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
        } else {
            android.buildTypes.release.signingConfig = null
        }
    }else {
        android.buildTypes.release.signingConfig = null
    }
Run Code Online (Sandbox Code Playgroud)

如果更改文件夹,则必须更改此行:

 def propFile = file('../signing.properties')
Run Code Online (Sandbox Code Playgroud)