標籤:android blog http io ar os 使用 sp 檔案
1. gradle 使用 svn 目前的版本資訊.
def getSvnRevision() { new ByteArrayOutputStream().withStream { os -> def result = exec { executable = ‘svn‘ args = [‘info‘] standardOutput = os } def outputAsString = os.toString() def matchLastChangedRev = outputAsString =~ /Last Changed Rev: (\d+)/ ext.svnRev = "${matchLastChangedRev[0][1]}".toInteger() } return svnRev}
使用例子:
versionCode 1versionName "0.${versionCode}." + getSvnRevision()
使用 git checkout 的 6位短版本資訊.
task gitReversion { def cmd = "git rev-parse --short HEAD"
// git rev-list --all | wc -l 擷取提交次 def proc = cmd.execute() ext.revision = proc.text.trim()}
使用例子:
versionCode 1versionName "0.${versionCode}." + gitReversion.revision
gradle 拷貝檔案:
task copyTaskWithPatterns(type: Copy) { from "${buildDir}/outputs/apk/" into "c:/apks/"
// 不拷貝未簽名的檔案. exclude { details -> details.file.name.endsWith(‘-unaligned.apk‘) || details.file.name .endsWith(‘-unsigned.apk‘) } include "**/*.apk" println "apk copied. ${buildDir}"}build.doLast { tasks.copyTaskWithPatterns.execute()}
其中注意的是 如果偷懶寫法的話, exclude 在include之前.
如下的 build 檔案指定輸出的檔案名稱.
buildTypes { release { runProguard true proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ signingConfig signingConfigs.release } debug { runProguard false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ debuggable true jniDebugBuild true } applicationVariants.all { variant -> def file = variant.outputFile if(variant.buildType.name.equals("release")){ // 判斷編譯的類型 variant.outputFile = new File( (String) file.parent, (String) (project.archivesBaseName + "-" + variant.mergedFlavor.versionName + ".apk") ) }else{ variant.outputFile = new File( (String) file.parent, (String) (project.archivesBaseName + "-" + variant.mergedFlavor.versionName + ".apk") ) } } }
另外
variant.baseName = {moduleName}-debug,
project.archivesBaseName ={projectName}
variant.name={moduleName}Debug
關於 android-studio中 gradle 的使用方式. 參見: http://tools.android.com/tech-docs/new-build-system/user-guide
androidStudio 中 gradle 常用功能