在gradle中定义全局变量
- 根据配置buildTypes中各个不同编译情况下,自定义参数值.
buildTypes {
release {
buildConfigField "boolean","testFlag","false"
}
debug{
buildConfigField "boolean","testFlag","true"
}
}
- 在不同module中的build.gradle中配置不同的配置参数,会在不同module下面生成BuildConfig.java文件.
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.xxx.xxx";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
// Fields from build type: debug
public static final boolean testFlag = true;
}
-
有多个模块则会生成多个BuildConfig.java
-
还可以根据不同buildTypes 定义不同的资源文件参数
buildTypes {
release {
resValue("string","app_name","鲸鱼签到")
}
debug{
resValue("string","app_name","鲸鱼签到Debug")
}
}
在manifest中使用app_name,并且删除资源文件中的app_name,避免冲突
<application
android:label="@string/app_name"
android:theme="@style/AppTheme">
<resources>
<!--<string name="app_name">鲸鱼签到</string>-->
</resources>