我如何在Android代码中知道build.gradle中参数的值是什么

时间:2018-08-08 15:54:13

标签: android gradle android-gradle

在我的build.gradle中,我有两种构建类型:

buildTypes {
    debug {
        debuggable true
    }
    release {
        debuggable false
    }
}

在我的Android项目的入口点MainApplication.create()中,我实例化了一个类ApplicationComponent

我已将类ApplicationComponent扩展为具有一些额外方法的类DebugApplicationComponent

我希望在debuggabletrue的情况下,MainApplication.create()应该实例化DebugApplicationComponent而不是ApplicationComponent

但是我怎么在MainApplication.create()中知道debuggable是真的?

如果我在Cmd-clickdebuggable,我会上一个名为DefaultBuildType的课程。我不确定此类是什么,或者如何从MainApplication引用它。

2 个答案:

答案 0 :(得分:3)

只需使用

if (BuildConfig.DEBUG) {
   //Do whatever you want
}

答案 1 :(得分:0)

您可以在app/build.gradle配置文件中创建自己的变量。

  buildTypes {
          debug {
              debuggable true
              buildConfigField "Boolean", "LOG_REQUIRED", "true"
          }

          release {
              debuggable false
              buildConfigField "Boolean", "LOG_REQUIRED", "false"
          }
  }

并像这样BuilConfig.LOG_REQUIRED

使用它