【问题标题】:How to pull key-value pairs from a txt file using gradle如何使用 gradle 从 txt 文件中提取键值对
【发布时间】:2014-12-18 00:47:54
【问题描述】:

假设我在一个 txt 文件中有一堆键值对,我想使用 gradle 将它们拉入我可以使用的变量中。我该怎么做呢?

我要做的不是在 build.gradle 文件中硬编码我的 storepass 和其他相关签名信息,并将它们排除在托管源代码控制之外。

我想要做的是类似的事情

propertiesFile file(/buildProperties.txt)

storeFile file(propertiesFile.getProperty("myStoreFile"))
storePassword propertiesFile.getProperty("myStorePassword")
keyAlias propertiesFile.getProperty("myKeyAlias")
keyPassword propertiesFile.getProperty("myKeyPassword")

我知道这很简单并且可以完成,但是谷歌搜索了一个小时还没有找到解决方案。

【问题讨论】:

    标签: groovy gradle android-studio android-gradle-plugin


    【解决方案1】:

    Gradle 为这个out of the box 提供了一种机制。只需将这些属性放在 USER_HOME/.gradle/gradle.properties 中,它们就会隐含在您的构建脚本中。

    【讨论】:

    • 我决定坚持我使用的方式,但如果我知道的话,这会奏效。
    【解决方案2】:

    所以我终于想通了。

    def Properties props = new Properties()
    def propFile = new File('path\to\file.txt')
    
     release {
    
            if (propFile.canRead()) {
                props.load(new FileInputStream(propFile))
    
                storeFile file(props['key.store'])
                storePassword props['key.store.password']
                keyAlias props['key.alias']
                keyPassword props['key.alias.password']
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多