Android gradle 配置productFlavors 子模块配置productFlavors


AndroidStudio gradle 配置 productFlavors

gradle版本
distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-all.zip

主模块配置productFlavors

app/build.gradle

android {
  ...
    flavorDimensions 'default' // 定义必要的配置 否则会报错 可定义多个 flavorDimensions 'default', 'app'
    productFlavors {
   		// 如果用way3方法 该名字不能和主模块相同 否则报错 ERROR: Unable to resolve dependency for ':app@zkDebugUnitTest/compileClasspath':
        coma { 
            dimension = 'default'
             // 定义不同的包名
            applicationId = "com.test.demo1"
            manifestPlaceholders = [
                    KEY_AMAP_API : "xxxxxxxxxxxxxxxxxxxx",
                    // 定义不同的APP名称
//                    APP_LABLE_FLAG : "@string/app_name", 
            ]
            // 自定义buildConfig.java中的变量供代码中使用
            buildConfigField "String", "APPID_BUGLY", '"123151554d"' 

        }

        comb {
            dimension = 'default'
            applicationId = "com.test.demo2"
            manifestPlaceholders = [
                    KEY_AMAP_API : "yyyyyyyyyyyyyyyyyyyy"
            ]
            buildConfigField "String", "APPID_BUGLY", '"adf88sfds8fd"'

        }
    }
}

// way3 begin 不用方法3则不需要定义该way3包裹代码
/*configurations {
    comaDebugImplementation
    comaReleaseImplementation
    combDebugImplementation
    combReleaseImplementation
}*/
// way3 end

dependencies {
    implementation project(':upgrade') // way1 推荐使用该方法
    
    // combImplementation project(path: ":upgrade", configuration: 'comaDebug') // way2 不用定义 configurations 
// way3 begin    
//	  comaImplementation project(path: ":upgrade", configuration: 'bbDebug')
//    comaImplementation project(path: ":upgrade", configuration: 'bbRelease')
//    combDebugImplementation project(path: ":upgrade", configuration: 'ccbDebug')
//    combReleaseImplementation project(path: ":upgrade", configuration: 'ccRelease')
// way3 end
}

子模块配置productFlavors

module1/build.gradle

android {
...
    publishNonDefault true
    flavorDimensions 'default'
    productFlavors {
        coma {
            dimension = 'default'
            buildConfigField "String", "APPID_BUGLY", '"123151554d"'
        }

        comb {
            dimension = 'default'
            buildConfigField "String", "APPID_BUGLY", '"adf88sfds8fd"'
        }
    }
}

参考:
http://www.luyixian.cn/news_show_349834.aspx

posted @ 2022-03-12 22:04  xiaomodecnblogs  阅读(377)  评论(0编辑  收藏  举报