• BuildConfig是什么
  • BuildConfig有哪些变量
  • BuildConfig在哪里
  • BuildConfig如何使用
  • BuildConfig可以添加其他变量吗
  • 参考文章

本文是自学BuildConfig的一些小知识点,希望对你有所帮助。

原文

android的BuildConfig学习

BuildConfig是什么?

BuildConfig是android在编译过程中自动生成的一个配置文件。

在不同的编译模式下会生成不同的变量,我们可以利用这些变量来方便不同编译环境下的开发,比如日志的打印(开发环境下可以打印Verbose一级,发布环境下可以打印Warn一级)。

BuildConfig有哪些变量?

没有自己变动过gradle文件的话,自动生成的BuildConfig一般如下文所示。

public final class BuildConfig {  public static final boolean DEBUG = Boolean.parseBoolean("true");  public static final String APPLICATION_ID = "com.xxxx.xxx.xx";  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";}

BuildConfig在哪里?

如图所示

BuildConfig如何使用?

同java常量。

public class MainActivity extends AppCompatActivity {    TextView msgText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        String lineSep = System.getProperty("line.separator", "\n");        msgText = (TextView) findViewById(R.id.msgText);        String DEBUG = "DEBUG = "                + BuildConfig.DEBUG                + lineSep;        String APPLICATION_ID = "APPLICATION_ID = "                + BuildConfig.APPLICATION_ID                + lineSep;        String BUILD_TYPE = "BUILD_TYPE = "                + BuildConfig.BUILD_TYPE                + lineSep;        String FLAVOR = "FLAVOR = "                + BuildConfig.FLAVOR                + lineSep;        String VERSION_CODE = "VERSION_CODE = "                + BuildConfig.VERSION_CODE                + lineSep;        String VERSION_NAME = "VERSION_NAME = "                + BuildConfig.VERSION_NAME;        String msg = DEBUG                + APPLICATION_ID                + BUILD_TYPE                + FLAVOR                + VERSION_CODE                + VERSION_NAME;        msgText.setText(msg);    }}

BuildConfig可以添加其他变量吗?

可以的。

在app模块的build.gradle中(不是Project的),有个buildTypes节点,我们修改如下。

    buildTypes {        debug {            buildConfigField "int", "myInt", "0"            buildConfigField "String", "myStr", "\"hello\""            buildConfigField "boolean", "myFlag", "true"        }        release {            buildConfigField "int", "myInt", "1"            buildConfigField "String", "myStr", "\"world\""            buildConfigField "boolean", "myFlag", "true"            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }

其中myIntmyStrmyFlag是我们自己定义的,编译后的BuildConfig

public final class BuildConfig {  public static final boolean DEBUG = Boolean.parseBoolean("true");  public static final String APPLICATION_ID = "com.qefee.pj.testbuildconfig";  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 boolean myFlag = true;  public static final int myInt = 0;  public static final String myStr = "hello";}

可以看到系统已经为我们生成了.

参考文章

  • Android BuildConfig.DEBUG的使用

  • Pro Tip: Android BuildConfig

更多相关文章

  1. 如何在Windows下搭建Android开发环境
  2. Win7+Eclipse下Android开发环境配置
  3. 手把手搭建 android 开发环境||资源打包下载【更新到android 4.2
  4. Android文章博客收藏
  5. Android开发环境(入门)
  6. Ubuntu 10.10从零开始建立android 2.2 froyo开发环境
  7. Android Sensor详解(5)搭建adsp firmware的环境
  8. Android 编译环境配置搭建(Ubuntu 12.04)及环境导致编译错误QA

随机推荐

  1. 浅谈android的selector背景选择器
  2. Android(安卓)UI学习 - 对话框 (AlertDia
  3. Android触控基础:MotionEvent
  4. Android(安卓)启动过程详解
  5. 【Android】19.3 ContentProvider及安卓
  6. Android(安卓)Toast 长期显示解决方案
  7. Android(安卓)中自定义View的应用 (自绘T
  8. Android(安卓)中文 API ——对话框 Alert
  9. Android(安卓)事件处理基于Handler 消息
  10. Android经典面试题大全[陆续完善中......