###1、在AndroidManifest.xml中添加
android:name="CHANNEL"android:value="${CHANNEL_VALUE}" />复制代码
###2、在app模块下的build.gradle中添加一下代码
android { defaultConfig { // 默认是umeng的渠道 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"] flavorDimensions "release" } lintOptions { abortOnError false } signingConfigs { debug { storeFile file("../keystore/xiaoxing.jks") storePassword "xuxingxing" keyAlias "xiaoxing" keyPassword "xuxingxing" } release { storeFile file("../keystore/xiaoxing.jks") storePassword "xuxingxing" keyAlias "xiaoxing" keyPassword "xuxingxing" } } buildTypes { debug { minifyEnabled false buildConfigField "boolean", "LOG_DEBUG", "true" } release { minifyEnabled false // Zipalign优化 zipAlignEnabled true // 移除无用的resource文件 shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release // 自定义输出配置 applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { // 输出apk名称为JPay_0.0.1_xiaomi.apk def fileName = "xiaoxing_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk" // output.outputFile = new File(outputFile.parent, fileName) outputFileName = new File("", fileName) } } } } } //多渠道打包 productFlavors { maqigou { dimension "release" // 每个环境包名不同 applicationId "com.qingqu.wc.maqi" // 动态添加 string.xml 字段; // 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!! resValue "string", "app_name", "瑪奇購" resValue "bool", "auto_updates", 'false' // 动态修改 常量 字段 buildConfigField "String", "ENVIRONMENT", '"瑪奇購"' // 修改 AndroidManifest.xml 里渠道变量 manifestPlaceholders = [CHANNEL_VALUE: "maqigou" , app_icon : "@mipmap/logo"] } } productFlavors.all { flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name] }}复制代码
###3、说明
AndroidStudio3.0版本多渠道打包和之前的版本打包的区别: ① 在 defaultConfig 中添加 flavorDimensions "release,debug" ② 在buildTypes 中release 中 之前的写法是 output.outputFile = new File(outputFile.parent, fileName) 现在改为 outputFileName = new File("", fileName) ③ 在productFlavors 中的maqigou 添加 dimension "release"或者dimension "debug"