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

问题描述 投票:0回答:6

我想将我的密钥库保存在项目目录之外。我不想将文件路径存储在存储库中,因此我将这些值委托给 ~/.gradle/gradle.properties

 中适当的 gradle 变量
我无法让 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 } } }

~/.gradle/gradle.properties

 文件:

RELEASE_STORE_FILE=/Users/username/.gradle/keystores/project/release.key RELEASE_STORE_PASS=****** RELEASE_ALIAS=****** RELEASE_KEY_PASS=******

简而言之:我想传递一个绝对路径值给gradle。

android gradle groovy
6个回答
3
投票
我最终使用了来自

this网站的一个有趣的解决方案。

这个想法是将变量保存在一个单独的文件夹中,该文件夹

存储在远程存储库中

~/.gradle/gradle.properties

 文件中放入:

Keys.repo=/Users/username/.signing

其中

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

您需要将

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 "=======================================================" } } } }
    

3
投票
我通过使用

符号链接解决了这个问题

  1. 在应用程序模块中创建符号链接

    keystore.lnk

    
    
    

    ln -s [密钥库路径] keystore.lnk

  2. 然后在

    keystore.lnk

    中使用
    gradle.properties

    RELEASE_STORE_FILE=keystore.lnk(不要使用引号)

现在你的 gradle 指令就可以工作了。


3
投票
在那里找到解决方案:

https://gist.github.com/gabrielemariotti/6856974

简单地说,您应该正确解析包含密钥库路径的文件。用下一行修改你的模块的 gradle。首先,这是如何根据 keystore.properties 文件内容创建签名配置:

signingConfigs { release { def Properties props = new Properties() def propFile = new File('path/to/your/keystore.properties') //absolute path to keystore.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 { println 'keystore.properties found but some entries are missing' android.buildTypes.release.signingConfig = null } } else { println 'keystore.properties not found' android.buildTypes.release.signingConfig = null } } }

然后将signingConfig添加到您的release buildType中:

buildTypes { ... release { ... signingConfig signingConfigs.release } }

此解决方案的 keystore.properties 文件示例:

STORE_FILE=absolute//path//to//store STORE_PASSWORD=yourPass KEY_PASSWORD=keysPass KEY_ALIAS=aliasName

这对我有用(Android Studio 3.0.1、Gradle 4.1、Windows 10)。


1
投票
在我的情况下 (macOS),我的

local_store.keystore

 保存在 
Users/<username>
 中,密钥库的路径在 
gradle.properties
 文件中配置为:

RELEASE_STORE_FILE=Users/<username>/local_store.keystore
我收到以下错误:

> Keystore file '/Users/<username>/Documents/MyProject/android/app/Users/<username>/local_store.keystore' not found for signing config 'release'.
因此,

gradle.properties

中设置的路径被添加到
build.gradle
文件的相对路径中(注意重复的
/Users/<username>
)。

我通过使用几个

../

 指定密钥库的路径来指向 
Users/<username>
 文件夹中的正确位置来修复此问题。

这就是

gradle.properties

 文件中的样子:

RELEASE_STORE_FILE=../../../../../local_store.keystore
无需修改 

build.gradle

 文件。


0
投票
我在 Mac 上使用这个

signingConfigs { debug { storeFile file(System.env.HOME + '/.android/debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } }

    

0
投票
我最终实现了与 Mikhail Avdeev 的答案类似的东西,但为了为基于 Kotlin 的构建脚本提供一个示例,无论如何,这是我的解决方案:

id("com.sidneysimmons.gradle-plugin-external-properties") version "2.0.1" apply false

 添加到您的项目级 build.gradle.kts (
此处为插件源

对于您的模块级 build.gradle.kts,请使用:

import com.android.build.api.dsl.ApkSigningConfig plugins { // [... all your other plugins] id("com.sidneysimmons.gradle-plugin-external-properties") } externalProperties { propertiesFileResolver(file("signing.properties")) } android { signingConfigs { named("debug") { if (checkExternalSigningConfig()) { applyExternalSigningConfig() } else { // Use the default debug keystore if no configuration has been specified defaultConfig.signingConfig } } create("release") { if (checkExternalSigningConfig()) { applyExternalSigningConfig() // automatically add the signingConfig to your // build config if the configuration file is valid android.buildTypes.getByName("release").signingConfig = this } } } } fun ApkSigningConfig.checkExternalSigningConfig(): Boolean { return props.exists("$name.keyStore") && file(props.get("$name.keyStore")).exists() && props.exists("$name.storePassword") && props.exists("$name.keyAlias") && props.exists("$name.keyPassword") } fun ApkSigningConfig.applyExternalSigningConfig() { storeFile = file(props.get("$name.keyStore")) storePassword = props.get("$name.storePassword") keyAlias = props.get("$name.keyAlias") keyPassword = props.get("$name.keyPassword") }
在模块目录中放置一个名为 

signing.properties

 的文件(
app
 或其他目录),您可以在其中愉快地使用绝对路径(本例中为 Windows 示例):

debug.keyStore=\\C:\\Path\\to\\keystore\\file debug.storePassword=your_password debug.keyAlias=your_keyAlias debug.keyPassword=your_password release.keyStore=\\C:\\Path\\to\\keystore\\file release.storePassword=your_password release.keyAlias=your_keyAlias release.keyPassword=your_password
    
© www.soinside.com 2019 - 2024. All rights reserved.