Android多渠道打包过程
一、准备好签名文件.keystore,(具体方法参照百度,android签名生成,或前面博文生成签名代码);
二、在AndroidMainfest.xml文件中,添加一下依赖代码:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
三、在APP中的Gradle添加多渠道打包依赖,代码如下:
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"]
}
}
}
或者修改另外一种风格:
android {
productFlavors {
kuan {}
xiaomi {}
qh360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
四、执行打包命令,两种方式打包。
第一种:
在AndroidStudio菜单栏点击Build菜单–>Generate signed APK–>选择key,并输入密码
然后下一步,选择打包渠道
最后点击完成按钮
第二种:
在android studio中的console命令框中输入:
打包命令 ./gradlew assembleRelease
-
如果系统中没有安装Gradle,则会自动下载完成安装及初始化
-
打包成功后会提示BUILD SUCCESSRUL
-
在app–>build–>outputs–>apk路径中就可以看到打包成功后的APK
注意,当我们需要生成的APK自定义APK名时,只需要在Gradle添加生成包的依赖即可,代码如下:
// 自定义输出配置,这里我们加上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)
}
}
}
最后,本人贴上自身多渠道打包生成的DEMO:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.musicdo.puluo"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
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"]
}
}
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 = "projazz${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
useLibrary 'org.apache.http.legacy'
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:palette-v7:25.0.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.zhy:okhttputils:2.6.2'
compile 'com.alibaba:fastjson:1.2.21'
compile 'com.youth.banner:banner:1.4.4'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:design:25.0.1'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'org.greenrobot:eventbus:3.0.0'
testCompile 'junit:junit:4.12'
compile(name: 'volley', ext: 'aar')
compile(name: 'navigationtabbar', ext: 'aar')
compile files('libs/xUtils-2.6.14.jar')
}
留意,签名密码写在gradle中不安全,故最好在打包上线的时候将相关代码注释掉。