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

标签 android android-studio gradle android-gradle-plugin build.gradle

在 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
        }
    }
}

问题是,我想将我的 build.gradle 文件保留在版本控制中,并且在某些设备上我的 keystore 密码(这与我用于其他东西的密码相同,愚蠢,我知道)感觉不太好混帐服务器。

有没有办法从我硬盘上某处的外部文件加载 signingConfig?

最佳答案

我用的是这样的。

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

STORE_FILE=xxxx
STORE_PASSWORD=xxx
KEY_ALIAS=xxx
KEY_PASSWORD=xxx

此文件不受版本控制。 当然,您可以更改文件夹。

然后在你的 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
    }

如果你改变文件夹,你必须改变这一行:

 def propFile = file('../signing.properties')

关于Android Gradle - 从外部文件加载签名配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263715/

相关文章:

java - 我的 Android 应用程序中的 Google Analytics v4 Cast 错误

android - 使用 OpenCV 在 Android 上使用神经网络进行灰度图像分类

ant - 如何使用Gradle脚本将Eclipse RAP应用程序导出到WAR中

gradle - mavenDeployer-工件如何在版本后添加文件名

java - 已签名的APK中出现Android Gradle警告问题。请首先更正以上警告

java - android:动态创建布局并动态设置ContentView

java - 更新 android 测验中单选按钮的数量及其标签

android - Gradle Android Studio中的错误

java - 使用 Scanner 类时未发现行错误

android - 将 SVN 迁移到 Android Studio