I have set sdk.dir
and ndk.dir
in local.properties
.
How do I read the values defined in sdk.dir
and ndk.dir
in the build.gradle
file?
I have set sdk.dir
and ndk.dir
in local.properties
.
How do I read the values defined in sdk.dir
and ndk.dir
in the build.gradle
file?
The answer that loads local.properties manually above obviously works, and the next one that requires you to know which plugin was applied should work as well.
These approaches might be a little better for some since they are more generic because they work regardless of whether you're using the Application, Test, or Library plugin. These snippets also give you full programmatic access to all of the Android plugin config (Product Flavors, Build Tools version, and much more):
If you need access in a build.gradle file that is using the Android Gradle Plugin simply access the Android DSL directly as it's now available directly:
The longer form (below) of this is handy if you're creating custom Gradle Tasks classes or Plugins or simply want to view which properties are available.
At the time of this posting there is also a handy
adbExe
property that is definitely worth noting.This code has to execute AFTER the Android Gradle Plugin is configured per the Gradle livecycle. Typically this means you put it in the
execute
method of aTask
or place it AFTER theandroid
DSL declaration in an Android app/libraries'build.gradle
file).These snippets also come with the caveat that as you upgrade Android Gradle Plugin versions these properties can change as the plugin is developed so simply test when moving between versions of the Gradle and Android Gradle plugin as well as Android Studio (sometimes a new version of Android Studio requires a new version of the Android Gradle Plugin).
I think it's more elegant way.
it works on android gradle 1.5.0 .
You can do that in this way:
Use
project.rootProject
if you are reading the properties file in a sub-projectbuild.gradle
:In case the properties file is in the same sub-project directory you can use just
project
.local.properties
build.gradle -
and in code you get it as other string from Resources -
Although @rciovati's answer is certainly correct, there is also an alternative way of reading the values for
sdk.dir
andndk.dir
.As pointed out in this blog entry by Gaku Ueda (Getting ndk directory) the
BasePlugin
class offers methods forgetNdkFolder()
andgetSdkFolder()
:Note: You may have to change
com.android.application
tocom.android.library
if you are building a libraryThis is maybe a more elegant way of reading the folder values. Although it has to be said that the answer provided by @rciovati is more flexible, as one could read any value in the properties file.
You might reconsider naming the file something other than
local.properties
as that is already in use by Android Studio (for the root project), and