Android Gradle Library 中使用BuildC
一. 坑
在 [《Android: 使用BuildConfig.DEBUG优化你的Log输出 & 开启混淆(proguard)的优化配置》]
(http://www.jianshu.com/p/658eb18838df) 这篇中推荐把
DevUtil放到公共库中去, 以方便重用, 但是这里有个巨坑: **主
module对library module的依赖都是release依赖**
a. 测试环境: AndroidStudio2.2 + Gradle Plugin 2.2.0 + Gradle 2.14.1
b. 在AndroidStudio中创建一个项目, 项目有一个主module和一个library module, 目录结构如下:
.../android-project/ --- 项目目录
.../android-project/app --- 主module
.../android-project/library --- library module
当我们对主module进行构建时, 会先构建library module, 但是library的构建永远是使用release模式构建的. 使用命令打debug包./gradlew :app:assembleDebug, log输出如下:
......:library:compileReleaseJavaWithJavac UP-TO-
DATE:library:extractReleaseAnnotations UP-TO-
DATE:library:mergeReleaseShaders UP-TO-
DATE:library:compileReleaseShaders UP-TO-
DATE:library:generateReleaseAssets UP-TO-
DATE:library:mergeReleaseAssets UP-TO-
DATE:library:mergeReleaseProguardFiles UP-TO-
DATE:library:packageReleaseRenderscript UP-TO-
DATE:library:packageReleaseResources UP-TO-
DATE:library:processReleaseJavaRes UP-TO-
DATE:library:transformResourcesWithMergeJavaResForRelease UP-TO-
DATE:library:transformClassesAndResourcesWithSyncLibJarsForRelease UP-TO-DATE:library:mergeReleaseJniLibFolders UP-TO-
DATE:library:transformNative_libsWithMergeJniLibsForRelease UP-TO-
DATE:library:transformNative_libsWithSyncJniLibsForRelease UP-TO-DATE:library:bundleRelease UP-TO-
DATE......:app:validateSigningDebug:app:packageDebug UP-TO-DATE:app:assembleDebug UP-TO-DATEBUILD SUCCESSFULTotal time: 11.475 secs
从log输出可以知道library module打包方式是使用release模式打包的, 因此BuildConfig.DEBUG的值自然是false了. 如果主module的buildType是release, 那么依赖的library module就更不用说了, buildType肯定是release../gradlew :app:assembleRelease
打release包, log输出如下:
......:library:packageReleaseRenderscript UP-TO-DATE:library:packageReleaseResources UP-TO-DATE:library:processReleaseJavaRes UP-TO-DATE:library:transformResourcesWithMergeJavaResForRelease UP-TO-DATE:library:transformClassesAndResourcesWithSyncLibJarsForRelease UP-TO-DATE:library:mergeReleaseJniLibFolders UP-TO-DATE:library:transformNative_libsWithMergeJniLibsForRelease UP-TO-DATE:library:transformNative_libsWithSyncJniLibsForRelease UP-TO-DATE:library:bundleRelease UP-TO-DATE......:app:packageRelease:app:assembleReleaseBUILD SUCCESSFULTotal time: 20.195 secs
这么一来, library module中的BuildConfig.DEBUG的值永远都是false. 如《Android: 使用BuildConfig.DEBUG优化你的Log输出 & 开启混淆(proguard)的优化配置》一文中所说把DevUtil工具类放到公共库中以便代码重用
, DevUtil是不能正常工作的, 因为依赖库总是以release模式打包, 导致BuildConfig.DEBUG的值总是false, 进而导致log永远不能输出. 那么解决DevUtil工具类的log输出问题, 就变成了解决依赖库的打包问题.
其实这个打包问题很早就有人提出来了, 大概时2013年5月的时候
关于这个问题的issue地址: https://code.google.com/p/android/issues/detail?id=52962
二. 解决方案issue上有很多hack, 下面就介绍其中一种比较简洁的hack, 如图:
在library的build.gradle文件中添加下面代码:
android { ...... ......
productFlavors { create('all') { } }
publishNonDefault true}
configurations {
allDebug
allRelease}
在主module的build.gradle中用下面的方式对library进行依赖
//省略其余配置............dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1' //依赖library
debugCompile project(path: ':library', configuration: 'allDebug')
releaseCompile project(path: ':library', configuration: 'allRelease') //省略其余依赖 ..... .....}
这样library库中的BuildConfig.DEBUG就会根据buildType生成不同的值.
涉及到DevUtil工具类的地方, 可以结合《Android: 使用BuildConfig.DEBUG优化你的Log输出 & 开启混淆(proguard)的优化配置》阅读