有時候,我們會希望能把APK文件名上帶上打包日期,打包時svn的版本號,應用版本號等。當然這些也可以手動添加,但是手動的話也未免太不優雅了,而且可能會出錯。
利用Gradle,我們可以讓打包出來的apk自動的帶上一些列信息。
默認讀者已經對gradle有一定的了解,有buildtypes,productFlavors的概念。不了解的可以看看上一篇或者去網上搜索來補充一下。
Gradle是基於groovy的自動化構建工具,在build.gradle中我們可以用一些腳本,函數來控制編譯的過程。本文所實現的功能,就是用gradle來控制編譯完成后輸出文件的文件名來實現的。
首先來個簡單的例子,文件名上加上日期。
android { compileSdkVersion 22 buildToolsVersion '23.0.1' defaultConfig { minSdkVersion 11 targetSdkVersion 22 versionCode 14 versionName "1.7" // dex突破65535的限制 multiDexEnabled true // 默認是umeng的渠道 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "test"] } buildTypes { release { minifyEnabled true zipAlignEnabled true // 移除無用的resource文件 shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } } }
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
重點在buildTypes->release->applicationVariants里面的
def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}.apk"
這句話,defaultConfig是在上面設置的一些參數,releaseTime()函數是在最下面定義的獲取當前時間的函數。按照這個格式,輸出的文件名應該是:myapp_v1.7_2015-22-22.apk
寫完這個后,執行:
./gradlew assemble_Release
就可以輸出指定文件名格式的APK了。
通過以上步驟,我們可以體會到gradle的靈活性。
下面就是這篇文章的重點了,在你的apk名字中加上svn版本號。這樣做的好處的測試的時候可以更好的定位bug等,還算是蠻有用的。只是不知道為什么百度根本檢索不到類似的文章,去google才找到一些資料。也不知道是因為國內的人不愛分享呢,還是百度太菜呢,哈哈。
加SVN版本號和上面的加入時間原理基本相同,就是要引入一個第三方的庫,這個庫可以獲取svn的信息。
首先在projece 的build.gralde中的dependencies中添加svnkit這個依賴:
dependencies { classpath 'com.android.tools.build:gradle:1.2.3' classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11' }
我們就是利用這個庫來在編譯的時候獲取svn的信息的。
然后在module的build.gradle最上方添加
import org.tmatesoft.svn.core.wc.*
這樣就把svnkit這個庫引入過來了。
再添加一個獲取svn版本號的方法,類似一獲取時間的方法。
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(); return revision.getNumber(); }
這里面用到的都是svnkit的一些方法了,有興趣的可以自己多了解一下。
整體build文件如下:
// project build.gradle buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11' } } allprojects { repositories { jcenter() } }
//module build.gradle import org.tmatesoft.svn.core.wc.* apply plugin: 'com.android.application' def releaseTime() { return new Date().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(); return revision.getNumber(); } android { compileSdkVersion 22 buildToolsVersion '23.0.1' defaultConfig { minSdkVersion 11 targetSdkVersion 22 //登錄注冊評論點贊 versionCode 14 versionName "1.7" // dex突破65535的限制 multiDexEnabled true // 默認是umeng的渠道 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "test"] } buildTypes { release { minifyEnabled true zipAlignEnabled true // 移除無用的resource文件 shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile //這里修改文件名 def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}_${getSvnRevision()}.apk" output.outputFile = new File(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' }
最后執行:
./gradlew assembleRelease
這樣,就可以打包出名字格式為:myapp_v1.7_20xx-xx-xx_1234.apk的APK文件了