【Android】解决 AndroidStudio 中 gradle 的 buildTypes 配置不生效问题

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

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中的,这给我形成了很大的困扰。gradle

解决方法:ui

Library:spa

android {
    publishNonDefault true
}复制代码

App:debug

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

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

相关文章
相关标签/搜索