Android 使用VasDolly 實現多渠道打包
前言:
因為公司專案需要釋出到更多的平臺上,以供使用者下載。打包就成了一個很大的問題。利用原本的打包方式,又慢油耗。主要是電腦慢的話,打一個包都已經要一倆分鐘了,如果需要釋出的渠道多的話,那麼真的就GG了。
當然第一想到的還是使用第三方框架來實現。原始的做法如何配置渠道打包我這就不介紹啦。咱們主要講的就是使用VasDolly來配置完成打包的過程。
這個框架是騰訊的開源框架,大家可以瞭解一下,VasDolly實現原理
大家可以對比一下目前比較流行的三個框架,VasDolly、packer-ng-plugin、Walle。選擇適用自己的。
正文:
先貼一下全部的配置程式碼
apply plugin: 'com.android.application' apply plugin: 'channel' android { compileSdkVersion rootProject.ext.android.compileSdkVersion buildToolsVersion rootProject.ext.android.buildToolsVersion defaultConfig { minSdkVersion rootProject.ext.android.minSdkVersion targetSdkVersion rootProject.ext.android.targetSdkVersion versionCode rootProject.ext.android.versionCode versionName rootProject.ext.android.versionName multiDexEnabled true ndk { abiFilters "armeabi", "armeabi-v7a", "x86", "mips" } } sourceSets rootProject.ext.android.sourceSets dexOptions { javaMaxHeapSize "4g" } flavorDimensions 'default' //等更新到3.0.0的版本AS更換成這個 android.applicationVariants.all { variant -> if (variant.buildType.name == "debug") { // 伺服器地址 buildConfigField "String", "HOST", "\"http://testBaidu.com/\"" } else if (variant.buildType.name == "release") { // 伺服器地址 buildConfigField "String", "HOST", "\"http://www.baidu.com/\"" } variant.outputs.all { outputFileName = "app-${variant.versionName}.apk" } } signingConfigs { debug { storeFile file("test.keystore") storePassword "android" keyAlias "test" keyPassword "123456" } release { storeFile file("test.keystore") storePassword "android" keyAlias test keyPassword "123456" v1SigningEnabled true//開啟V1簽名 v2SigningEnabled true//開啟V2簽名 } } buildTypes { debug { signingConfig signingConfigs.debug proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' debuggable true } release { signingConfig signingConfigs.release debuggable false minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dataBinding { enabled = true } packagingOptions{ exclude "META-INF/rxjava.properties" exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' } useLibrary 'org.apache.http.legacy' //直接編譯生成多渠道包 //命令列操作打包,gradlew channelDebug、gradlew channelRelease channel{ //指定渠道檔案 channelFile = new File(project.rootDir, "channel.txt") //多渠道包的輸出目錄,預設為new File(project.buildDir,"channel") baseOutputDir = new File(project.rootProject.buildDir,"channel") //多渠道包的命名規則,預設為:${appName}-${versionName}-${versionCode}-${flavorName}-${buildType} apkNameFormat ='${appName}-${versionName}-${versionCode}-${flavorName}-${buildType}' //快速模式:生成渠道包時不進行校驗(速度可以提升10倍以上,預設為false) isFastMode = false //buildTime的時間格式,預設格式:yyyyMMdd-HHmmss buildTimeDateFormat = 'yyyyMMdd-HH:mm:ss' //低記憶體模式(僅針對V2簽名,預設為false):只把簽名塊、中央目錄和EOCD讀取到記憶體,不把最大頭的內容塊讀取到記憶體,在手機上合成APK時,可以使用該模式 lowMemory = false } /*根據已有基礎包重新生成多渠道包 rebuildChannel { //命令操作打包 gradlew rebuildChannel //指定渠道檔案 channelFile = new File(project.rootDir, "channel.txt") //存在的基礎包路徑 baseDebugApk = 已有Debug APK baseReleaseApk = 已有Release APK //預設為new File(project.buildDir, "rebuildChannel/debug") debugOutputDir = Debug渠道包輸出目錄 //預設為new File(project.buildDir, "rebuildChannel/release") releaseOutputDir = Release渠道包輸出目錄 //快速模式:生成渠道包時不進行校驗(速度可以提升10倍以上,預設為false) isFastMode = false //低記憶體模式(僅針對V2簽名,預設為false):只把簽名塊、中央目錄和EOCD讀取到記憶體,不把最大頭的內容塊讀取到記憶體,在手機上合成APK時,可以使用該模式 lowMemory = false }*/ aaptOptions { cruncherEnabled = false useNewCruncher = false } } dependencies { //extra lib for libs package implementation fileTree(include: ['*.jar'], dir: 'libs') androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testImplementation rootProject.ext.dependencies.junit implementation rootProject.ext.dependencies.cardviewV7 implementation rootProject.ext.dependencies.multidex implementation rootProject.ext.dependencies.percent implementation rootProject.ext.dependencies.constraintLayout implementation rootProject.ext.dependencies.butterknife annotationProcessor rootProject.ext.dependencies.butterknifeCompiler implementation rootProject.ext.dependencies.switchbutton implementation rootProject.ext.dependencies.easypermissions implementation rootProject.ext.dependencies.TimePickerDialog implementation rootProject.ext.dependencies.chenypAdapter implementation rootProject.ext.dependencies.journeyappsZxing implementation rootProject.ext.dependencies.zxingCore implementation rootProject.ext.dependencies.systembartintSystembartint implementation rootProject.ext.dependencies.systembartintSystembartint implementation rootProject.ext.dependencies.umengAnalytics implementation rootProject.ext.dependencies.flowlayoutLib implementation project(path: ':simple_lib') implementation project(path: ':tribes_module') api 'com.leon.channel:helper:2.0.1' }
接下來說一下步驟細節。
第一步:在專案的build.gradle中新增這行程式碼
classpath 'com.leon.channel:plugin:2.0.1'
第二步:在主APP工程中,新增對VasDolly的引用
apply plugin: 'channel'
第三步:在工程中去引用helper依賴(版本還是看官網當前的版本吧)
api 'com.leon.channel:helper:2.0.1'
第四步:配置渠道列表,這裡呢有兩種方式。(以下部分說明,摘自github VasDolly 點選開啟連結)
1)第一種:在grable.properties檔案指定渠道檔名稱,該渠道檔案不許位於根工程目錄下,一行一個渠道訊號
channel_file=channel.txt
2) 第二種:在 channel 或者 rebuildChannel 屬性中通過 channelFile 屬性指定渠道檔案,一行一個渠道資訊(目前我是使用的這種方式,方便於我在一個地方可以管理)
其中,多渠道包的命名規則中,可使用以下欄位:
- appName : 當前project的name
- versionName : 當前Variant的versionName
- versionCode : 當前Variant的versionCode
- buildType : 當前Variant的buildType,即debug or release
- flavorName : 當前的渠道名稱
- appId : 當前Variant的applicationId
- buildTime : 當前編譯構建日期時間,時間格式可以自定義,預設格式:yyyyMMdd-HHmmss
第五步:開始打包,在Android Studio 下的 Terminal 下輸入命令列即可
gradlew channelDebug 測試
gradlew channelRelease 正式
一個回車鍵....進度條跑起來啦!!!
當我們看到這個BUILD SUCCESSFUL的時候,就說明打包成功啦
看一下這速度,25秒,打出了20個包。厲不厲害,牛X不
另外咱們再說一下,VasDolly 還給我們提供的另外一個方式,已經有基礎包了,根據基礎包生成更多的渠道包。
注意:這裡需要配置的東西有,基礎包的路徑,和輸出的目錄路徑
然後這是的命令就是 gradlew rebuildChannel
rebuildChannel {
//命令操作打包 gradlew rebuildChannel
//指定渠道檔案
channelFile = new File(project.rootDir, "channel.txt")
//存在的基礎包路徑
baseDebugApk = 已有Debug APK
baseReleaseApk = 已有Release APK
//預設為new File(project.buildDir, "rebuildChannel/debug")
debugOutputDir = Debug渠道包輸出目錄
//預設為new File(project.buildDir, "rebuildChannel/release")
releaseOutputDir = Release渠道包輸出目錄
//快速模式:生成渠道包時不進行校驗(速度可以提升10倍以上,預設為false)
isFastMode = false
//低記憶體模式(僅針對V2簽名,預設為false):只把簽名塊、中央目錄和EOCD讀取到記憶體,不把最大頭的內容塊讀取到記憶體,在手機上合成APK時,可以使用該模式
lowMemory = false
}
還有就是,如果你有整合友盟統計,那麼就需要你在,Application 下的 onCreate() 方法中去加多這兩行程式碼了
private void initUmeng() {
//獲取渠道標識
String channel = ChannelReaderUtil.getChannel(getApplicationContext());
MobclickAgent.setDebugMode(true);
// SDK在統計Fragment時,需要關閉Activity自帶的頁面統計,
// 然後在每個頁面中重新整合頁面統計的程式碼(包括呼叫了 onResume 和 onPause 的Activity)。
MobclickAgent.openActivityDurationTrack(false);
//普通統計場景型別
MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
MobclickAgent. startWithConfigure(new MobclickAgent.UMAnalyticsConfig(this,
"你的友盟AppKey", channel, MobclickAgent.EScenarioType.E_UM_NORMAL,true));
}
好了,到這裡就全部介紹完咯。如果不明白的歡迎留言。其他詳情可以觀看github上作者的描述