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'
}