Load property from external file into build.gradle

android-gradle-plugin, build.gradle, gradle, groovy

Solution

Just like `ndk.dir`, `"$ndk.dir"` first gets the `ndk` property, and then the `dir` property, which is not what you want. (This is also evident in the error message, which says "Could not find property 'ndk'".) Instead, this should work:

def ndkBuild = new File(project.property('ndk.dir'), 'ndk-build')

A safer solution is to store the whole `Properties` object as a single extra property:

...
ext.externalProps = config

Then you can access external properties like so:

def ndkBuild = new File(externalProps['ndk.dir'], 'ndk-build')

Problem

I have this: ``` def loadProperties(String sourceFileName) { def config = new Properties() def propFile = new File(sourceFileName) if (propFile.canRead()) { config.load(new FileInputStream(propFile)) for (Map.Entry property in config) { ext[property.key] = property.value; } } } loadProperties 'gradle.properties' ``` How do I reference the property (ndk.dir) in build.gradle? ``` def ndkBuild = new File("$ndk.dir", 'ndk-build') ``` And is there a better way of reading ndk.dir from file gradle.properties? And how do I use it in? ``` def ndkBuild = new File("$ndk.dir", 'ndk-build') ``` Full code: ``` task buildNative(type: Exec) { loadProperties 'gradle.properties' if (System.getenv('NDK_HOME') != null || "$ndk.dir" != null) { if ("$ndk.dir" != null) { def ndkBuild = new File("$ndk.dir", 'ndk-build') } else { def ndkBuild = new File(System.getenv('NDK_HOME'), 'ndk-build') } workingDir "jni" executable ndkBuild } else { throw new GradleException('Reason: NDK_HOME not set or ndk.dir is missing in gradle.properties...') } } def loadProperties(String sourceFileName) { def config = new Properties() def propFile = new File(sourceFileName) if (propFile.canRead()) { config.load(new FileInputStream(propFile)) for (Map.Entry property in config) { ext[property.key] = property.value; } } } ```

Original source