Android 通过python实现自动化构建打包上传加固
Posted 隔壁小王66
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 通过python实现自动化构建打包上传加固相关的知识,希望对你有一定的参考价值。
android 通过python实现自动化构建打包上传加固
实现需求:
基于gradle命令,通过python实现多渠道,多环境打包,上传蒲公英,360加固等
经过学习调研完成步骤如下:
一,通过gradle配置多渠道多环境:
productFlavors
baidu
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
xiaodou
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaodou"]
c360
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"]
qqyyb
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qqyyb"]
miapp
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "miapp"]
huaweiapp
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huaweiapp"]
oppoapp
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "oppoapp"]
vivoapp
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "vivoapp"]
flymeapp
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "flymeapp"]
buildTypes
debug
minifyEnabled false //是否开启混淆
shrinkResources false //是否删除无用的资源
zipAlignEnabled true//对齐字节码进行压缩
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationIdSuffix "test"
signingConfig signingConfigs.debug
resValue "string", "app_name", "测试版"
manifestPlaceholders = [JPUSH_APPKEY: "xxx"]
buildConfigField "String", "JPUSH_TAG", '"test"'
release
minifyEnabled true //是否开启混淆
shrinkResources false //是否删除无用的资源
zipAlignEnabled true//对齐字节码进行压缩
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
resValue "string", "app_name", "xx"
manifestPlaceholders = [JPUSH_APPKEY: "xxx"]
buildConfigField "String", "JPUSH_TAG", '"online"'
pre_release
minifyEnabled true //是否开启混淆
shrinkResources false //是否删除无用的资源
zipAlignEnabled true//对齐字节码进行压缩
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationIdSuffix "pre_release"
signingConfig signingConfigs.release
resValue "string", "app_name", "预发布版"
manifestPlaceholders = [JPUSH_APPKEY: "xxx"]
buildConfigField "String", "JPUSH_TAG", '"pre_release"'
二,通过python来控制gradle打包命令,实现多渠道,多环境打包
#获取打包配置信息
def getConfig(build_line,android_project_path,app_src,apk_path):
flag=raw_input(' build type is release ? \\r\\n\\n 0:release \\r\\n 1:debug \\r\\n 2:pre_release \\r\\r\\r\\r\\r\\n\\n')
if flag.endswith('0'):
channel=raw_input('\\n\\n please input build channel \\r\\n\\n 0:all \\r\\n 1:baidu \\r\\n 2:xiaodou \\r\\n 3:c360 \\r\\n 4:qqyyb \\r\\n 5:miapp \\r\\n 6:huaweiapp \\r\\n 7:oppoapp \\r\\n 8:vivoapp \\r\\n 9:flymeapp \\r\\r\\r\\r\\r\\n\\n')
if channel.endswith('0'):
build_line="gradle assembleRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-xiaodou-release.apk"
elif channel.endswith('1'):
build_line="gradle assembleBaiduRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-baidu-release.apk"
elif channel.endswith('2'):
build_line="gradle assembleXiaodouRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-xiaodou-release.apk"
elif channel.endswith('3'):
build_line="gradle assembleC360Release"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-c360-release.apk"
elif channel.endswith('4'):
build_line="gradle assembleQqyybRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-qqyyb-release.apk"
elif channel.endswith('5'):
build_line="gradle assembleMiappRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-miapp-release.apk"
elif channel.endswith('6'):
build_line="gradle assembleHuaweiappRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-huaweiapp-release.apk"
elif channel.endswith('7'):
build_line="gradle assembleOppoappRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-oppoapp-release.apk"
elif channel.endswith('8'):
build_line="gradle assembleVivoappRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-vivoapp-release.apk"
elif channel.endswith('9'):
build_line="gradle assembleFlymeappRelease"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-flymeapp-release.apk"
elif flag.endswith('1'):
build_line="gradle assembleXiaodouDebug"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-xiaodou-debug.apk"
elif flag.endswith('2'):
build_line="gradle assembleXiaodouPre_release"
app_src = android_project_path + "/build/outputs/apk/xd-app-quesbk-xiaodou-pre_release.apk"
pass
print('<------------------------------start------------------------------>')
print(build_line)
os.system(build_line)
new_apk_name = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + ".apk"
app_dest = apk_path+new_apk_name
shutil.copy2(app_src, app_dest)
print('<---------------------------------end-------------------------------->')
print ('<------------------------------success--------------------------->')
next=raw_input("next is firm or upload ? \\r\\n1:firm \\n2:upload \\r\\n")
if next.endswith("1"):
exec_360_jiagu_comm(app_src)
else:
uploadApktopgy(app_src)
通过raw_input来获取用户需要打包的配置,如buildtype and build channel,然后生成不同的打包命令以及包的路径地址,如
gradle assembleBaiduRelease,
android_project_path + "/build/outputs/apk/xd-app-quesbk-baidu-release.apk"
通过 os.system(build_line)
来执行gradle命令
这里需要处理一个问题,我们在Android studio的Terminal中执行打包命令用的是gradlew命令,如何在cmd命令行中执行gradle命令而不是gradlew命令呢?
1:配置gradle 环境变量
2:检查gradle是否配置成功
然后cd 到项目目录下就可以执行gradle命令进行测试了。
三,获取包进行上传蒲公英操作
#上传蒲公英或者fir
def uploadApktopgy(app_dest):
print("app_dest--->"+app_dest)
print("-------------------------------Upload preparation------------------------------")
# 将要上新的版本描述
updateDescription = ""
#蒲公英App_Key
uKey = 'xxx'
apiKey = 'xxx'
res=os.system("curl -F file=@"+app_dest+" -F _api_key="+apiKey+" https://www.pgyer.com/apiv2/app/upload")
res_json=json.loads(str(res))
if res_json["code"]==0:
print("---------------------upload pgy success-------------------")
else:
print("---------------------upload pgy failure-------------------")
pass
这里需要用到curl执行蒲公英提供的上传api,配置好uKey跟apiKey,参数去蒲公英账号获取。
四,360加固
#360加固配置
def exec_360_jiagu_comm(apkPath):
#360账号
_360JiaguUserName="xxx"
#360密码
_360JiaguPwd="xxx"
#360加固工具包路径
_360JiaguToolsPath=raw_input("Please specify the storage path of the 360 hardening tool.(eg:My local path is D:\\\\jiagu\\\\360jiagubao_windows_64\\\\jiagu): \\n")
#jks文件路径
keystorePath=_360JiaguToolsPath+"/xx.jks"
#别名
keystore_alias="xx"
#jks密码
keystore_pass="xx"
#jks密码
key_pass="xx"
#渠道配置文件路径
mulpkg_filepath=_360JiaguToolsPath+"/channel.txt"
#加固apk输出路径
mulpkg_output_path=_360JiaguToolsPath+"/apk/"
#检测apk输出路径是否存在,不存在就创建
checkFilePath(mulpkg_output_path)
#切换到工作目录下
os.chdir(_360JiaguToolsPath)
#首次使用必须先登录
os.system("java -jar jiagu.jar -login " + _360JiaguUserName + " " + _360JiaguPwd)
#导入签名配置
os.system("java -jar jiagu.jar -importsign " + keystorePath + " " + keystore_pass + " " + keystore_alias + " " + key_pass)
#导入多渠道配置信息,txt格式
os.system("java -jar jiagu.jar -importmulpkg " + mulpkg_filepath)
#查看已配置的签名信息
os.system("java -jar jiagu.jar -showsign ")
#查看已配置的多渠道信息
os.system("java -jar jiagu.jar -showmulpkg ")
#显示已配置加固项
os.system("java -jar jiagu.jar -showconfig ")
#显示当前版本号
os.system("java -jar jiagu.jar -version ")
#加固命令
jiagu_cmd = "java -jar jiagu.jar -jiagu " + apkPath + " " + mulpkg_output_path + " -autosign -automulpkg "
#执行加固命令
result = os.system(jiagu_cmd)
if not result:
print("========" + str(result) + " jiagu success ========")
filename = tkFileDialog.askopenfilename(initialdir = mulpkg_output_path)
else:
print("======== jiagu failure ========")
配置好360账号密码,360加固工具包路径,jks文件路径,别名,jks密码,渠道配置文件路径,然后切换到360加固包下载路径下,通过os.system执行360相应的加固命令即可。
加固渠道模板配置:
UMENG_CHANNEL_VALUE c360 1
UMENG_CHANNEL_VALUE qqyyb 2
UMENG_CHANNEL_VALUE miapp 3
UMENG_CHANNEL_VALUE flymeapp 4
UMENG_CHANNEL_VALUE huaweiapp 5
UMENG_CHANNEL_VALUE baidu 6
UMENG_CHANNEL_VALUE oppoapp 7
UMENG_CHANNEL_VALUE vivoapp 8
另外增加了加固完成打开加固目录的操作tkFileDialog.askopenfilename
,使用它需要import tkFileDialog
。
基本上流程就是如此了,有什么问题欢迎下面详谈。
以上是关于Android 通过python实现自动化构建打包上传加固的主要内容,如果未能解决你的问题,请参考以下文章
Android 借助 Python 实现自动打包上传 fir
Android QA专用,Python实现不一样的多渠道打包工具
Android QA专用,Python实现不一样的多渠道打包工具
游戏开发进阶教你Unity通过Jenkins实现自动化打包,打包这种事情就交给策划了(保姆级教程 | 命令行打包 | 自动构建)