Sometimes, we would like to put the APK file name on the package date, the SVN version number when packaging, the application version number and so on. Of course, these can be added manually, but the manual is also too elegant, and may be wrong.
With Gradle, we can let the packaged apk automatically bring some column information.
The default reader has a certain understanding of gradle, there are buildtypes,productflavors concept. Do not know can look at the previous article or go online search to add.
Gradle is an automated build tool based on groovy, and in build.gradle we can use some scripts, functions to control the process of compiling. The function implemented in this paper is to use Gradle to control the file name of output files after compiling.
Let's start with a simple example that adds a date to the file name.
Android {compilesdkversion22buildtoolsversion' 23.0.1 'Defaultconfig {minsdkversion11targetsdkversion22Versioncode14Versionname"1.7"//Dex exceeds the limit of 65535Multidexenabledtrue //The default is Umeng channelManifestplaceholders = [umeng_channel_value: ' Test ']}buildtypes {release {Minifyenabledtruezipalignenabledtrue //removing useless resource filesShrinkresourcestrueproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro 'Applicationvariants.all {Variant-Variant.outputs.each {Output-def OutputFile=Output.outputfiledef FileName= "Myapp_v${defaultconfig.versionname}_${releasetime ()}.apk"Output.outputfile=NewFile (outputfile.parent, FileName)} } }}}
Def releasetime () {
return new Date (). Format ("Yyyy-mm-dd", TimeZone. getTimeZone ("UTC"))
}
The focus is on buildtypes->release->applicationvariants .
def fileName = "Myapp_v${defaultconfig.versionname}_${releasetime ()}.apk"
In this sentence, Defaultconfig is a set of parameters above, the Releasetime () function is defined at the bottom of the function to get the current time. In this format, the output file name should be: myapp_v1.7_2015-22-22.apk
After writing this, execute:
./gradlew Assemble_release
You can output the APK with the specified file name format.
Through the above steps, we can realize the flexibility of gradle.
Here is the key to this article, add the SVN version number to your APK name. The benefits of doing this test can be better positioned when the bug, etc., is quite useful. Just do not know why Baidu does not retrieve a similar article, to Google to find some information. Also do not know is because the domestic people do not love to share it, or Baidu too food, haha.
The addition of the SVN version number and the addition time principle of the above basically the same, is to introduce a third-party library, this library can get SVN information.
First, add Svnkit this dependency in the dependencies in Projece Build.gralde:
Dependencies { ' com.android.tools.build:gradle:1.2.3 ' org.tmatesoft.svnkit ', Name: ' Svnkit ', version: ' 1.8.11 ' }
We're using this library to get SVN's information at compile time.
and add it at the top of the module's Build.gradle
Import org.tmatesoft.svn.core.wc.*
In this way, the Svnkit library was introduced.
Add a method to get the SVN version number, similar to how you get the time.
def getsvnrevision () { = svnwcutil.createdefaultoptions (true); = svnclientmanager.newinstance (options); = clientmanager.getstatusclient (); false ); = status.getcommittedrevision (); return revision.getnumber ();}
Here is the use of Svnkit are some of the methods, interested can learn more about themselves.
The overall build file is as follows:
// Project Build.gradle Buildscript { repositories { jcenter () } dependencies { ' com.android.tools.build: gradle:1.2.3 ' Org.tmatesoft.svnkit ', Name: ' Svnkit ', version: ' 1.8.11 ' }}allprojects { repositories { jcenter () }}
//Module Build.gradleImportOrg.tmatesoft.svn.core.wc.*Apply plugin:' Com.android.application 'def releasetime () {return NewDate (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC") )}def getsvnrevision () {isvnoptions options= Svnwcutil.createdefaultoptions (true); Svnclientmanager Clientmanager=svnclientmanager.newinstance (options); Svnstatusclient statusclient=clientmanager.getstatusclient (); Svnstatus Status= Statusclient.dostatus (ProjectDir,false); Svnrevision Revision=status.getcommittedrevision (); returnrevision.getnumber ();} Android {compilesdkversion22buildtoolsversion' 23.0.1 'Defaultconfig {minsdkversion11targetsdkversion22//sign in to sign up for comments.Versioncode 14Versionname"1.7"//Dex exceeds the limit of 65535Multidexenabledtrue //The default is Umeng channelManifestplaceholders = [umeng_channel_value: ' Test '] }buildtypes {release {Minifyenabledtruezipalignenabledtrue //removing useless resource filesShrinkresourcestrueproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro 'Applicationvariants.all {Variant-Variant.outputs.each {Output-def OutputFile=output.outputfile//Modify file name heredef fileName = "Myapp_v${defaultconfig.versionname}_${releasetime ()}_${getsvnrevision ()}.apk" Output.outputfile=NewFile (outputfile.parent, FileName) } } } Productflavors {xiaomi {manifestplaceholders= [Umeng_channel_value: "Xiaomi"]} yingyongbao {manifestplaceholders= [Umeng_channel_value: "Yingyongbao"]}}}dependencies {compile filetree (include: [' *.jar '], dir: ' Libs ') Compile' Com.umeng.analytics:analytics:latest.integration 'Compile' com.android.support:appcompat-v7:22.2.0 '}
Final execution:
./gradlew Assemble_release
In this way, you can package the name format as: myapp_v1.7_20xx-xx-xx_1234.apk apk file
Android gradle Practical Tips--apk file name plus SVN version number, date, etc.