参考:
http://stackoverflow.com/questions/20176284/buildconfig-debug-always-false-when-building-library-projects-with-gradle#
修改默认的发布配置
通过 android.publishNonDefault = true 启用其他variants的发布
通过 compile project(path: ‘:project’, configuration: ‘flavor1Debug’) 引用指定的variant
library module :
1 2 3
| android { publishNonDefault true }
|
app module:
1 2 3 4
| dependencies { debugCompile project(path: ':library', configuration: 'debug') releaseCompile project(path: ':library', configuration: 'release') }
|
自定义buildconfig field
library module:
1 2 3 4 5 6 7 8 9 10
| android { buildTypes { debug { buildConfigField "boolean", "LOG_DEBUG", "true" } release { buildConfigField "boolean", "LOG_DEBUG", "false" } } }
|
java code:
1 2 3
| if (BuildConfig.LOG_DEBUG) { }
|
ReBuild之后你会发现debug和release模式下BuildConfig中LOG_DEBUG的值是不同的。
这种方式不仅用与此,你还可以添加其他的字段,比如debug和release模式下不同的API base url,等等。