Multi-channel packaging
There are too many Android app markets in China, dozens of channel packs per release. Fortunately Android Gradle provides us with productflavors, we can customize the generated APK package.
productFlavors {//多渠道打包 xiaomi { applicationId ‘com.wuxiaolong.gradle4android1‘ } googlepaly { applicationId ‘com.wuxiaolong.gradle4android2‘ } }
Custom generated APK file name
Applicationvariants.all {variant if (variant.buildType.name. Equals (' release ')) { variant.outputs.each {output-& Gt def outputFile = Output.outputfile &nbs P  IF (outputFile! = null && OUTPUTFILE.N Ame.endswith ('. apk ')) {  D EF fileName = "gradle4android_v${defaultconfig.versionname}_${releasetime ()}_${variant.flavorname}.apk" output.outputfile = new File (out Putfile.parent, FileName) } &nbs P &NBSP } } & nbsp;}
Output APK Name: gradle4android_v1.0_2016-03-23_xiaomi.apk
Placeholder
Multi-channel packaging, but also encounter a problem, such as the Friend League statistics channel number, gradle approach: Manifestplaceholders, which allows us to dynamically replace the placeholder we defined in the Androidmanifest file.
Androidmanifest.xml:
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
as follows, the ${umeng_channel_value} placeholder is replaced by Dev.
defaultConfig { manifestPlaceholders = [UMENG_CHANNEL_VALUE: ‘dev‘] }
If there are too many channels, do not write to each other, you can cycle:
productFlavors.all { flavor -> manifestPlaceholders.put("UMENG_CHANNEL_VALUE",name) }
Channel Packaging complete Code:
Android {//Omit part of code Buildtypes { release { &NBSP ; minifyenabled false//start obfuscation proguardfiles getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro ' applicationvariants.all {variant if (variant.buildType.name.equals (' release ')) { variant.outputs.each {output, &N Bsp def outputFile = output.outputfile &NBS P &NBSP;IF (outputFile! = null && outputFile.name.endsWith ('. apk ')) { &nbs P def fileName = "Gradletest_v${defaultco Nfig.versionname}_${releasetiMe ()}_${variant.flavorname}.apk " &NBSP ; output.outputfile = new File (outputfile.parent, FileName) &N Bsp &NBSP,} } & nbsp &NBSP,} &NBSP, //for many channels //productflavors.all {flavor, // m Anifestplaceholders.put ("Umeng_channel_value", name) //} &N BSP;} } productflavors {//Multichannel packaging xiaomi { applicationid ' com.wuxiaolong.gradle4android1 ' Manifestplaceholders.put ("Umeng_cHannel_value ", ' Xiaomi ') } googlepaly { applicationid ' Com.wuxiaolong.gradle4android2 ' Manifestplaceholders.put ("Umeng_channel_value", ' googlepaly ') } }//Omit part of code DEF Releasetime () { return new Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC")}
Configure signature Information Android Studio sets the default signature file
Sina Weibo SSO login, share these need to be signed packaging, to see the effect, set the default signature file for their own signature jks, so that you do not need to package the direct operation is a formal signature.
Define one or more signature information under android.signingconfigs{} and then use it in the buildtypes{} configuration.
Add your. JKs in the app directory, and the following additions are added to the app's Build.gradle file:
The first type:
android { signingConfigs { debug { storeFile file("WuXiaolong.jks") storePassword ‘android‘ keyAlias ‘android‘ keyPassword ‘android‘ } } }
Buildtypes not configured, also directly get debug, is not not configured by default is Debug?
The second type:
android { signingConfigs { release { storeFile file("WuXiaolong.jks") storePassword ‘android‘ keyAlias ‘android‘ keyPassword ‘android‘ } } buildTypes { debug { signingConfig signingConfigs.release } }}
Signature package signed by Android Studio
There's no detail here.
Signing from the command line
Configure signature information as above
android { signingConfigs { release { storeFile file("WuXiaolong.jks") storePassword ‘android‘ keyAlias ‘android‘ keyPassword ‘android‘ } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ signingConfig signingConfigs.release } }}
First "Build-clean Project" and then terminal enter the named line:
gradlew assembleRelease
Print the following information:
E:\AndroidStudioProjects\Gradle4Android>gradlew assembleRelease:app:preBuild UP-TO-DATE :app:preReleaseBuild UP-TO-DATE :app:checkReleaseManifest //省略部分 :app:packageRelease :app:zipalignRelease :app:assembleRelease BUILD SUCCESSFUL
OK, package the successful apk path such as: E:\AndroidStudioProjects\Gradle4Android\app\build\outputs\apk\app-release.apk
Gradle for Android (iii) multi-channel packaging, configuring signature information