本文最后更新于 1034 天前,其中的信息可能已经有所发展或是发生改变。
开发的时候有时候频繁修改 smali,或者 合并 res,然后出问题,都要重新 修改逻辑,然后 回编译签名安装,很慢,拖慢了工作效率,写个简单的脚本把整个流程自动化;留着这个时间泡杯咖啡
写一个简单的工具类
import os
import time
import argparse
class Signer(object):
def __init__(self,alias="zhangkun",alg="RSA",validity=20000,location=os.popen('pwd').read()):
self.alias = alias
self.alg = alg
self.validity = validity
self.location = location.replace('\n',"")
def setinfo(self,storepwd="ZK2019",keypwd="ZK2019",name="apson"):
self.storepwd = storepwd
self.keypwd = keypwd
self.name = name
return self
def setcompany(self,unit="unknown",organization="unknown",area="unknown",province="unknown",contry="cn"):
self.unit=unit
self.organization = organization
self.area = area
self.province = province
self.contry=contry
return self
# keytool -genkey -noprompt -alias kf2018 -keyalg RSA -validity 21900 -dname "CN=zhusheng, OU=kuafu, O=kuafugs, L=guangzhou, ST=guangdong, C=cn"
# -keystore /Users/wenhuayang/Desktop/easdk/kf_game.keystore -storepass kf2018.. -keypass kf2018##
'''生成key'''
def genkey(self):
print('生成秘钥配置文件')
if os.system('mkdir ' + self.unit)!=0:
return "the company is already exist"
try:
f = open(self.unit+'/'+"key.config",'w')
f.write("alias:"+self.alias+"\n")
f.write("company:"+self.unit+"\n")
f.write("alg:"+self.alg+"\n")
f.write("validity:"+str(self.validity)+"\n")
f.write("keypwd:"+self.keypwd+"\n")
f.write("storepwd:"+self.storepwd+"\n")
except Exception as e:
return "fail:\n"+str(e)
finally:
f.close()
dname = "'"+"CN={name},OU={unit},O={organization},L={area},ST={province},C={contry}"+"'".format(name=self.name , unit=self.unit , organization=self.organization , area = self.area , province = self.province,contry=self.contry)
cmd = "keytool -genkey -noprompt " + " -alias " + self.alias + " -keyalg " + self.alg + " -validity " + str(self.validity)
cmd += " -dname " + dname
cmd += " -keystore " + self.location + "/" + self.unit + "/" + self.alias + ".keystore"
cmd += " -storepass "+ self.storepwd +" -keypass " + self.keypwd +""
print(cmd)
if os.system(cmd)==0:
return "gen success"
else:
return "error"
'''去除签名 后期可在这里做资源替换的处理'''
def unsignapk(self,apklocation=需要删除签名的apk文件):
if apklocation[apklocation.find('.'):]!='.apk':
return 'u should input an android application'
appname = apklocation[apklocation.rfind('/')+1:apklocation.find('.')]
location = os.popen('pwd').read().replace('\n',"")+"/"+appname
print(location)
os.system('mkdir '+appname)
os.system('unzip ' + apklocation + ' -d ' + location)
os.system('rm -rf '+ location +'/'+'META-INF')
os.system(f'pushd {location}' +"&&"+'zip -q -r unsigned.apk ' + '*'+"&&"+'popd')
os.system(f'mv {location}/unsigned.apk signed.apk')
# os.system()
'''目标sdk位置 | 签名后sdk位置(默认为程序运行位置) | 签名密匙位置(绝对路径)默认为程序内自带的密匙 | 密匙密码 | 密匙密码'''
def sign(self,target,siginedlocation=签名完成后的apk位置,keystore=写你的密钥文件位置,storepwd=写你的密钥,keypwd=写你的密钥 ):
cmd = "jarsigner -verbose -keystore {keystorelocation} -storepass {storepass} -keypass {keypass} -signedjar {signedjarlocation} -digestalg SHA1 -sigalg MD5withRSA {targetapklocation} {alias}"
cmd = cmd.format(keystorelocation=keystore,storepass=storepwd,keypass=keypwd,signedjarlocation=target,targetapklocation=target,alias=self.alias)
print(siginedlocation)
os.system(cmd)
通过外部传入参数调用工具类
parser = argparse.ArgumentParser(description="powered from apson")
parser.add_argument('-sp','--signingproperty',help='签名属性选择,ysdk、normal,默认是 normal',default='normal',choices=['normal'])
parser.add_argument('-t','--target',help='目标文件位置')
parser.add_argument('-r','--result',help='结果输出位置',default='signed.apk')
parser.add_argument('-d','--dargon',help='目标文件夹位置,打包签名安装一条龙',)
parser.add_argument('-s','--serial',help='指定设备系列号',)
args = parser.parse_args()
filename = args.dargon
resultname = args.result
spCheck = args.signingproperty
serial = args.serial
print(spCheck)
if serial:
serial = ' -s ' + serial
else :
serial = ''
if filename!=None and filename!='':
cmd_akt = 'java -jar apktool_2.4.1.jar b ' + filename + ' -f -o ' + resultname
os.system(cmd_akt)
print('回编译打包apk文件:'+os.popen('pwd').read().replace('\n',"")+"/"+resultname)
if spCheck=='normal':
s.sign(resultname)
os.system('adb '+serial+'install ' + resultname)
过了几年看自己写的 python 代码,写的好丑啊,都是简单的字符串拼接,大家想要用的话 直接拷贝就能用,因为都是字符串拼接,所以路径不支持 windows 的,windows 的开发者这份代码仅供参考哈