有时候,咱们会但愿能把APK文件名上带上打包日期,打包时svn的版本号,应用版本号等。固然这些也能够手动添加,可是手动的话也未免太不优雅了,并且可能会出错。android
利用Gradle,咱们可让打包出来的apk自动的带上一些列信息。app
默认读者已经对gradle有必定的了解,有buildtypes,productFlavors的概念。不了解的能够看看上一篇或者去网上搜索来补充一下。svn
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.apkgradle
写完这个后,执行:ui
./gradlew assemble_Release
就能够输出指定文件名格式的APK了。google
经过以上步骤,咱们能够体会到gradle的灵活性。spa
下面就是这篇文章的重点了,在你的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文件了