【问题标题】:Android Gradle - load signing config from external fileAndroid Gradle - 从外部文件加载签名配置
【发布时间】:2014-08-07 11:02:34
【问题描述】:

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

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 服务器。

有没有办法从我硬盘上某处的外部文件加载signingConfig?

【问题讨论】:

    标签: android android-studio gradle android-gradle-plugin build.gradle


    【解决方案1】:

    我用这样的东西。

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

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

    此文件不受版本控制。 当然你可以改变文件夹。

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

     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
        }
    

    如果更改文件夹,则必须更改此行:

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

    【讨论】:

    • 我正在尝试相同的方法,但不是硬编码我从local.properties获取路径的路径,它可以读取文件但在properties.load(new FileInputStream(propFile))行中显示Error:(33, 0) Malformed \uxxxx encoding.我什至很难编码路径并尝试但没有用。有什么帮助吗?
    【解决方案2】:

    您可以将您的敏感详细信息移动到单独的签名文件中,并让 gradle 加载这些信息。

    Here's a good article on how to do it

    【讨论】:

      【解决方案3】:

      对于 Kotlin 脚本 (build.gradle.kts)

      将文件 signing.properties 放在找到模块特定 build.gradle.kts 的位置。不要忘记将它添加到您的 .gitignore 文件中!

      signing.properties

      storeFilePath=/home/willi/example.keystore
      storePassword=secret
      keyPassword=secret
      keyAlias=myReleaseSigningKey
      

      build.gradle.kts

      android {
          // ...
          signingConfigs {
              create("release") {
                  val properties = Properties().apply {
                      load(File("signing.properties").reader())
                  }
                  storeFile = File(properties.getProperty("storeFilePath"))
                  storePassword = properties.getProperty("storePassword")
                  keyPassword = properties.getProperty("keyPassword")
                  keyAlias = "release"
              }
          }
      
          buildTypes {
              getByName("release") {
                  signingConfig = signingConfigs.getByName("release")
                  // ...
              }
          }
      }
      

      【讨论】:

      • 您使用的是硬编码的 keyAlias,而不是属性文件中的那个
      【解决方案4】:

      对于 Groovy (build.gradle)

      将文件 signing.properties 放在找到特定模块的 build.gradle 的位置。不要忘记将它添加到您的 .gitignore 文件中!

      signing.properties

      storeFilePath=/home/willi/example.keystore
      storePassword=secret
      keyPassword=secret
      keyAlias=myReleaseSigningKey
      

      build.gradle

      android {
          // ...
          signingConfigs{
              release {
                  def props = new Properties()
      
                  def fileInputStream = new FileInputStream(file('../signing.properties'))
                  props.load(fileInputStream)
                  fileInputStream.close()
      
                  storeFile = file(props['storeFilePath'])
                  storePassword = props['storePassword']
                  keyAlias = props['keyAlias']
                  keyPassword = props['keyPassword']
              }
          }
      
          buildTypes {
              release {
                  signingConfig signingConfigs.release
                  // ...
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多