用android studio多渠道打包
1. 官方教程
https://developer.android.com/studio/build/build-variants.html
2. 设置Build Types参数
打开 Project Sturcture --> Build Variatns --> Build Types
设置参数如:zipalign = true
3. 创建 dimension
进入第2个选项卡: Flavors 创建 dimension
4. 添加渠道,设置渠道参数
点 + 按钮 ---> 点 Add Product Flavor -- > 输入渠道名字 --> 设置渠道参数,如ProGuard Files.
5. 添加其它渠道
如上.添加完后,如下:
6. 设置生成apk的名字
module 的 build.gradle 文件
1 apply plugin: 'com.android.application' 2 apply plugin: 'org.greenrobot.greendao' // apply plugin 3 apply plugin: 'com.jakewharton.butterknife' 4 5 def buildTime(){ 6 return new Date().format("yyMMdd", TimeZone.getTimeZone("UTC")) 7 } 8 9 android { 10 compileSdkVersion 28 11 buildToolsVersion '28.0.3' 12 defaultConfig { 13 applicationId "cn.com.example.app" 14 minSdkVersion 15 15 targetSdkVersion 26 16 versionCode 5 17 versionName "v3.1.2.180103" 18 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 } 20 buildTypes { 21 release { 22 minifyEnabled false 23 shrinkResources false 24 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 zipAlignEnabled = true 26 applicationVariants.all { variant -> 27 variant.outputs.all { output -> 28 outputFileName = new File( 29 "${applicationId}_${variant.productFlavors[0].name}_${versionName}_${buildTime()}.apk".toLowerCase()) 30 } 31 } 32 } 33 } 34 sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-zh'] } } 35 compileOptions { 36 sourceCompatibility = '1.8' 37 targetCompatibility = '1.8' 38 } 39 flavorDimensions 'channels' 40 productFlavors { 41 myapp { 42 dimension = 'channels' 43 minSdkVersion 22 44 proguardFiles 'proguard-rules.pro' 45 } 46 huawei { 47 dimension = 'channels' 48 minSdkVersion 22 49 proguardFiles 'proguard-rules.pro' 50 } 51 xiaomi { 52 dimension = 'channels' 53 minSdkVersion 22 54 } 55 } 56 } 57 dependencies { 58 implementation fileTree(include: ['*.jar'], dir: 'libs') 59 androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 60 exclude group: 'com.android.support', module: 'support-annotations' 61 }) 62 // add library 63 //... 64 65 implementation 'com.github.bumptech.glide:glide:4.9.0' 66 annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' 67 }
- buildTime()是个自定义函数,返回时间.
- "${applicationId}_${variant.productFlavors[0].name}_${versionName}_${buildTime()}.apk".toLowerCase() 包名_渠道名_版本名_时间.apk