::快速編譯打包apk腳本
echo "$$package_begin$$"
sleep 1
::執(zhí)行打包命令前,需要先定位到項目根目錄
::echo %pa%
::rd .idea
::先去Applibs 去切換分支拉取代碼
cd ../..
cd AppLibs
git checkout master
git pull
::回到主分支 然后拉取代碼
cd ..
cd AndroidLeader
git checkout master
git pull
::開始打包
::gradlew build
::執(zhí)行打包命令
gradle uploadApk
echo -e "$$package success$$"
::桌面右上角彈出通知
notify-send build.sh "package down!"
pause
import groovy.json.JsonOutput
def uploadApk() {
def apkFile = getApkPath()
def twoHyphens = "--"
def boundary = "*********"
def end = "\r\n"
//模擬表單上傳 multipart/form-data
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)
//可以允許若干 控件域 同時上傳其值,每個域值使用 boundary 分割
conn.setDoInput(true)
conn.setDoOutput(true)
println "*************** start upload file ***************"
// 添加參數(shù) 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("5e58ea99d93b16451f83793ab74db8fa").append(end)
//添加 app_key
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=app_key")
sb.append(end).append(end)
sb.append("470e351df0e203f2451bde1f73125757").append(end)
//添加參數(shù):buildInstallType 設(shè)置密碼安裝
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)
//添加參數(shù):buildPassword 設(shè)置密碼
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=buildPassword")
sb.append(end).append(end)
sb.append("120").append(end)
//添加參數(shù)file: 需要上傳的apk文件
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=file;filename=").append(apkFile)
sb.append(end).append(end)
println "===========參數(shù)============" + sb.toString()
def dos = new DataOutputStream(conn.getOutputStream())
dos.writeBytes(sb.toString())
dos.flush()
sb.delete(0, sb.length())
def fis = new FileInputStream(apkFile)
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 groovy.json.JsonSlurper().parseText(text)
println resp
println "*************** upload finish ***************"
//瀏覽器中打開短連接
println("測試" + resp.data)
def url = "https://www.pgyer.com/" + resp.data.buildShortcutUrl
sendMsgToDing(resp.data)
println("上傳成功,應(yīng)用鏈接:" + url)
}
/**
* 發(fā)送到釘釘
*/
class ContentModel {
String text
String title
String picUrl
String messageUrl
}
def sendMsgToDing(def data) {
def conn = new URL("https://oapi.dingtalk.com/robot/send?access_token=34e7c8a2bd608cb6d86245ab67de1077b65f90d411a671905a335de0406398d1").openConnection()
conn.setRequestMethod('POST')
conn.setRequestProperty("Connection", "Keep-Alive")
conn.setRequestProperty("Content-type", "application/json;charset=UTF-8")
conn.setConnectTimeout(30000)
conn.setReadTimeout(30000)
conn.setDoInput(true)
conn.setDoOutput(true)
def dos = new DataOutputStream(conn.getOutputStream())
HashMap<String, Object> map = new HashMap<>()
map.put("msgtype", "link")
ContentModel contentModel = new ContentModel()
contentModel.text = "App已經(jīng)上傳至蒲公英, 可以下載使用了${getUpdateContent()}" + data.buildCreated
contentModel.title = "${getAppName()}App${data.buildVersion}上傳提醒"
contentModel.picUrl = data.buildQRCodeURL
contentModel.messageUrl = "https://www.pgyer.com/" + data.buildShortcutUrl
map.put("link", contentModel)
def JSON = new JsonOutput().toJson(map)
println(JSON)
dos.writeBytes(JSON)
def input = new BufferedReader(new InputStreamReader(conn.getInputStream()))
String line = ""
String result = ""
while ((line = input.readLine()) != null) {
result += line
}
dos.flush()
dos.close()
input.close()
conn.connect()
println("查看發(fā)送釘釘結(jié)果" + result)
}
def getApkPath() {
String buildType = getBuildType().toLowerCase()
def file = new File(buildDir, "/outputs/apk/${getAreaName()}/${buildType}/app_${getApplicationId()}-${getVersionName()}-${releaseTime()}.apk")
println('絕對路徑' + file.absolutePath)
return file.absolutePath
}
/**
*
* @return
*/
def getApplicationId() {
return project.property('android').properties['defaultConfig'].properties['applicationId']
}
def getVersionName() {
return project.property('VERSION_NAME')
}
/**
* 獲取地區(qū)名字
*/
def getAreaName() {
return 'ZhengZhou'
}
//獲取編譯時間
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
def getAppName() {
return "領(lǐng)導(dǎo)端"
}
def getUpdateContent() {
return "更改個人中心到最下面和自動上傳打包功能"
}
/**
* 獲取版本類型
* @return
*/
def getBuildType() {
return "Release"
}
task uploadApk(group: "upload") {
dependsOn("assemble${getAreaName()}${getBuildType()}")
// 等待編譯完成后再執(zhí)行上傳操作
doLast {
uploadApk()
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。