file = {'file': open(icon_path, 'rb')}
param = {"key": 'd1bca0636623f17782d9f851aa9e08c77f875a62',
'token': '替换成你自己的token'
}
req = requests.post('https://upload.qbox.me', files=file, data=param, verify=False)
print 'success:' + req.content
except Exception as e:
print'error:' + e
运行结果
{"is_completed":true}
编写gradle脚本
task debugToFir {
dependsOn 'assembleDebug'
doLast {
def upUrl = "http://api.fir.im/apps"
def appName = "Python2"
def bundleId = project.android.defaultConfig.applicationId
def verName = project.android.defaultConfig.versionName
def apiToken = "9812fa28e4dac156673a5e45e7119631"
def iconPath = "F:/PythoPackage/app/src/main/res/mipmap-xxhdpi/ic_launcher.png"
def apkPath = "F:/PythoPackage/app/build/outputs/apk/debug/app-debug.apk"
def buildNumber = project.android.defaultConfig.versionCode
def changeLog = "版本更新日志"
//执行Python脚本
def process = "python upToFir.py ${upUrl} ${appName} ${bundleId} ${verName} ${apiToken} ${iconPath} ${apkPath} ${buildNumber} ${changeLog}".execute()
println("开始上传至fir")
//获取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"))
println "上传结束 "
}
}
该脚本放在 app/build.gradle 中的 android 目录下
统一Python脚本
# coding=utf-8
# encoding = utf-8
import requests
import sys
def upToFir():
# 打印传递过来的参数数组长度,便于校验
print 'the argLength--->:' + len(sys.argv)
upUrl = sys.argv[1]
appName = sys.argv[2]
bundleId = sys.argv[3]
verName = sys.argv[4]
apiToken = sys.argv[5]
iconPath = sys.argv[6]
apkPath = sys.argv[7]
buildNumber = sys.argv[8]
changeLog = sys.argv[9]
queryData = {'type': 'android', 'bundle_id': bundleId, 'api_token': apiToken}
iconDict = {}
binaryDict = {}
# 获取上传信息
try:
response = requests.post(url=upUrl, data=queryData)
json = response.json()
iconDict = (json["cert"]["icon"])
binaryDict = (json["cert"]["binary"])
except Exception as e:
print('query:' + e)
# 上传apk
try:
file = {'file': open(apkPath, 'rb')}
param = {"key": binaryDict['key'],
'token': binaryDict['token'],
"x:name": appName,
"x:version": verName,
"x:build": buildNumber,
"x:changelog": changeLog}
req = requests.post(url=binaryDict['upload_url'], files=file, data=param, verify=False)
print 'success_apk:' + req.content
except Exception as e:
print'error_apk:' + e
# 上传logo
try:
file = {'file': open(iconPath, 'rb')}
param = {"key": iconDict['key'],
'token': iconDict['token']}
req = requests.post(url=iconDict['upload_url'], files=file, data=param, verify=False)
print 'success_icon:' + req.content
except Exception as e:
print'error_icon:' + e
if __name__ == '__main__':
upToFir()
前面的三个 python 脚本的参数都是写死的,所以需要改变成动态从 gradle 中获取,获取的时候先判断一下数组长度,看看是不是跟之前约定的一样
整体进行测试
这个时候修改一下apk的一些参数,跟logo
versionCode 3
versionName "1.2"
icOnPath=ic_launcher.png
appName="python"
执行gradle命令 gradlew debugToFir,运行结果
开始上传至fir
http://api.fir.im/apps
success_apk:{"is_completed":true}
success_icon:{"is_completed":true}
上传结束 with value 0
运行成功,到官网查看结果
完美,简单,以后简单的打包就用一行代码就可以搞定了,吼吼。
小结
其实 Python 的语法很简洁,作为一门动态语言,不需要像 Java 定义各种类型变量,gradle 的语法其实也一样,掌握这两种语言的基本用法,有助于更高效的开发 Android。
源码下载 https://github.com/wustor/PythoPackage
与之相关
1 6 天时间修改 1 行代码:现实中的软件开发流程
2 编程之旅,致新入行的朋友
3 如何阅读代码(八点要记牢)