1

I have been working on adding flavors to project and ran into below problem. Instead of adding the keystore path, alias and password, I added them in a .properties file and tried accessing them in build.gradle.

So this is how I did,

The project is in git, so other users also get my local path to .properties file path if I keep it in gradle.properties. For that reason I added the .properties path in local.properties. In build.gradle I'm reading the variable from local.properties and getting the each variables written in properties file.

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

Here, in the line properties.load(new FileInputStream(propFile)) it always throws

Malformed \uxxxx encoding.

I have tried,

  1. Hard coding the path of pro.properties instead of taking from local.properties

  2. Tried as given in the following link

  3. Also tried with changing the forward-slash, backslash and escape-char combinations

But nothing helped so far. Any help will be appreciated

In pro.properties file

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
Community
  • 1
  • 1
Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36

1 Answers1

0

I cannot comment so I have to answer, but i just want to ask you if you have the same typo in your real file as you got in your question?

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

I mean that backslash between C and :

Christian Quast
  • 85
  • 2
  • 12
  • Yes, it's an escape character. It has to be there else it shows error – Kavin Prabhu Aug 09 '16 at 12:45
  • I think a good way to find the bug would be to print some output if you haven't already. so after `def localPropertiesPath = props.getProperty('variable.prop', null);` you should include a line like `println localPropertiesPath` and after `def propFile = file(localPropertiesPath)` something like `println propFile` – Christian Quast Aug 09 '16 at 13:42