[英]How to use specific fields for specific application builds (debug/release)?
I use gradle
to build my android project. 我使用
gradle
构建我的android项目。 When the build type is Debug
, I want to change a boolean value in java file Constans.java
. 当构建类型为
Debug
,我想在Java文件Constans.java
更改一个布尔值。
public static final boolean IS_TEST_ENVIRONMENT = false;
I use this value to control my app request different server. 我使用此值来控制我的应用程序请求其他服务器。 How to do that, I can't find any information.
怎么做,我找不到任何信息。
You could create your own build variables: 您可以创建自己的构建变量:
buildTypes {
debug {
buildConfigField "boolean","IS_TEST_ENVIRONMENT","false"
}
}
You can add as many fields as you want by editing your build.gradle
file like this: 您可以通过编辑
build.gradle
文件来添加任意多个字段,如下所示:
buildTypes {
debug {
buildConfigField "boolean","IS_TEST_ENVIRONMENT","true"
buildConfigField "boolean","SOME_OTHER_FIELD","false"
}
release {
buildConfigField "boolean","IS_TEST_ENVIRONMENT","true"
buildConfigField "boolean","SOME_OTHER_FIELD","true"
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
For me however it would not at first recognize the field with 但是对我来说,它起初并不会意识到
if (BuildConfig.IS_TEST_ENVIRONMENT) {
// Do something on Debug build
}
if (BuildConfig.SOME_OTHER_FIELD) {
// Do something else
}
Rebuilding didn't help either. 重建也无济于事。 What helped was just simply restarting my AndroidStudio.
有用的只是重启我的AndroidStudio。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.