1.在Manifest.xml文件中添加权限和AppKey
权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
AppKey和渠道变量:
<meta-data android:value="59ba89cff43e48471f0000eb" android:name="UMENG_APPKEY"/>
//设置动态渠道变量
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
2.在build.gradle中设置productFlavors
为不同的渠道市场进行设置:
android {
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}
//通过gradle脚本语法统一设置不同的渠道
android {
productFlavors {
xiaomi {}
baidu {}
wandoujia {}
}
productFlavors.all{
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
3.对应用进行打包操作
在AndroidStudio菜单栏点击Build菜单–>Generate signed APK
4.通过studio命令行工具,进行打包
命令:gradlew assembleRelease 开始打包
打包成功
查看apk
至此,多渠道打包就已经完成!!
4.签名
//添加签名文件配置
signingConfigs{
debug{}
//为release包添加签名文件配置
release{
storeFile file("Multi.jks")
storePassword "5258168699"
keyAlias "Multi"
keyPassword "5258168699"
}
}
注意:signingConfigs代码块一定要写在buildTypes前面,否则会报错:
Could not find property 'debugConfig' on SigningConfig container.
5.自己的多渠道打包
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "xiaoming.com.multi"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
//默认设置友盟
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
//添加签名文件配置
signingConfigs{
debug{}
//为release包添加签名文件配置
release{
storeFile file("Multi.jks")
storePassword "5258168699"
keyAlias "Multi"
keyPassword "5258168699"
}
}
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 = "${variant.productFlavors[0].name}_app_v${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
productFlavors{
// xiaomi{
//// manifestPlaceholders = [UMENG_CHANNEL_VALUE : "xiaomi"]
// //对不同的渠道进行值替换
// resValue("string","app_name","xiaomi")
// }
// wandoujia{
//// manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
// resValue("string","app_name","wandoujia")
// }
// baidu{
//// manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
// resValue("string","app_name","baidu")
// }
//为不同功能的apk进行打包测试,可以在同一手机上安装同一应用进行不同功能的测试
okhttp{
applicationIdSuffix "okhttp"
resValue "string","app_name","okHttp"
}
jpush{
applicationIdSuffix "jpush"
resValue "string","app_name","jpush"
}
}
//通过gradle脚本语法统一设置不同的渠道
productFlavors.all{
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
//添加友盟应用统计支持
compile 'com.umeng.analytics:analytics:latest.integration'
}