Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read properties defined in local.properties in build.gradle

I have set sdk.dir and ndk.dir in local.properties.

How do I read the values of sdk.dir and ndk.dir in the build.gradle file?

like image 277
Vikram Avatar asked Feb 24 '14 21:02

Vikram


People also ask

What is local properties in Android?

The local. properties file goes in the project's root level, in the same folder as the gradlew , gradlew. bat , settings. gradle and other files.


2 Answers

You can do that in this way:

Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def sdkDir = properties.getProperty('sdk.dir') def ndkDir = properties.getProperty('ndk.dir') 

Use project.rootProject if you are reading the properties file in a sub-project build.gradle:

. ├── app │   ├── build.gradle <-- You are reading the local.properties in this gradle build file │   └── src ├── build.gradle ├── gradle ├── gradlew ├── gradlew.bat ├── settings.gradle └── local.properties 

In case the properties file is in the same sub-project directory you can use just project.

like image 125
rciovati Avatar answered Oct 10 '22 05:10

rciovati


local.properties

default.account.iccid=123 

build.gradle -

def Properties properties = new Properties() properties.load(project.rootProject.file("local.properties").newDataInputStream())  defaultConfig {      resValue "string", "default_account_iccid", properties.getProperty("default.account.iccid", "") } 

and in code you get it as other string from Resources -

resources.getString(R.string.default_account_iccid); 
like image 45
Dmitrijs Avatar answered Oct 10 '22 06:10

Dmitrijs