【问题标题】:Gradle, loading external file for signing configGradle,加载外部文件以签署配置
【发布时间】:2016-08-09 10:07:47
【问题描述】:

我一直在努力为项目添加风味,但遇到了以下问题。我没有添加keystore 路径、别名和密码,而是将它们添加到.properties 文件中,并尝试在build.gradle 中访问它们。

所以我就是这样做的,

该项目在 git 中,因此如果我将其保存在 gradle.properties 中,其他用户也会获得我的本地路径到 .properties 文件路径。出于这个原因,我在local.properties 中添加了.properties 路径。在build.gradle 中,我从local.properties 读取变量并将每个变量写入属性文件。

local.properties

variable.prop=C\:\\Users\\user\\signing\\project\\pro.properties

build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

def versionMajor = 1
def versionMinor = 0
def versionPatch = 1
def versionBuild = 1

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

   Properties props = new Properties()
   props.load(project.rootProject.file('local.properties').newDataInputStream())
   def localPropertiesPath = props.getProperty('variable.prop', null);
   if (localPropertiesPath == null)
        throw new GradleException("Unable to find properties file. Variable might not be added in local.properties or file may not exist in given path")
   else {
        def propFile = file(localPropertiesPath)
        if (propFile.canRead()) {
            Properties properties = new Properties()
            properties.load(new FileInputStream(propFile))

            signingConfigs {

                uat {
                    storeFile file(properties['uatKeystorePath'])
                    keyAlias properties['uatKeyAlias']
                    keyPassword properties['uatKeyPassword']
                    storePassword properties['uatStorePassword']
                }

                release {
                    storeFile file(properties['releaseKeystorePath'])
                    keyAlias properties['releaseKeyAlias']
                    keyPassword properties['releaseKeyPassword']
                    storePassword properties['releaseStorePassword']
                }
            }

            defaultConfig {
                applicationId "com.package.name"
                minSdkVersion 15
                targetSdkVersion 23
                versionCode versionMajor * 1000000 + versionMinor * 10000 + versionPatch * 100 + versionBuild
                versionName "${versionMajor}.${versionMinor}.${versionPatch}"
                setMultiDexEnabled true
            }

            dexOptions {
                javaMaxHeapSize "2048M"
            }

            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                    signingConfig signingConfig.release
                }
            }
        }
    } 
}

在这里,在properties.load(new FileInputStream(propFile)) 行中它总是抛出

Malformed \uxxxx encoding.

我试过了,

  1. 硬编码pro.properties的路径,而不是从local.properties获取

  2. 尝试如下link

  3. 还尝试更改 forward-slashbackslashescape-char 组合

但到目前为止没有任何帮助。任何帮助将不胜感激

pro.properties 文件中

uatKeystorePath=C:\Users\user\.signing\project\uat_keystore.jks
uatStorePassword=StorePassword
uatKeyAlias=AliasName
uatKeyPassword=KeyPassword
releaseKeystorePath=C:\Users\user\.signing\project\release_keystore.jks
releaseStorePassword=StorePassword
releaseKeyAlias=AliasName
releaseKeyPassword=KeyPassword

【问题讨论】:

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


    【解决方案1】:

    我无法发表评论,所以我必须回答,但我只想问您,您的真实文件中是否有与您的问题相同的错字?

    variable.prop=C\:\\Users\\user\\signing\\project\\pro.properties

    我的意思是C:之间的反斜杠

    【讨论】:

    • 是的,它是一个转义字符。它必须在那里,否则它会显示错误
    • 我认为找到错误的一个好方法是打印一些输出,如果你还没有的话。所以在def localPropertiesPath = props.getProperty('variable.prop', null); 之后,你应该包括像println localPropertiesPath 这样的行,在def propFile = file(localPropertiesPath) 之后像println propFile 这样的东西
    猜你喜欢
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2020-07-22
    • 2018-12-30
    相关资源
    最近更新 更多