eJoe
eJoe

Reputation: 485

Android reading from local.properties

I have local.properties file in root of my project.

sdk.dir=C\:\\Users\\vucet\\AppData\\Local\\Android\\Sdk
versionName='7.0'

and in build gradle in app module I am trying to get versionName on this way

Properties properties = new Properties()
if (project.rootProject.file('local.properties').exists()) {
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
}

def getVerCode = { ->
    def code = project.hasProperty("versionCode") ? project.versionCode.toInteger() : 1000
    return code
}

def getVerName = { ->
    def name = project.hasProperty("versionName") ? project.versionName.toString() : "5.0"
    return name
}

but every time I get 5.0 value. Please can anyone help me ?

Upvotes: 11

Views: 14642

Answers (5)

Bharat Lalwani
Bharat Lalwani

Reputation: 1530

It's better to create new data.properties file, which will all your keys, tokens, Host Url, secrets, etc. Make sure to create in under app folder, which means src folder & this file would be in the same path or hierarchy.

===>data.properties file<===

GMAP_KEY="AIzaSyDthaO7P************************SSSD"
HOST_LIVE="https://google.co.in/"
HOST_STAGING="http://amazon.com/"
BASE_IMAGE_PATH="https://amazonaws.com/s3/bucket25"
COMPANY_ID="57c3db87dacc********"

===>app level build.gradle file<===


def getSecretKeys(){
    def keyFile = file("data.properties")
    def secretKeys = new Properties()
    secretKeys.load(new FileInputStream(keyFile))
    return secretKeys
}


release {
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    buildConfigField 'String', 'BASE_PATH', getSecretKeys()['HOST_LIVE']     
    buildConfigField 'String', 'BASE_PATH_IMAGE', getSecretKeys()['BASE_IMAGE_PATH']
    buildConfigField 'String', 'MAP_KEY', getSecretKeys()['MAP_KEY']
    buildConfigField 'String', 'COMPANY_ID', getSecretKeys()['COMPANY_ID']
}
debug {
    ext.enableCrashlytics = false
    buildConfigField 'String', 'BASE_PATH', getSecretKeys()['HOST_STAGING']
    buildConfigField 'String', 'BASE_PATH_IMAGE', getSecretKeys()['BASE_IMAGE_PATH']
    buildConfigField 'String', 'MAP_KEY', getSecretKeys()['MAP_KEY']
    buildConfigField 'String', 'COMPANY_ID', getSecretKeys()['COMPANY_ID']
}

==>Android java code<==

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    Log.d("TAG", "My Map key is: " + BuildConfig.MAP_KEY);
    Log.d("TAG", "My URL Base Path is: " + BuildConfig.BASE_PATH);

}

Upvotes: 4

Lucas Orso
Lucas Orso

Reputation: 106

I could only read by doing this

Properties props = new Properties()
props.load(new FileInputStream(new File('local.properties')))

then

props['KEY']

Upvotes: 3

giltsl
giltsl

Reputation: 1451

Reading local.properties using gradle and use them in your Android app java code.

local.properties:

TOKEN="yyyyy" SERVER_URL="https://foo.com"

build.gradle:

Properties properties = new Properties()
if (project.rootProject.file('local.properties').canRead()) {
        properties.load(project.rootProject.file("local.properties").newDataInputStream())
}

buildConfigField 'String', 'TOKEN', properties.getProperty('TOKEN', '"xxx"')
buildConfigField 'String', 'SERVER_URL', properties.getProperty('SERVER_URL', '"https://example.com/"')

Android java code

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    Log.d("TAG", "My 'token' is: " + BuildConfig.TOKEN);
    Log.d("TAG", "My 'server url' is: " + BuildConfig.SERVER_URL);

}

Upvotes: 16

eJoe
eJoe

Reputation: 485

Hi I find way how to read local.properties file in gradle script

def Properties properties = new Properties()

if (project.rootProject.file('local.properties').canRead()) {
    properties.load(new FileInputStream(project.rootProject.file('local.properties')))
    def versionBuild = properties['versionName'].toString()

} else {
    throw new GradleException("Could not read version.properties!")
}

def getVerCode = { ->
    def code = properties.getProperty("versionCode", "1").toInteger()
    return code
}

def getVerName = { ->
    def name = properties.getProperty("versionName", "1")
    return name
}

Upvotes: 3

Rafael
Rafael

Reputation: 1634

Try printing versionName to check if you aren't doing anything wrong until this point :

def getVerName = { ->
    println project.versionName.toString()
    ...
}

Or if you can, try using gradle.properties file instead as you can access properties straght from build.gradle :

gradle.properties:

versionName='7.0'

build.gradle:

task printProps << {
    println versionName
    ...
}

Upvotes: 0

Related Questions