如何在Gradle中访问ext变量 [英] How to access ext variables in Gradle
问题描述
我试图获取在第3方库(结构)中定义的变量,以根据是否启用了Crashlytics做一个条件。
I am trying to get the variable defined in a 3rd party library (fabric) to do a condition based on whether Crashlytics is enabled or not.
ext.enableCrashlytics = true
http://support.crashlytics.com/knowledgebase/articles/202938-gradle
可以在以下位置配置变量buildTypes或风味,但在build.gradle中找不到其他方法。
http://support.crashlytics.com/knowledgebase/articles/202938-gradle The variable can be configured in buildTypes or in flavors but I can't find a way to access it elsewhere in my build.gradle
我尝试了几件事没有运气。
I tried several things without any luck.
allprojects.getProperties().get("enableCrashlytics")
project.enableCrashlytics
project.ext.enableCrashlytics
allProjects.ext.enableCrashlytics
有人尝试过吗?我正在尝试执行的上下文是根据是否启用来写入fabric.properties文件。
Anyone tried that before? The context I'm trying to do this is to write the fabric.properties file based on if it is enabled or not.
android.applicationVariants.all { variant ->
...
//create fabric.properties
...
}
推荐答案
您可以在顶级 build.gradle
:
ext {
myproperty = 12
}
或一个数组:
ext {
myarray = [
name0 : "xx",
name1 : "xx"
]
}
然后在每个模块中都可以使用somenthig,例如:
Then in each module you can use somenthig like:
rootProject.ext.myproperty
rootProject.ext.myarray.name0
这篇关于如何在Gradle中访问ext变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!