1. 官方教程

  https://developer.android.com/studio/build/build-variants.html

2. 设置Build Types参数

打开 Project Sturcture --> Build Variatns --> Build Types

设置参数如:zipalign = true

用android studio多渠道打包-LMLPHP

3. 创建 dimension

进入第2个选项卡: Flavors 创建 dimension

用android studio多渠道打包-LMLPHP

4. 添加渠道,设置渠道参数

点 + 按钮 ---> 点 Add Product Flavor -- > 输入渠道名字 --> 设置渠道参数,如ProGuard Files.

用android studio多渠道打包-LMLPHP

5. 添加其它渠道

如上.添加完后,如下:

用android studio多渠道打包-LMLPHP

6. 设置生成apk的名字

module 的 build.gradle 文件

 apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
apply plugin: 'com.jakewharton.butterknife' def buildTime(){
return new Date().format("yyMMdd", TimeZone.getTimeZone("UTC"))
} android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "cn.com.example.app"
minSdkVersion 15
targetSdkVersion 26
versionCode 5
versionName "v3.1.2.180103"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled = true
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File(
"${applicationId}_${variant.productFlavors[0].name}_${versionName}_${buildTime()}.apk".toLowerCase())
}
}
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-zh'] } }
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
flavorDimensions 'channels'
productFlavors {
myapp {
dimension = 'channels'
minSdkVersion 22
proguardFiles 'proguard-rules.pro'
}
huawei {
dimension = 'channels'
minSdkVersion 22
proguardFiles 'proguard-rules.pro'
}
xiaomi {
dimension = 'channels'
minSdkVersion 22
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
// add library
//... implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
  • buildTime()是个自定义函数,返回时间.
  • "${applicationId}_${variant.productFlavors[0].name}_${versionName}_${buildTime()}.apk".toLowerCase() 包名_渠道名_版本名_时间.apk
05-11 11:32