Android Studio自定义多渠道打包

现在为了推广产品,会在多个渠道应用市场发布应用,为了统计不同渠道的数据,需要在应用中表明渠道,如果一个一个去修改渠道打包效率会很低。AS为我们提供了简便的方法:配置好所需渠道信息,可以一次打包所有的渠道包。下面就来介绍一下多渠道打包配置和自定义APK打包名称方法。

《Android Studio自定义多渠道打包》 多渠道签名图

1. AS 2.x多渠道打包

(1) AndroidManifest中增加节点

 <!-- 多渠道打包 -->
      ...
        <meta-data
            android:name="UMENG_CHANNEL"
            android:value="${UMENG_CHANNEL_VALUE}" />
      ...

(2) 项目app下 build.gradleandroid中添加配置

android {
      ....
        //多渠道打包
        productFlavors {
            yingyongbao {}
            huawei {}
            baidu {}          
            xiaomi {}
            qh360 {}         
        }
        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
      ....
}
2. AS 2.x自定义APK打包名称
android {
     ....
     //自定义打包时apk名称
     applicationVariants.all { variant ->
          variant.outputs.each { output ->
          def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"
          def outFile = output.outputFile
          if (outFile != null && outFile.name.endsWith('.apk')) {
              output.outputFile = newFile(outFile.parent, fileName)
          }   
     }
     ....
}

APK名称:版本名_渠道名.apk

3. AS 3.x多渠道打包

(1) AndroidManifest中增加节点(和2.x一样,没有变化)

 <!-- 多渠道打包 -->
      ...
        <meta-data
            android:name="UMENG_CHANNEL"
            android:value="${UMENG_CHANNEL_VALUE}" />
      ...

(2) 项目app下 build.gradleandroid中添加配置

  • 多版本打包
android {
      ....
        //多渠道打包
        flavorDimensions "tier","minApi"
        productFlavors {
            yingyongbao {
               dimension "tier"
            }
            huawei {
               dimension "tier"
            }
            baidu {
               dimension "tier"
            }          
            xiaomi {
               dimension "minApi"
            }
            qh360 {
               dimension "minApi"
            }         
        }
        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
      ....
}
  • 单版本打包
android {
   ...
   defaultConfig {
       ...
      flavorDimensions "code"
   }
   productFlavors {
         yingyongbao {}
         huawei {}
         baidu {}          
         xiaomi {}
         qh360 {}         
    }
   productFlavors.all {
          flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
   }
   ...
}

AS 3.0后gradle添加了flavorDimensions属性,用来控制多个版本的代码和资源,缺失会报错

4. AS 3.x自定义APK打包名称
android {
     ....
     //自定义打包时apk名称
     applicationVariants.all { variant ->
          variant.outputs.all { output ->// each 改为 all
          def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"
          def outFile = output.outputFile
          if (outFile != null && outFile.name.endsWith('.apk')) {
              outputFileName = fileName  //  output.outputFile 改为 outputFileName 
              //或者 outputFileName = new File(outFile.parent, fileName).getName()
          }   
     }
     ....
}

APK名称:版本名_渠道名.apk,此外,AS 3.0后打包完,apk所在层级发生了变化:release/xxx.apk,而且还多一个release/output.json参数文件。如果不想要output.json文件,可以打包完成后自动删除。

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def fileName = "${variant.versionName}_${variant.productFlavors[0].name}_release.apk"          
            def outFile = output.outputFile
            if (outFile != null && outFile.name.endsWith('.apk')) {
                outputFileName = new File(outFile.parent, fileName).getName()
            }
        }

        variant.assemble.doLast {
            variant.outputs.all { output ->
                delete output.outputFile.parent + "/output.json"
                copy {
                    from output.outputFile.parent
                    into output.outputFile.parentFile.parent
                }
                delete output.outputFile.parent
            }
        }
    }

这样最后打包后的 apk文件就和 AS 2.x打包后的一样了。

以上就是 AS 2.x和 AS 3.x多渠道打包、自定义打包APK名称对比区别,大家可根据自己的AS版本来选用相应的方法,希望对大家有所帮助!

========== 相关推荐 ==========

Android获取渠道名称

Android Studio签名配置方法

Android常用混淆配置

============================
    原文作者:翻译不了的声响
    原文地址: https://www.jianshu.com/p/14d3d3d9803d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞