签署 APK 而不将密钥库信息放入 build.gradle [英] Sign APK without putting keystore info in build.gradle

查看:16
本文介绍了签署 APK 而不将密钥库信息放入 build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置签名过程,以便密钥库密码和密钥密码不会存储在项目的 build.gradle 文件中.

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

目前我在 build.gradle 中有以下内容:

Currently I have the following in the build.gradle:

android {
    ...
    signingConfigs {
        release {
            storeFile file("my.keystore")
            storePassword "store_password"
            keyAlias "my_key_alias"
            keyPassword "key_password"
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release            
        }
    }
}

它工作得很好,但我不能storePasswordkeyPassword 的值放在我的存储库中.我也不想把 storeFilekeyAlias 放在那里.

It works perfectly fine but I must not put the values for the storePassword, and keyPassword in my repository. I would prefer to not put storeFile and keyAlias there either.

有没有办法改变 build.gradle 以便它从某些外部来源(例如仅驻留在我的计算机上的文件)获取密码?

Is there a way to alter the build.gradle so that it will obtain passwords from some external source (like a file that resides on my computer only)?

当然,更改后的 build.gradle 应该可以在任何其他计算机上使用(即使该计算机无法访问密码).

And of course, the altered build.gradle should be usable on any other computer (even if the computer doesn't have access to passwords).

我正在使用 Android Studio 和 Mac OS X Maverics,如果有关系的话.

I am using Android Studio and in Mac OS X Maverics if it does matter.

推荐答案

Groovy 的好处在于您可以自由混合 Java 代码,并且使用 java.util 在键/值文件中读取非常容易.属性.也许使用惯用的 Groovy 有更简单的方法,但 Java 仍然非常简单.

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.

创建一个 keystore.properties 文件(在这个例子中,在你项目的根目录中 settings.gradle 旁边,尽管你可以把它放在你喜欢的任何地方:

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=...

将此添加到您的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']
        }
    }
}

这篇关于签署 APK 而不将密钥库信息放入 build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆