Gradle-android Channel Packaging and renaming

Source: Internet
Author: User

Now an Android project will release products on different store platforms, and for statistical convenience, each platform will be assigned a channel ID, so we need to package the APK for different platforms based on the different IDs. No doubt Gradle is a good tool, specific gradle is what, please click here.

With the new version of Gradle, it is convenient to solve this problem, using manifestplaceholders (click here for details), but if you encounter the old version of Gradle, is not supported, so we need to find another way.

Here's a way to do it:

1 Buildscript {2 Repositories {3 mavencentral ()4     }5 Dependencies {6Classpath ' com.android.tools.build:gradle:0.6.+ '7     }8 }9 TenApply plugin: ' Android ' One  A Repositories { - mavencentral () -Maven {URL ' libraries ' } the } -  - Android { -Compilesdkversion 19 +Buildtoolsversion "19" -  + Defaultconfig { AMinsdkversion 8 atTargetsdkversion 19 -  -Versioncode 1 -Versionname "1.1.1" -PackageName "~~~~~~~" -     } in  - Signingconfigs { to Release { +StoreFile file ("~~~.keystore") -Storepassword "~ ~" theKeyalias "~~~.keystore" *Keypassword "~ ~" $         }Panax Notoginseng     } -  the productflavors { + Channel name { A signingconfig signingconfigs.release thePackageName "corresponding package name" +         } -     } $  $ Buildtypes { - Release { -Runproguardtrue theProguardfile "Proguard.cfg" -         }Wuyi     } the  - Sourcesets { Wu Main { -Manifest.srcfile ' Androidmanifest.xml ' AboutJava.srcdirs = [' src '] $Resources.srcdirs = [' src '] -Aidl.srcdirs = [' src '] -Renderscript.srcdirs = [' src '] -Res.srcdirs = [' Res '] AAssets.srcdirs = [' Assets '] +         } the     } -  $     //Replace "Channel_value" in manifest with channel number theAndroid.applicationvariants.all{Variant the variant.processmanifest.dolast{ the copy{ theFrom ("${builddir}/manifests"){ -Include "${variant.dirname}/androidmanifest.xml" in                 } theInto ("${builddir}/manifests/$variant. Name") the       About filter{ theString line-Line.replaceall ("Channel_value", "${variant.productflavors[0].name}") the                 } theVariant.processResources.manifestFile = File ("${builddir}/manifests/${variant.name}/${variant.dirname}/ Androidmanifest.xml ") +             }     -         } the         // RenamingBayi Renameoutputfile (variant) the     } the  - } -  the def releasetime () { the     return NewDate (). Format ("YyyyMMdd", Timezone.gettimezone ("UTC")) the } the  - def renameoutputfile (variant) { thedef apk =Variant.outputfile; thedef newName = ""; the 94     //get data for apk renaming thedef packagename = Variant.productflavors[0].packagename thedef channelname = Variant.productflavors[0].name thedef versionname =Android.defaultConfig.versionName98def versioncode =Android.defaultConfig.versionCode Aboutdef releasetime =releasetime () - 101     if(variant.zipalign) {102NewName = "First Name-" + Versioncode + "-" + Versionname + "-" + PackageName + "-" + ChannelName + "-smaller-" + Releasetime + "- release.apk "103}Else{104NewName = "First Name-" + Versioncode + "-" + Versionname + "-" + PackageName + "-" + ChannelName + "-smaller-" + Releasetime + "- unalign.apk " the     }106Variant.outputfile =NewFile (Apk.parentfile, newName);107 }108 109 Dependencies { the     //Compile project (': ABC ')111     //compile ' com.android.support:support-v4:13.0.+ ' the 113Compile Filetree (dir: ' Libs ', include: [' *.jar ']) the } the  the task Copynativelibs (type:copy) {117From Filetree (dir: ' Libs ', include: ' **/*.so ') into ' build/native-libs '118 }119  - Tasks.withtype (Compile) {121CompiletaskCompiletask.dependson copynativelibs122options.encoding = "UTF-8"123 }124  theTasks.withtype (com.android.build.gradle.tasks.PackageApplication) {pkgtask-126Pkgtask.jnidir file (' Build/native-libs ')127}

There will be a problem, that is, the problem of Chinese coding, if the manifest.xml contains Chinese, in all likelihood will be error. So here's a workaround:

Modify your gradle_home/bin under the Gradle execution file, Windows should be Gradle.bat, the other system is directly Gradle file, the value of default_jvm_opts is modified to "-dfile.encoding= UTF-8 ".

I wish you all Bon voyage.

Gradle-android Channel Packaging and renaming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.