【问题标题】:gradle set absolute path value for a keystore filegradle 设置密钥库文件的绝对路径值
【发布时间】:2016-06-29 06:19:21
【问题描述】:

我想将我的密钥库保存在项目目录之外。我不想将文件路径存储在存储库中,所以我将这些值委托给~/.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。

【问题讨论】:

  • 你的意思是你不能让 gradle 接受 s 路径?当你这样做时会发生什么?
  • 很抱歉让您感到困惑 - 这可能是我的配置错误。我最终使用了 Tim Rose 的另一个有趣的解决方案。
  • 如果你使用storeFile file(RELEASE_STORE_FILE),我很确定它会使用绝对路径。请确保您没有在路径前后添加双引号或单引号。

标签: android gradle groovy


【解决方案1】:

我最终使用了来自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 "======================================================="
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我通过使用 符号链接

    解决了这个问题
    1. 在应用模块中创建符号链接keystore.lnk

      ln -s [path-to-keystore] keystore.lnk

    2. 然后在gradle.properties中使用keystore.lnk

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

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

    【讨论】:

      【解决方案3】:

      在那里找到解决方案:https://gist.github.com/gabrielemariotti/6856974

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

      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添加到你的发布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)。

      【讨论】:

        【解决方案4】:

        在我的情况下 (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/&lt;username&gt;)。

        我通过使用几个 ../ 指定密钥库的路径以指向 Users/&lt;username&gt; 文件夹中的正确位置来解决此问题。

        这是它在gradle.properties 文件中的外观:

        RELEASE_STORE_FILE=../../../../../local_store.keystore
        

        build.gradle 文件无需修改。

        【讨论】:

          猜你喜欢
          • 2019-04-11
          • 1970-01-01
          • 2018-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-04
          • 1970-01-01
          相关资源
          最近更新 更多