架構日記(六)新建Gradle Task實現打包後上傳到蒲公英

前面的文章中提到了腳本自動執行打包和安裝,其實將腳本重組也是可以做到一鍵發佈的,只是每一篇文章都有自己需要解決的問題,所以沒有完全串起來實現一鍵打包發佈。

首先下面貼出蒲公英的官網地址

https://www.pgyer.com

蒲公英開放api地址

https://www.pgyer.com/doc/view/api

我們需要用到的蒲公英上傳的post請求的地址

https://www.pgyer.com/apiv2/app/upload

剩下的細節可以自己去官網查看,接下來看代碼吧

粘貼一些常用命令方便查看,服務到位吧~~~

//構建
gradlew app:clean    //移除所有的編譯輸出文件,比如apk

gradlew app:build   //構建 app module ,構建任務,相當於同時執行了check任務和assemble任務

//檢測
gradlew app:check   //執行lint檢測編譯。

//打包
gradlew app:assemble //可以編譯出release包和debug包,可以使用gradlew assembleRelease或者gradlew assembleDebug來單獨編譯一種包

gradlew app:assembleRelease  //app module 打 release 包

gradlew app:assembleDebug  //app module 打 debug 包

//安裝,卸載

gradlew app:installDebug  //安裝 app 的 debug 包到手機上

gradlew app:uninstallDebug  //卸載手機上 app 的 debug 包

gradlew app:uninstallRelease  //卸載手機上 app 的 release 包

gradlew app:uninstallAll  //卸載手機上所有 app 的包

 多渠道的包加上渠道名也可以執行命令,尤其是安裝,下載,和打包,以豌豆莢爲例(Mac)

./gradlew app:installWandoujiaDebug  or  Release

./gradlew app:uninstallWandoujiaDebug  or Release

./gradlew app:assembleWandoujiaDebug  or Release

新建文件 uploadApk.gradle

存放在setting.gradle同級目錄下

import groovy.json.JsonSlurper

//自動上傳到蒲公英
def uploadApk(){
    //查找等待上傳的apk文件
    //正常打包可以在debug和relase下找到,但是多渠道打包,需要選擇正確的渠道路徑
    def apkDir = new File("build/outputs/apk/wandoujia/debug")

    if(!apkDir.exists()){
        throw new RuntimeException("apk outputs path not exists!")
    }

    def apk = null
    for(int i = apkDir.listFiles().length - 1 ; i >= 0 ; i--){
        File file = apkDir.listFiles()[i]
        if(file.name.endsWith(".apk")){
            apk = file
            break
        }
    }

    if(apk == null){
        throw new RuntimeException("apk file not exists!")
    }

    println "*********************************** start upload file ******************************************"

    def twoHyphens = "--"
    def boundary = "*********"
    def end = "\r\n"

    //蒲公英API上傳接口   https://www.pgyer.com/doc/view/api#uploadApp  官方文檔地址
    def conn = new URL("https://www.pgyer.com/apiv2/app/upload").openConnection()
    conn.setRequestMethod("POST")
    conn.setRequestProperty("Connection", "Keep-Alive")
    conn.setRequestProperty("Charset", "UTF-8")
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary)
    conn.setDoInput(true)
    conn.setDoOutput(true)

    /**
     * 蒲公英爲測試賬號
     * xxxxxxxxx
     */

    //添加參數:_api_key
    def sb = new StringBuilder()
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=_api_key")
    sb.append(end).append(end)
    sb.append("寫自己的").append(end)

    //添加參數:buildUpdateDescription 更新日誌,取值git
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=buildUpdateDescription")
    sb.append(end).append(end)
    sb.append(getGitVersionInfo()).append(end)

    //添加參數:buildInstallType 設置密碼安裝
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=buildInstallType")
    sb.append(end).append(end)
    sb.append(2).append(end)

    //添加參數:buildPassword 設置密碼  cfss1234  fgbp1234
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=buildPassword")
    sb.append(end).append(end)
    sb.append("自己的提取密碼").append(end)

    //添加參數file: 需要上傳的apk文件
    sb.append(twoHyphens).append(boundary).append(end)
    sb.append("Content-Disposition: form-data; name=file;filename=").append(apk.getName())
    sb.append(end).append(end)

    def dos = new DataOutputStream(conn.getOutputStream())
    dos.writeBytes(sb.toString())
    dos.flush()
    sb.delete(0, sb.length())

    def fis = new FileInputStream(apk)
    byte[] bf = new byte[8192]
    int len
    while ((len = fis.read(bf)) != -1) {
        dos.write(bf, 0, len)
    }
    sb.append(end)
    sb.append(twoHyphens).append(boundary).append(end)
    dos.writeBytes(sb.toString())

    dos.flush()
    fis.close()
    dos.close()
    conn.connect()

    def text = conn.getContent().text
    def resp = new JsonSlurper().parseText(text)

    println text
    println "*************** upload finish ***************"

    if (resp.code != 0) {
        throw new RuntimeException(resp.message)
    }

    //瀏覽器中打開短連接
    def url = "https://www.pgyer.com/" + resp.data.buildShortcutUrl
    print("上傳成功,應用鏈接:" + url)

}

def getGitVersionInfo() {
    return 'git log -n 1'.execute().text.trim()
}

//打包測試環境apk 上傳蒲公英 發送郵件功能使用蒲公英自帶的郵件功能
task uploadApk(group: "upload") {
    dependsOn("assembleDebug")

    doLast {
        uploadApk()
    }
}

 雙擊uploadApk任務,由於依賴了assembleDebug任務,所以會先執行編譯打包的任務。wandoujia的路徑,見上一篇多渠道打包。具體的地址根據自己的工程結構來構建

Android Gradle的總結,強烈推薦這篇文章

https://blog.csdn.net/zhaoyanjun6/article/details/77678577

最終效果

網頁的地址再AS的Run輸出中打印了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章