之前寫過利用Jenkins實現(xiàn)在線的自動化打包并自動上傳至fir州藕,適用于非開發(fā)人員打出自己想要環(huán)境的包,但對于開發(fā)而言酝陈,在平時本地開發(fā)過程中床玻,有時改動個字體,改動點顏色需要重新打包沉帮,利用JenKins未免有點小題大做锈死。
打包不麻煩贫堰,主要是手動把它傳到fir這一步比較煩人,百度了一通fir自動上傳插件待牵,沒找到適合自己的其屏,看到官方有提供相關的api,所以自己用python簡單寫了個自動上傳腳本缨该,在gradle打包完成后執(zhí)行python腳本進行上傳偎行。
fir官方開發(fā)文檔地址:https://fir.im/docs/publish
fir分三個主要步驟
- 1.獲取fir上傳憑證
- 2.上傳APK+APP logo圖標
- 3.獲取最新的下載地址供別人下載
- 注:python使用的是requests網(wǎng)絡請求庫
1.獲取fir上傳憑證
代碼如下:
# 第一步:獲取fir上傳憑證
print("get fir upload certificate")
icondict = {} # 后面上傳圖標和apk需要使用的參數(shù),這里保存下來
binarydict = {}
try:
req = requests.post("http://api.fir.im/apps",
{'type': 'android', 'bundle_id': apppackage, 'api_token': apitoken})
resjson = req.json()
icondict = (resjson["cert"]["icon"])
binarydict = (resjson["cert"]["binary"])
print("get fir upload certificate success")
except Exception:
print("get fir upload certificate error")
traceback.print_exc()
2.上傳APK+logo圖標
代碼如下:
# 第二步:上傳APK
try:
print("uploading apk......")
apkfile = {'file': open(apkpath, 'rb')}
param = {"key": binarydict["key"],
"token": binarydict["token"],
"x:name": appname,
"x:version": appversion,
"x:build": appbuild,
"x:changelog": apkchangelog}
req = requests.post(url=binarydict["upload_url"], files=apkfile, data=param, verify=False)
except Exception as e:
print("upload apk error")
traceback.print_exc()
# 第三步:上傳APK logo
try:
apklogofile = {'file': open(apklogo, 'rb')}
param = {"key": icondict["key"],
"token": icondict["token"]}
req = requests.post(url=icondict["upload_url"], files=apklogofile, data=param, verify=False)
except Exception:
print("upload apk error")
traceback.print_exc()
3.獲取最新的下載地址
代碼如下:
# 第四步:獲取APK最新下載地址
queryurl = 'http://api.fir.im/apps/latest/%s?api_token=%s&type=android' % (apppackage, apitoken)
try:
req = requests.get(queryurl)
update_url = (req.json()["update_url"])
print("upload apk success, update url is " + update_url)
except Exception:
print("upload apk error")
traceback.print_exc()
代碼里面如下參數(shù)都是動態(tài)傳入的贰拿,配合AndroidStudio自帶的gradle使用
appname = sys.argv[1] # app名稱
apppackage = sys.argv[2] # 唯一包名蛤袒,也即是bundle_id
appversion = sys.argv[3] # app版本號
appbuild = sys.argv[4] # app build號
apitoken = sys.argv[5] # fir token
apklogo = sys.argv[6] # 等待上傳的APK logo路徑
apkpath = sys.argv[7] # 等待上傳的APK路徑
apkchangelog = sys.argv[8] # 等待上傳的APK更新日志(可能沒有填寫)
完整代碼
# encoding = utf-8
import sys
import traceback
import requests
requests.packages.urllib3.disable_warnings()
def uploadtofir():
# 參數(shù)檢查
paramnum = 8
syslen = len(sys.argv)
if syslen < paramnum:
print("please input param")
return
else:
# 基礎參數(shù)
appname = sys.argv[1] # app名稱
apppackage = sys.argv[2] # 唯一包名,也即是bundle_id
appversion = sys.argv[3] # app版本號
appbuild = sys.argv[4] # app build號
apitoken = sys.argv[5] # fir token
apklogo = sys.argv[6] # 等待上傳的APK logo路徑
apkpath = sys.argv[7] # 等待上傳的APK路徑
apkchangelog = syslen == 9 and sys.argv[8] or "" # 等待上傳的APK更新日志(可能沒有填寫)
# 第一步:獲取fir上傳憑證
print("get fir upload certificate")
icondict = {} # 后面上傳圖標和apk需要使用的參數(shù)膨更,這里保存下來
binarydict = {}
try:
req = requests.post("http://api.fir.im/apps",
{'type': 'android', 'bundle_id': apppackage, 'api_token': apitoken})
resjson = req.json()
icondict = (resjson["cert"]["icon"])
binarydict = (resjson["cert"]["binary"])
print("get fir upload certificate success")
except Exception:
print("get fir upload certificate error")
traceback.print_exc()
# 第二步:上傳APK
try:
print("uploading apk......")
apkfile = {'file': open(apkpath, 'rb')}
param = {"key": binarydict["key"],
"token": binarydict["token"],
"x:name": appname,
"x:version": appversion,
"x:build": appbuild,
"x:changelog": apkchangelog}
req = requests.post(url=binarydict["upload_url"], files=apkfile, data=param, verify=False)
except Exception as e:
print("upload apk error")
traceback.print_exc()
# 第三步:上傳APK logo
try:
apklogofile = {'file': open(apklogo, 'rb')}
param = {"key": icondict["key"],
"token": icondict["token"]}
req = requests.post(url=icondict["upload_url"], files=apklogofile, data=param, verify=False)
except Exception:
print("upload apk error")
traceback.print_exc()
# 第四步:獲取APK最新下載地址
queryurl = 'http://api.fir.im/apps/latest/%s?api_token=%s&type=android' % (apppackage, apitoken)
try:
req = requests.get(queryurl)
update_url = (req.json()["update_url"])
print("upload apk success, update url is " + update_url)
except Exception:
print("upload apk error")
traceback.print_exc()
if __name__ == '__main__':
uploadtofir()
Android Gradle配置
將上面寫好的.py文件復制到項目的app目錄下(非project目錄)妙真,同時在app級別下的gradle文件中添加如下代碼(添加到android{}里面)
//自定義一個任務,實現(xiàn)打包meisha渠道自動上傳到fir
task assembleWithFir{
dependsOn 'assembleXXXXRelease'//打包自己需要的渠道
doLast{
def appname="你的APP名稱"
def apppackage=project.android.defaultConfig.applicationId
def appversion=project.android.defaultConfig.versionName
def appbuild=project.android.defaultConfig.versionCode
def apitoken="你的fir ApiToken"
def apklogo="你的APK LOGO"
def apkpath="你的APK本地地址"
def apkchangelog=""
//調用python腳本 這個腳本需要放在app工程目錄下荚守,不要放在project目錄下
def process="python UploadToFir.py ${appname} ${apppackage} ${appversion} ${appbuild} ${apitoken} ${apklogo} ${apkpath} ${apkchangelog}".execute()
//打印Python腳本日志珍德,便于出錯調試
ByteArrayOutputStream result = new ByteArrayOutputStream()
def inputStream = process.getInputStream()
byte[] buffer = new byte[1024]
int length
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length)
}
println(result.toString("utf-8"))
}
}
至此全部搞定,同步一下gradle后發(fā)現(xiàn)多了一個assembleWithFir命令
雙擊assembleWithFir開始打包剛才指定的渠道矗漾,之后自動上傳APK到fir菱阵,如下點擊該連接就能查看相關了
注意事項:
- .py python文件要放在app級別的目錄下(非project)
- 確保電腦已安裝python運行環(huán)境,requests網(wǎng)絡請求庫