解決 AndroidStudio 中 gradle 的 buildTypes 配置不生效問題
阿新 • • 發佈:2019-02-17
在開發過程中,我們喜歡把開發和現網壞境的配置區別開,比如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版本,主專案根據編譯型別去引用。