(2.2.8.7) Android中BuildConfig类的那些事
2017-06-29 15:25
267 查看
声明
本文章都只是在AndroidStudio基于
Gradle构建项目开发的验证,所以不保证其它开发环境与构建项目方式也是这样
BuildConfig身在何处
了解一个东西前,至少先要知道这东西在哪里吧!而我们今天要了解的这个类又在哪里了,我相信应该还有一些安卓开发人员没见过此类的身影。那么这类在哪里了?答案:一般情况是在
applicationId<应用包名>.BuildConfig;如:我的应用ID为:
com.jay.demo,那么此类的全类名就是
com.jay.demo.BuildConfig;
但这是一般情况,也就是说我们在创建工程时确定的应用包名,但这里答案准确的来说,此类是和
R<resouce>类在同一个包里的,那么
R<resouce>类的名路径是怎么确定的了?
答案:很明确,是由
AndroidManifest.xml文件中的
manifest标签中的
package属性指定的包路径
BuildConfig有啥用
我们先从类名来试图理解这个类是做什么的,BuildConfig很明显是由
Build与
Config组成,
Build = 构建,
Config = 配置,那么直译过来就是
BuildConfig = 构建配置,大致猜到了这个类可能会与一个配置相关的信息
BuildConfig的真面目
package com.jay.demo; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.jay.demo"; 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"; public BuildConfig() { } }1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
[/code]
这是创建一个项目后
BuildConfig类,那这类中有些啥东西了?如果看过我之前的一篇文章,可能会更好的理解–Android
Studio中Module的build.gradle详解,说之前科普下一个小知识,此类是不可修改的,严格来说不能通过我们之前正常编码那样对类一样修改
package com.jay.demo; public final class BuildConfig { //这个常量是标识当前是否为`debug`环境,由`buildType`中的`debuggable`来确定的,这是修改此类值的一个方式 public static final boolean DEBUG = Boolean.parseBoolean("true"); //application id public static final String APPLICATION_ID = "com.jay.demo"; //当前编译方式 public static final String BUILD_TYPE = "debug"; //编译渠道,如果没有渠道,则为空 public static final String FLAVOR = ""; //版本号 public static final int VERSION_CODE = 1; //版本名,所以获取当前应用的版本名可以直接 BuildConfig.VERSION_NAME public static final String VERSION_NAME = "1.0"; public BuildConfig() { } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[/code]
上篇文章已经简单讲解了
BuildConfig类,今天我们来学习怎么扩展一些我们自己的信息进去
给FLAVOR字段赋值
FLAVOR字段是在我们多渠道打包的时候会自动赋值的,
value取的就是我们的渠道名<怎么利用AndroidStudio打多渠道,请大家自行找搜索引擎>。
下面我们直接来实操一下:
android { ...... productFlavors{ 应用宝{ } } ...... }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
[/code]
package com.jay.demo; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.jay.demo"; 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"; public BuildConfig() { } }1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
[/code]
这时我们进入
BuildConfig,就可以看到
FLAVOR被赋值了。
添加自己的字段
BuildConfig自有的一些常量值可能并不是很厉害,但如果可以添加自己想要的一些值就好了,这样就可以把一些常量值放置在此类了,很庆幸,这样的需求完全可以实现。
我们假设有这么一个需求,一般我们
app和服务端交互时,要请求服务端的
Url,然而
BaseUrl在开发时大家一般都是抽出来定义成常量,这里我们就把这个
BaseUrl写到1
BuildConfig中。
android { ...... buildType { debug { buildConfigField "String","BASE_URL","\"http://www.test.com/\"" buildConfigField "int","DATE","20160701" } } }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
[/code]
我们在
buildType中的任意type(包括自定义的)中输入
buildConfigField语法来实现的,此方法有三个参数
buildConfigField(String type,String name,String value),解释下:
String type | 要创建的字段类型,如上面的String与 int |
---|---|
String name | 要创建的字段名,如上面的BASE_URL与 DATE |
String value | 创建此字段的值,如上面的\"http://www.test.com/\"与 20160701 |
String时,定义value的时候要注意加上字符串不能缺少的双引号
"",由于参数本身要传入的类型也是
String,所以我们在添加的时候加上转义字符。
package com.jay.demo; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.jay.demo"; 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"; public static final String BASE_URL = "http://www.test.com/"; public static final int DATE = 20160701; public BuildConfig() { } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[/code]
相关文章推荐
- Android中BuildConfig类的那些事<二>
- Activity的那些事『Android系列三』
- android和iphone 那些事
- android手机上的那些事
- android 优化那些事之图片缓存设计/如何高效的展示图片(三)
- 【Android界面实现】关于自定义Dialog的那些事
- Android 重写view的那些事
- Android 面试那些事之Java基础
- android屏幕适配的那些事~
- android 优化那些事之布局(一)
- android那些事是——交互性
- Android之Activity的那些事
- 关于Android权限那些事(应用程序获取root权限,system权限)
- android开发中,那些年关于焦点返回的那些事
- android 那些事---主线程是线程不安全的
- android那些事——android的成长
- Android 那些事
- Android之Handler与AsyncTask的那些事
- Android 面试那些事之android基础
- Android ListView那些事