Is it possible to declare a variable in Gradle usable in Java?(是否可以在 Gradle 中声明一个可在 Java 中使用的变量?)
问题描述
是否可以在 Gradle 中声明一个可在 Java 中使用的变量?基本上我想在 build.gradle 中声明一些变量,然后在构建时(显然)得到它.就像 C/C++ 中的预处理器宏...
Is it possible to declare a variable in Gradle usable in Java ? Basically I would like to declare some vars in the build.gradle and then getting it (obviously) at build time. Just like a pre-processor macros in C/C++...
声明的一个例子是这样的......:
An example of declaration would be something like that ... :
android {
debug {
A_VAR_RETRIEVABLE_IN_JAVA = 42
}
release {
A_VAR_RETRIEVABLE_IN_JAVA = 42+52
}
}
有没有办法做这样的事情?
Is there a way to do something like that ?
推荐答案
这里有两种从 Gradle 传递值以在 Java 中使用的方法;
Here are two ways to pass value from Gradle to use in Java;
android {
buildTypes {
debug {
buildConfigField "int", "FOO", "42"
buildConfigField "String", "FOO_STRING", ""foo""
buildConfigField "boolean", "LOG", "true"
}
release {
buildConfigField "int", "FOO", "52"
buildConfigField "String", "FOO_STRING", ""bar""
buildConfigField "boolean", "LOG", "false"
}
}
}
您可以使用 BuildConfig.FOO
android {
buildTypes {
debug{
resValue "string", "app_name", "My App Name Debug"
}
release {
resValue "string", "app_name", "My App Name"
}
}
}
您可以使用 @string/app_name 或 R.string.app_name
这篇关于是否可以在 Gradle 中声明一个可在 Java 中使用的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以在 Gradle 中声明一个可在 Java 中使用的变量?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
