转发请注明出处:
安卓猴的博客(http://sunjiajia.com)


前言

国内Android应用市场品种太多,血统不纯,每次上线App都打包打到手软,上传上到吐血!好在我们有了Android studio和gradle,一条命令打包各种渠道版本App!

正文

Umeng多渠道

我们在项目中会使用统计SDK,用的最流行的当属umeng了吧。这里我们就用友盟来说明。
用了友盟的App的清单文件AndroidManifest.xml中都会写上meta-data,来区分不同的渠道:

1
2
3
<meta-data
android:name="UMENG_CHANNEL"
android:value="Channel_ID" />

1.将Channel_ID更改为${UMENG_CHANNEL_VALUE},如下:

1
2
3
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />

2.在appbuild.gradle设置produceFlavors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qihoo {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihoo"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
qq {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq"]
}
}

3.来看看我在项目中使用的build.gradle文件内容:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
// 这里的applicationId根据自己的替换
applicationId "com.sunjiajia.xxxxx"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "0.0.1"
multiDexEnabled true //dex突破65535限制
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
lintOptions {
disable 'MissingTranslation', 'ExtraTranslation'
abortOnError false
}
signingConfigs {
// /home/monkey/Projects/AndroidStudioProjects/debug.jks是签名文件所在全路径
// debug时打包用的签名
debug {
keyAlias 'debug'
keyPassword '123456'
storeFile file('/home/monkey/Projects/AndroidStudioProjects/debug.jks')
storePassword '123456'
}
// release时打包用的签名
release {
keyAlias 'monkey'
keyPassword '123123'
storeFile file('/home/monkey/Projects/AndroidStudioProjects/monkey.jks')
storePassword '123123'
}
}
buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为appname_v1.0_2015-09-15_09-57-58_wandoujia.apk
// 下面的appname改为你自己的app name
def fileName = "appname_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
// 友盟多渠道打包
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qihoo {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihoo"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
qq {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq"]
}
}
}
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}

命令创建task

1.进入项目的根目录:

1
2
// 打包全部Debug版本
./gradlew assembleDebug
1
2
// 打包全部Release版本
./gradlew assembleRelease

2.assembleProduct Flavor 结合创建指定渠道的任务版本。例如:

1
2
// 打包wandoujia渠道的Release版本和Debug版本
./gradlew assembleWandoujia
1
2
// 打包wandoujia渠道的Release版本
./gradlew assembleWandoujiaRelease

结语

大家有什么问题,在下面留言就可以了,也可以关注我的微博@安卓猴,交流更及时。

关于我

  • 微信公众号:Android奇想录(android_amazing)
扫描二维码关注公众号
扫描二维码关注公众号