Android 友盟多渠道打包
阿新 • • 發佈:2018-12-11
目錄
一、簡介
為了統計不同渠道上的下載數量及使用者活動情況,方便後臺做運營統計。我們需要在安裝包中新增不同的標識(渠道資訊 Channel)。多渠道的實現方式多種多樣,此篇只實現友盟多渠道打包的方法。友盟多渠道打包方法試用於少量的渠道,如果要打包十幾個或上百個的渠道就要花費大量的時間,影響效率。
二、基本配置
(1)基於專案整合友盟統計的基礎上,我們只需要在AndroidManifest.xml 檔案中修改渠道資訊:
<meta-data android:value="******" android:name="UMENG_APPKEY"/> <meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
(2)在module的build.gradle的android{}中新增如下內容:
android { ... ... // 多渠道打包 productFlavors { wandoujia { } xiaomi { } } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } // 修改命名規則3.0之前可以使用 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def fileName = outputFile.name.replace(".apk","-${defaultConfig.versionName}.apk") output.outputFile = new File(outputFile.parent, fileName) } } }
三、AS升到3.0後遇到的錯
(1)AS升到3.0後 output.outputFile變成了只讀模式,不能再往裡面寫東西了。這時我們需要修改命名規則
//修改後的命名規則 applicationVariants.all { variant -> variant.outputs.all { def formattedDate = new Date().format('yyyy_MM_dd') outputFileName = rootProject.getName()+"-"+variant.flavorName+"-"+buildType.name+"-"+formattedDate+"-v"+defaultConfig.versionName+"-"+ defaultConfig.versionCode+ ".apk"; } }
(2)Android Plugin3.0的依賴機制:在使用library時會自動匹配variant(debug, release),就是說app的debug會自動匹配library的debug。同樣如果使用flavor的時候,比如app的redDebug同樣會自動匹配library的readDebug。雖然有這樣的優勢,但是在使用flavor時,必須定義flavor dimension,否則會提示錯誤(不知道是哪位大神說的感覺蠻有道理的 借用一下,反正我是菜鳥,不是很懂。)。所以需要在以下加上這句話
//在下面新增 flavorDimensions "versionCode" 即可
defaultConfig {
applicationId "*********"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
flavorDimensions "versionCode"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}