从外部文件加载签名配置

从外部文件加载签名配置

本文介绍了Android Gradle - 从外部文件加载签名配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 的 Gradle 中,为发布版本定义签名配置似乎是一种常见的做法:

In Gradle for Android it seems to be commons practice to define your signing config for release build like this:

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 文件保留在版本控制中,并且对于我的密钥库的密码(这与我用于其他东西的密码相同,愚蠢,我知道)感觉不太好git 服务器.

Thing is, I want to keep my build.gradle file in version control and don't have a good feeling having the password for my keystore (which is the same I use for other stuff, stupid, I know) on some git server.

有没有办法从我硬盘上某处的外部文件加载签名配置?

Is there a way to load the signingConfig from an external file from somewhere on my hard drive?

推荐答案

我使用的是这样的.

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

I have a signing.properties in my app root folder.

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

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

This file is not on under version control.Of course you can change folder.

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

Then in your build.gradle you can use something like this:

 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
    }

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

If you change the folder, you have to change this line:

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

这篇关于Android Gradle - 从外部文件加载签名配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:25