fir分三个主要步骤
1.获取fir上传凭证
2.上传APK+APP logo图标
3.获取最新的下载地址供别人下载
注:python使用的是requests网络请求库
1.获取fir上传凭证
image.png
代码如下:
# 第一步:获取fir上传凭证 print("get fir upload certificate") icondict = {} # 后面上传图标和apk需要使用的参数,这里保存下来 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图标
image.png
代码如下:
# 第二步:上传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.获取最新的下载地址
image.png
代码如下:
# 第四步:获取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()
代码里面如下参数都是动态传入的,配合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-8import sysimport tracebackimport requests requests.packages.urllib3.disable_warnings()def uploadtofir(): # 参数检查 paramnum = 8 syslen = len(sys.argv) if syslen < paramnum: print("please input param") return else: # 基础参数 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需要使用的参数,这里保存下来 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{}里面)
//自定义一个任务,实现打包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后发现多了一个assembleWithFir命令
image.png
双击assembleWithFir开始打包刚才指定的渠道,之后自动上传APK到fir,如下点击该连接就能查看相关了
image.png
注意事项:
.py python文件要放在app级别的目录下(非project)
确保电脑已安装python运行环境,requests网络请求库
作者:CKTim
链接:https://www.jianshu.com/p/833cc4d0c0ae