在settings.gradle最后加上(在configuration之前确定好variant):
def determineReleaseBuild() {
boolean valueAssigned = false
if (gradle.startParameter.taskRequests.size() > 0) {
for (String arg : gradle.startParameter.taskRequests.get(0).args) {
if (arg.contains('assemble') && arg.contains('Release')) {
gradle.ext.releaseBuild = true
valueAssigned = true
break
}
}
}
if (!valueAssigned) {
gradle.ext.releaseBuild = false
}
}
determineReleaseBuild()
然后在library module的gradle里加上这个判断即可 :
android {
defaultPublishConfig gradle.releaseBuild ? 'release' : 'debug'
}
做好配置后,则无须在app module里面对每个library module都分别写releaseCompile和debugCompile。