当前位置:网站首页>Gradle multi-channel packaging

Gradle multi-channel packaging

2021-10-22 10:32:27 Bird's nest

original text : Android Studio Series six --Gradle Multi channel packaging ,

Meituan adopted another hack The way to quickly package for multiple channels : Meituan Android The journey of automation — Generate channel package , stay META-INF Add different empty files under the folder .

Because at home Android There are many channels in the market , In order to count the downloads and other data of each channel , We need to package separately for each channel , If you are asked to pack dozens of markets, you will be bored to death , But there is. Gradle, This is no longer a problem .

Alliance of friends package through multiple channels

I don't say much nonsense , Take Youmeng statistics as an example , stay AndroidManifest.xml There will be such a paragraph :

       
       
       
1
2
3
       
       
       
<meta-data
android:name= "UMENG_CHANNEL"
android:value= "Channel_ID" />

Inside Channel_ID It's the channel sign . Our goal is to automatically change this value when compiling .

  • First step stay AndroidManifest.xml Internal configuration PlaceHolder
       
       
       
1
2
3
       
       
       
<meta-data
android:name= "UMENG_CHANNEL"
android:value= "${UMENG_CHANNEL_VALUE}" />
  • The second step stay build.gradle Set up productFlavors
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
       
       
       
android {
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
_360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}
}

Or batch modify

       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
android {
productFlavors {
xiaomi {}
_360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [ UMENG_CHANNEL_VALUE : name]
}
}

It's simple and clear. Do you have ? Direct execution ./gradlew assembleRelease , Then you can have a cup of coffee quietly and wait for the packing to be completed .

assemble combination Build Variants To create task

The last blog introduced assemble This command , Will combine Build Type Create your own task, Such as :

  • ./gradlew assembleDebug*
    • ./gradlew assembleRelease*

besides assemble And Product Flavor Combine to create new tasks , Actually assemble Is and Build Variants Used together , and Build Variants = Build Type + Product Flavor , For example, you can see :

If we want to pack wandoujia Channel release edition , Just execute the following command :

  • ./gradlew assembleWandoujiaRelease*

If we only play wandoujia Channel version , be :

  • ./gradlew assembleWandoujia*

This command generates wandoujia Channel Release and Debug edition

Similarly, I want to play all Release edition :

  • ./gradlew assembleRelease*

This order will Product Flavor All channels under the Release All versions are typed .

All in all ,assemble Command to create task It can be used as follows :

  • assemble: Allows you to build a directly Variant edition , for example assembleFlavor1Debug.
  • assemble: Allows you to build specified Build Type All of the APK, for example assembleDebug Will build Flavor1Debug and Flavor2Debug Two Variant edition .
  • assemble: Allows you to build specified flavor All of the APK, for example assembleFlavor1 Will build Flavor1Debug and Flavor1Release Two Variant edition .

complete gradle Script

Finally, the welfare was released , A complete... I used in the project gradle File configuration :

       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
       
       
       
apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format( "yyyy-MM-dd", TimeZone.getTimeZone( "UTC"))
}
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "com.boohee.*"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
// dex breakthrough 65535 The limitation of
multiDexEnabled true
// The default is umeng Channel of
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
lintOptions {
abortOnError false
}
signingConfigs {
debug {
// No debug config
}
release {
storeFile file( "../yourapp.keystore")
storePassword "your password"
keyAlias "your alias"
keyPassword "your password"
}
}
buildTypes {
debug {
// Show Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// No display Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true
// Remove useless resource file
shrinkResources true
proguardFiles getDefaultProguardFile( 'proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs. each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith( '.apk')) {
// Output apk The name is boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
// Alliance of friends package through multiple channels
productFlavors {
wandoujia {}
_360 {}
baidu {}
xiaomi {}
tencent {}
taobao {}
...
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: [ '*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
...
}

You have questions or questions 、 It is recommended to welcome blog comments ,Android Studio That's the end of my tutorial , I believe everyone can basically use , There are other skills and operations that we have to explore by ourselves , After that, I will sort out some on my blog Tips And so on. , Welcome to pay attention .

版权声明
本文为[Bird's nest]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/10/20211009000611474d.html

随机推荐