在开发过程中,我们喜欢把开发和现网坏境的配置区别开,比如IP、端口等。gradle也很好地给我们提供了buildTypes的功能,如下所示:

buildTypes {    debug {        buildConfigField "String", "httpServerIp", "\"10.10.19.15\""        buildConfigField "String", "fileServerIp", "\"http://10.10.19.15:8099/\""        buildConfigField "int", "httpPort", "8020"    }    release {        buildConfigField "String", "httpServerIp", "\"10.10.19.18\""        buildConfigField "String", "fileServerIp", "\"http://10.10.19.18:8099/\""        buildConfigField "int", "httpPort", "8020"    }}

这个功能在主项目中使用的话是没有什么问题的,但如果是依赖的Module项目中的话,不管你是debug还是release构建,配置的信息都会是release中的,这给我造成了很大的困扰。

解决方法:

Library:

android {    publishNonDefault true}

App:

dependencies {    releaseCompile project(path: ':library', configuration: 'release')    debugCompile project(path: ':library', configuration: 'debug')}

这样的话Library项目每次编译的时候会同时生成对应的debug和release版本,主项目根据编译类型去引用。