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

android, android-gradle-plugin, build.gradle, gradle, properties-file

Solution

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`.

Problem

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?

Original source

Related problems