Android幾種多渠道打包


1、什么是多渠道打包
  在不同的應用市場可能有不同的統計需求,需要為每個應用市場發布一個安裝包,這里就引出了Android的多渠道打包。在安裝包中添加不同的標識,以此區分各個渠道,方便統計app在市場的各種。

2、幾種打包方式

  • 友盟 UMeng
  • Android Studio自帶
  • 美團 Walle

3、開始使用

  3.1 友盟UMeng

  第一步:在AndroidManifest中添加

<meta-data    
    android:name="UMENG_CHANNEL"    
    android:value="${channel}" />

  第二步:在build.gradle中添加,baidu {}為指定渠道名稱簡寫

build {
    ......
    productFlavors { 
        baidu {}
        xiaomi {}
        qihu360 {}
        yingyongbao {}
        huawei {}
    } 
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name]
    }   
}

  第三步:設置輸出APK名稱

  Android Studio 2.3版本:

build {
    ......
    applicationVariants.all { variant ->
         variant.outputs.each { output ->
             def outputFile = output.outputFile
             if (outputFile != null && outputFile.name.endsWith('.apk')) {
                 def fileName = "driver_${variant.productFlavors[0].name}_v${defaultConfig.versionName}.apk" 
                 output.outputFile = new File(outputFile.parent, fileName)
             }
         }
     }
}

  Android Studio 3.0版本:

build {
    ......
    applicationVariants.all { variant ->
         variant.outputs.all {
            outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk"
        }
    }
}

  Gradle后如果出現如下報錯

  則需要配置flavor dimension的維度是該版本號,這樣維度就是都是統一的了

build {
    ......
    defaultConfig {
        ......
        flavorDimensions "versionCode"
    }
}

  第四步:編譯打包

  Build - Generate Signed Bundle or APK - 選擇Release或Debug

  3.2 Android Studio自帶

  Android Studio多形態打包與友盟打包方式相同,但是標簽<meta-data>中name可自行定義,不限制為"UMENG_CHANNEL"

<meta-data    
    android:name="UMENG_CHANNEL" //可以隨意定義
    android:value="${channel}" />

  3.3 美團 Walle

  第一步:配置根build.gradle

buildscript {
   dependencies {
         classpath 'com.mcxiaoke.packer-ng:plugin:2.0.1'
    }
}

  第二步:配置App build.gradle

apply plugin: 'packer'
dependencies {
    ......
    implementation 'com.mcxiaoke.packer-ng:helper:2.0.1'
}

  第三步:插件配置

build {
    ......
    packer {
        archiveNameFormat = '${buildType}-v${versionName}-${channel}'  // 定義輸出APK名稱
        archiveOutput = new File(project.rootProject.buildDir, "apks")  // 設置APK輸出目錄
        channelFile = new File(project.rootDir, "channel.txt")  // 添加渠道配置文件
    }
}

  第四步:新建渠道配置文件channel.txt

  在工程根目錄下新建channel.txt文件,如圖

  文件內容為渠道名稱,要求:必須每一行一個渠道

  第五步:編譯打包

  使用Terminal命令:

gradlew clean apkRelease

可參考:[美團多渠道打包官方文檔](https://github.com/mcxiaoke/packer-ng-plugin)

4、獲取渠道信息

  1、友盟 和 Android Studio獲取方式

  通過讀取AndroidManifest中<meta-data>標簽

private String getChannel() {
    try {    
      PackageManager pm = getPackageManager();    
      ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);  
      String channel = appInfo.metaData.getString(key);  // key為<meta-data>標簽中的name      
      if (!TextUtils.isEmpty(channel)) {           
         return channel;
      }
  } catch (Exception e) {    
      e.printStackTrace();
  }
  return null;
}

  2、美團 Walle獲取方式

  美團集成自帶獲取方法

// 如果沒有找到渠道信息或遇到錯誤,默認返回的是""
// com.mcxiaoke.packer.helper.PackerNg
String channel = PackerNg.getChannel(Context);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM