androidStudio 中 gradle 常用功能

來源:互聯網
上載者:User

標籤: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 常用功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.