(2.2.8.7) Android中BuildConfig类的那些事

来源:互联网 发布:如何做淘宝直通车 编辑:程序博客网 时间:2024/06/07 20:48


声明

本文章都只是在AndroidStudio基于Gradle构建项目开发的验证,所以不保证其它开发环境与构建项目方式也是这样

BuildConfig身在何处

了解一个东西前,至少先要知道这东西在哪里吧!而我们今天要了解的这个类又在哪里了,我相信应该还有一些安卓开发人员没见过此类的身影。那么这类在哪里了? 
答案:一般情况是在applicationId<应用包名>.BuildConfig;如:我的应用ID为:com.jay.demo,那么此类的全类名就是com.jay.demo.BuildConfig

但这是一般情况,也就是说我们在创建工程时确定的应用包名,但这里答案准确的来说,此类是和R<resouce>类在同一个包里的,那么R<resouce>类的名路径是怎么确定的了? 
答案:很明确,是由AndroidManifest.xml文件中的manifest标签中的package属性指定的包路径

BuildConfig有啥用

我们先从类名来试图理解这个类是做什么的,BuildConfig很明显是由BuildConfig组成,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

这是创建一个项目后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

上篇文章已经简单讲解了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
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

BuildConfig.class

这时我们进入BuildConfig,就可以看到FLAVOR被赋值了。

添加自己的字段

BuildConfig自有的一些常量值可能并不是很厉害,但如果可以添加自己想要的一些值就好了,这样就可以把一些常量值放置在此类了,很庆幸,这样的需求完全可以实现。 
我们假设有这么一个需求,一般我们app和服务端交互时,要请求服务端的Url,然而BaseUrl在开发时大家一般都是抽出来定义成常量,这里我们就把这个BaseUrl写到1BuildConfig中。

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

我们在buildType中的任意type(包括自定义的)中输入buildConfigField语法来实现的,此方法有三个参数buildConfigField(String type,String name,String value),解释下:

String type要创建的字段类型,如上面的StringintString name要创建的字段名,如上面的BASE_URLDATEString 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

Custom BuildConfig.class




原创粉丝点击