gradle作为Android开发最好用的一款构建工具,它的功能相当丰富,其中有一个极为常见的就是构建类型buildTypes
。
在开发中经常会遇到这样的场景,线上、线下的后台接口Url
不同,也有的可能图片和文件url
都是分开的,这时部分猿友们可能会通过设置全局变量的方式去定义,比如下面的写法
然后在打包debug.apk
和release.apk
时修改成相应的url
,这样很麻烦,容易忘记或者写错,其实gradle
都帮我们想到了,不然它也得不到那么多猿友(哈哈)的认可。
设置URL
1、在build.gradle文件中找到buildTypes,填写需要的url
信息
buildTypes {
debug {
buildConfigField "boolean", "TEST_DEBUG_MODE", "true"
buildConfigField "String", "TEST_BASE_URL", "\"https://www.base.com\""
buildConfigField "String", "TEST_IMAGE_URL", "\"https://www.image.com\""
buildConfigField "String", "TEST_FILE_URL", "\"https://www.file.com\""
debuggable true
}
release {
buildConfigField "boolean", "TEST_DEBUG_MODE", "false"
buildConfigField "String", "TEST_BASE_URL", "\"https://www.release.base.com\""
buildConfigField "String", "TEST_IMAGE_URL", "\"https://www.release.image.com\""
buildConfigField "String", "TEST_FILE_URL", "\"https://www.release.file.com\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
}
}
2、此时默认的BuildConfig类中会出现上面配置的信息(因为用as在开发过程中就是debug模式,所以此类显示的是debug模式下的url
)
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.buildconfig.test";
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";
// Fields from build type: debug
public static final String TEST_BASE_URL = "https://www.base.com";
public static final boolean TEST_DEBUG_MODE = false;
public static final String TEST_FILE_URL = "https://www.file.com";
public static final String TEST_IMAGE_URL = "https://www.image.com";
}
3、BuildConfig类的使用方法可根据个人习惯而定,下面是我测试调用的。在debug模式下运行app,吐司的则是debug配置中的信息,打包release.apk运行吐司的则是release配置中的信息
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
Toast.makeText(this, BuildConfig.TEST_DEBUG_MODE + "", Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(this, BuildConfig.TEST_BASE_URL, Toast.LENGTH_LONG).show();
break;
case R.id.button3:
Toast.makeText(this, BuildConfig.TEST_IMAGE_URL, Toast.LENGTH_LONG).show();
break;
case R.id.button4:
Toast.makeText(this, BuildConfig.TEST_FILE_URL, Toast.LENGTH_LONG).show();
break;
}
}
至此,简单的配置和用法演示完毕,个人觉得非常有用(有不对的地方欢迎指教,勿喷),如果你现在的项目还没有引用,抓紧配置吧,老板会觉得你是个人才,升值、加薪、走上人生巅峰,接下来你随意(哈哈)。
拓展一点信息,有时候项目如果复杂一点,debug
和release
这两个BuildTypes类型不能满足我们的需求,则可以在此基础上继续添加
添加buildTypes类型
1、File->Project Structure->Build Types,对应的参数参考官网
2、build.gradle
文件会出现刚才添加的名称
buildTypes {
debug {
buildConfigField "boolean", "TEST_DEBUG_MODE", "false"
buildConfigField "String", "TEST_BASE_URL", "\"https://www.base.com\""
buildConfigField "String", "TEST_IMAGE_URL", "\"https://www.image.com\""
buildConfigField "String", "TEST_FILE_URL", "\"https://www.file.com\""
debuggable true
}
release {
buildConfigField "boolean", "TEST_DEBUG_MODE", "true"
buildConfigField "String", "TEST_BASE_URL", "\"https://www.release.base.com\""
buildConfigField "String", "TEST_IMAGE_URL", "\"https://www.release.image.com\""
buildConfigField "String", "TEST_FILE_URL", "\"https://www.release.file.com\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
}
detest {
debuggable true
}
}
3、构建APK
时会出现相应的选项,打包时选择哪一项就会匹配哪一项的url
,不需要我们在代码中改来改去