AndroidStudio多渠道的打包方法,今天整理一下之前積累的打包套路,寫一篇文章,手把手的教給大家。
說到多渠道,這裏不得不提一下友盟統計,友盟統計是大家日常開發中常用的渠道統計工具,而我們的打包方法就是基於友盟統計實施的。按照友盟官方文檔說明,渠道信息通常需要在AndroidManifest.xml中配置如下值:
<meta-data android:value="Channel ID" android:name="UMENG_CHANNEL"/>
- 1
- 1
上面的value值Channel_ID就是渠道標識。我們的期望的就是在編譯時候這個值能夠自動變化以滿足區分多渠道的需求。
(一)在AndroidManifest.xml裏設置動態渠道變量
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
- 1
- 2
- 3
- 1
- 2
- 3
(二)在build.gradle設置productFlavors
這裏假定我們需要打包的渠道爲酷安市場、360、小米、百度、豌豆莢
android {
productFlavors {
kuan {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "kuan"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qh360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
或者批量修改
android {
productFlavors {
kuan {}
xiaomi {}
qh360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
所謂ProductFlavors其實就是可定義的產品特性,配合 manifest merger 使用的時候就可以達成在一次編譯過程中產生多個具有自己特性配置的版本。上面這個配置的作用就是,爲每個渠道包產生不同的 UMENG_CHANNEL_VALUE 的值。
(三)執行打包操作
在AndroidStudio菜單欄點擊Build菜單–>Generate signed APK–>選擇key,並輸入密碼
然後下一步,選擇打包渠道
最後點擊完成按鈕
上圖可以看到,我們已經成功的將五個渠道包打好。
(四)執行打包命令 ./gradlew assembleRelease
除了使用AndroidStudio圖形打包操作以外,我們也可以使用命令行進行打包操作,具體步驟如下:
注意,此時這裏的APK包名顯示爲unsigned,也就是說未簽名,我們可以繼續在build.gradle文件中配置簽名信息
signingConfigs {
release{
storeFile file("../wooyun_keystore") //簽名文件路徑
storePassword "123456"
keyAlias "123456"
keyPassword "123456" //簽名密碼
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
這次生成的就是含有簽名的渠道包。
// 自定義輸出配置,這裏我們加上APK版本號1.0
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲wooyun_v1.0_wandoujia.apk
def fileName = "wooyun_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
如果我們想打包wandoujia渠道的release版本,執行如下命令就好了:
gradlew assembleWandoujiaRelease
- 1
- 1
如果我們想打包wandoujia渠道的debug版本,執行如下命令就好了:
gradlew assembleWandoujiaDebug
- 1
- 1
如果我們只打wandoujia渠道版本,則:
gradlew assembleWandoujia
- 1
- 1
此命令會生成wandoujia渠道的Release和Debug版本
同理我想打全部Release版本:
gradlew assembleRelease
- 1
- 1
這條命令會把Product Flavor下的所有渠道的Release版本都打出來。
下面是我個人的build.gradle配置文件,分享給大家,以作參考
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.wooyun.castiel"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
//簽名
signingConfigs {
debugConfig {
storeFile file("../wooyun_keystore") //簽名文件
storePassword "123456"
keyAlias "123456"
keyPassword "123456" //簽名密碼
}
release{
storeFile file("../wooyun_keystore") //簽名文件
storePassword "123456"
keyAlias "123456"
keyPassword "123456" //簽名密碼
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// 自定義輸出配置
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲wooyun_v1.0_wandoujia.apk
def fileName = "wooyun_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
productFlavors {
kuan {}
xiaomi {}
qh360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
上面的signingConfigs配置中,可以寫兩個代碼塊,分別名爲debugConfig和releaseConfig,並在其中寫好一個完整簽名需要的keyAlias、keyPassword、storeFile file、storePassword。
然後在buildTypes中,分兩個代碼塊,分別是debug時用的,和release時用的。在其中引用剛剛寫好的debugConfig和releaseConfig即可。
注意:signingConfigs代碼塊一定要寫在buildTypes前面,否則會報下面這種錯:
Could not find property 'debugConfig' on SigningConfig container.
- 1
- 1
簽名密碼寫在gradle中不安全,故最好在打包上線的時候將相關代碼註釋掉。
參考鏈接:http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/