最近因为公司业务需要,需要用Python写了一个自动修改apk,自动签名的小工具。我把核心代码抽取出来,今天分享给大家。
1,业务需求
自定义配置文件,修改多个渠道的渠道名,修改完成后自动打包签名。
2,开发思路
2.1,读取配置文件
2.2,然后反编译apk,替换manifest里面的配置信息,然后回编译,签名。
2.3,用循环实现多渠道打包。
大概意思就是要替换如下图所示“Change_qudao”部分
3,技术要点
3.1,反编译和回编译apk,需要用到apktools。
- apktool官方网站:https://ibotpeaches.github.io/Apktool/
- 安装方法:https://ibotpeaches.github.io/Apktool/install
3.2, 读取配置文件
通过系统的open函数打开文件读取文件信息,然后存在列表里,代码如下
1 2 3 4 5 6 7 8 |
import sys path_item = sys.path[0]#获取当前目录的绝对路径 var lines=open(path_item+"\\qudao.txt",'r',encoding='utf-8').readlines()#配置信息存储在“qudao.txt”文件下,每一行一个渠道 for line_item in lines: line_item=line_item.strip('\n') if not line_item:#非空判断 continue print(line_item) |
3.2,修改文件内容
修改文件内容需要用到File模块和shutil模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#修改文件信息 #参数含义:tfile 文件名 # sstr 原始字符 # rstr 修改后的字符 def modifyFile(tfile,sstr,rstr): print('开始修改文件'+tfile) print('原始内容'+sstr) print('替换内容'+rstr) try: lines=open(tfile,'r',encoding='utf-8').readlines() flen=len(lines) for i in range(flen): if sstr in lines[i]: lines[i]=lines[i].replace(sstr,rstr) open(tfile,'w',encoding='utf-8').writelines(lines) print('修改文件内容成功:'+rstr) except IOError: print('输入输出异常') except Exception as e: print(e) print('修改内容失败') |
4,把代码整合一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#FileName:lean001.py #author:www.py40.com #执行os命令 import os import shutil import sys path_item = sys.path[0] path_apk_folder =path_item+'\\apk' keystore_password = 'aa123456' keystore_bieming = 'autosign' keystore_name = 'autosign_aa123456.jks' listqudao = [] #修改文件类容 def modifyFile(tfile,sstr,rstr): print('开始修改文件'+tfile) print('原始内容'+sstr) print('替换内容'+rstr) try: lines=open(tfile,'r',encoding='utf-8').readlines() flen=len(lines) for i in range(flen): if sstr in lines[i]: lines[i]=lines[i].replace(sstr,rstr) open(tfile,'w',encoding='utf-8').writelines(lines) print('修改文件内容成功:'+rstr) except IOError: print('输入输出异常') except Exception as e: print(e) print('修改内容失败') #反编译apk def decomApk(): #删除过期文件 print('删除过期文件'+path_apk_folder) shutil.rmtree(path_apk_folder,True) path_apk_old = 'apktool d '+path_item+'\\app.apk -o ' +path_apk_folder os.system(path_apk_old) print('反编译apk文件成功:'+path_apk_folder) #对apk进行签名 def signApk(): print('开始回编译打包') apk_back = 'apktool b '+path_item+"\\apk" os.system(apk_back) print('回编译打包成功') print('开始执行自动签名任务') signapkcommond = 'jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore '+path_item+'\\'+keystore_name+' -storepass '+ keystore_password+' '+ path_item+'\\apk\\dist\\app.apk '+keystore_bieming; print('签名命令:'+signapkcommond) os.system(signapkcommond) print('签名成功') #初始化 def initItem(): #删除appresult文件夹 print('清除appresult文件夹') shutil.rmtree(path_item+"\\appresult",True) #创建appresult文件夹 print('创建appresult文件夹') os.makedirs(path_item+"\\appresult") lines=open(path_item+"\\qudao.txt",'r',encoding='utf-8').readlines() for line_item in lines: line_item=line_item.strip('\n') if not line_item: continue print(line_item) decomApk() print('开始修改apk内容') fileManifest = path_apk_folder+"\\AndroidManifest.xml" modifyFile(fileManifest,'Duoyou_qudao',line_item) signApk() shutil.copy(path_item+"\\apk\\dist\\app.apk", path_item+"\\appresult") print('文件复制成功') new_app_name = 'app_'+line_item+'.apk' print("文件名修改为:",new_app_name) os.rename(path_item+"\\appresult\\app.apk", path_item+"\\appresult\\"+new_app_name) print("当前目录为:"+path_item) initItem() print("脚本执行结束:") |
未经允许不得转载:Python在线学习 » Python自动化批量替换apk渠道号