您的位置:首页 > 产品设计 > UI/UE

(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]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: