gradle设置密钥库文件的绝对路径值

kos*_*cki 8 groovy android gradle

我想将我的密钥库保存在项目目录之外.我不想里面的仓库,所以我委派值适当gradle这个变量中存储的文件路径~/.gradle/gradle.properties ,我不能让gradle这个接受像一个绝对路径: /Users/username/.gradle/keystores/project/release.key~/.gradle/keystores/project/release.key

我想: storeFile file(RELEASE_STORE_FILE)storeFile new File(RELEASE_STORE_FILE)

然而,它们似乎都不起作用.

如何通过RELEASE_STORE_FILE变量将绝对路径值传递给密钥库文件?

android {
    signingConfigs {
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASS
            keyAlias RELEASE_ALIAS
            keyPassword RELEASE_KEY_PASS
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

~/.gradle/gradle.properties文件:

RELEASE_STORE_FILE=/Users/username/.gradle/keystores/project/release.key
RELEASE_STORE_PASS=******
RELEASE_ALIAS=******
RELEASE_KEY_PASS=******
Run Code Online (Sandbox Code Playgroud)

简而言之:我想将绝对路径值传递给gradle.

kos*_*cki 3

我最终使用了这个网站上的一个有趣的解决方案。

这个想法是将变量保存在存储在远程存储库上的单独文件夹中。

~/.gradle/gradle.properties文件中您输入:

Keys.repo=/Users/username/.signing
Run Code Online (Sandbox Code Playgroud)

其中Keys.repo是远程存储库的本地路径。

稍后/Users/username/.signing/YourProjectName.properties你有:

RELEASE_STORE_FILE=/YourProjectName/release.keystore //in fact it's a relative path
RELEASE_STORE_PASS=xxxxx
RELEASE_ALIAS=xxxxx
RELEASE_KEY_PASS=xxxxx
Run Code Online (Sandbox Code Playgroud)

您需要将release.keystore文件存储在/Users/username/.signing/YourProjectName/release.keystore路径中

该配置的使用方式如下:

android {
    signingConfigs {
        debug { /* no changes - usual config style */ }
        release {
            if (project.hasProperty("Keys.repo")) {
                def projectPropsFile = file(project.property("Keys.repo") + "/YourProjectName.properties")
                if (projectPropsFile.exists()) {
                    Properties props = new Properties()
                    props.load(new FileInputStream(projectPropsFile))

                    storeFile file(file(project.property("Keys.repo") + props['RELEASE_STORE_FILE']))
                    storePassword props['RELEASE_STORE_PASS']
                    keyAlias props['RELEASE_ALIAS']
                    keyPassword props['RELEASE_KEY_PASS']
                }
            } else {
                println "======================================================="
                println "[ERROR] - Please configure release-compilation environment - e.g. in ~/.signing  directory"
                println "======================================================="
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)