如何阅读在build.gradle local.properties定义的属性
问题描述:
我已经设置sdk.dir和ndk.dir在local.properties。
I have set sdk.dir and ndk.dir in local.properties.
我要如何阅读sdk.dir和ndk.dir在build.gradle文件中定义的值?
How do i read the values defined in sdk.dir and ndk.dir in the build.gradle file?
答
您可以做,在这种方式:
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')
使用 project.rootProject
如果你正在阅读的一个子项目 build.gradle
属性文件:
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
.