我有一个基本问题。在我的项目中,我在根build.gradle中定义了一个最终的可变变量,就像下面的一样
final int PUBLISH_TYPE_DEV = 1
allprojects {
repositories {
...
}
project.ext {
versionCode = 63055
...
}
}
然后,我需要参考应用程序build.gradle文件中的PUBLISH_TYPE_DEV。我直接尝试了";类型_ DEV";,或项目。PUBLISH_TYPE_DEV或项目["PUBLISH-TYPE_DEV["],它们都不适合我;无法获取未知属性"PUBLISH_TYPE_GREY"消息
如何在子模块的build.gradle中使用这个最终变量?
另一个解决方案是有一个自定义属性文件,您将在其中存储参数
参数.属性
# a beautiful comment to explain
PUBLISH_TYPE_DEV=1
然后,在你的建筑中.gradle
// Load Configuration
try {
Properties props = new Properties()
props.load(new FileInputStream("parameters.properties"))
props.each { prop -> project.ext.setProperty(prop.key, prop.value) }
} catch (exception) {
throw new InvalidUserDataException("Parameters not found : $exception.message")
}
if (project.hasProperty("PUBLISH_TYPE_DEV")) {
// do what you want
} else {
throw new InvalidUserDataException('Parameter not found : PUBLISH_TYPE_DEV')
}
在项目build.gradle中,您可以编写以下内容:
buildscript {
ext {
PUBLISH_TYPE_DEV = '1'
}
}
然后,在你的模块build.gradle中,你可以用$PUBLISH_TYPE_DEV
来引用它
使用此代码
示例:
ext {
compileSdkVersion = 29
buildToolsVersion = "29.0.2"
targetSdkVersion = 29
}
调用:
buildToolsVersion rootProject.ext.buildToolsVersion
you can make it like this
android{
defaultConfig {
buildConfigField "String", "BaseUrl",""http:\example.com""
}
}
你可以这样使用它:
BuildConfig.BaseUrl