bale_apk/ApkTool/MakeTool.py
2021-06-02 11:13:02 +08:00

387 lines
15 KiB
Python

# -*- coding:utf-8 -*-
import re
import os
import stat
import shutil
import json
print(os.path.abspath(__name__))
import sys
RD, WD, XD = 4, 2, 1
BNS = [RD, WD, XD]
MDS = [
[stat.S_IRUSR, stat.S_IRGRP, stat.S_IROTH],
[stat.S_IWUSR, stat.S_IWGRP, stat.S_IWOTH],
[stat.S_IXUSR, stat.S_IXGRP, stat.S_IXOTH]
]
def chmod(path, mode):
if isinstance(mode, int):
mode = str(mode)
if not re.match("^[0-7]{1,3}$", mode):
raise Exception("mode does not conform to ^[0-7]{1,3}$ pattern")
mode = "{0:0>3}".format(mode)
mode_num = 0
for midx, m in enumerate(mode):
for bnidx, bn in enumerate(BNS):
if (int(m) & bn) > 0:
mode_num += MDS[bnidx][midx]
# os.chmod(path, mode_num)
#遍历res下的所有文件
def isFindApk (parent):
for i in os.listdir(parent) :
if i.endswith('.apk') :
print ('find apk file-===%s' %i)
return i;
print ('no apk file=====')
return ''
def readChannelfile(filename):
try:
print (filename)
f = open(filename)
global channelList
except Exception as e:
print(e)
exit(0)
channelList = json.load(f)
def backUpManifest():
if os.path.exists('./AndroidManifest.xml'):
os.remove('./AndroidManifest.xml')
manifestPath = './temp/AndroidManifest.xml'
shutil.copyfile(manifestPath, './AndroidManifest.xml')
print ('manifest info--%s' %manifestPath)
def modifyChannel(value):
tempXML = ''
f = open('./AndroidManifest.xml')
for line in f :
if line.find('leguChannel') > 0 and easyName != '' :
if easyNameSuffix == "1" :
if value < 10 :
line = '<meta-data android:name="leguChannel" android:value="%s_0%d"/>' % (easyName, value)
else :
line = '<meta-data android:name="leguChannel" android:value="%s_%d"/>' % (easyName, value)
else :
line = '<meta-data android:name="leguChannel" android:value="%s"/>' % (easyName)
elif line.find('owner') > 0 and owner != '' :
print ('owner====%s' %line)
line = '<meta-data android:name="owner" android:value="%s"/>' % (owner)
elif line.find('package') > 0 and packageId != '' :
print ('package====%s' %line)
line = '<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="30" android:compileSdkVersionCodename="11" android:installLocation="auto" package="%s" platformBuildVersionCode="30" platformBuildVersionName="11">' % (packageId)
elif line.find('appid') > 0 and appid != '' :
print ('appid====%s' %line)
line = '<meta-data android:name="appid" android:value="%s"/>' % (appid)
elif line.find('gism_id') > 0 and gism_id != '' :
print ('gism_id====%s' %line)
line = '<meta-data android:name="gism_id" android:value="%s"/>' % (gism_id)
elif line.find('gism_name') > 0 and gism_name != '' :
print ('gism_name====%s' %line)
line = '<meta-data android:name="gism_name" android:value="%s"/>' % (gism_name)
elif line.find('gism_channel') > 0 and gism_channel != '' :
print ('gism_channel====%s' %line)
line = '<meta-data android:name="gism_channel" android:value="%s"/>' % (gism_channel)
elif line.find('lg_ad_id') > 0 and lg_ad_id != '' :
print ('lg_ad_id====%s' %line)
line = '<meta-data android:name="lg_ad_id" android:value="%s"/>' % (lg_ad_id)
elif line.find('lg_ad_name') > 0 and lg_ad_name != '' :
print ('lg_ad_name====%s' %line)
line = '<meta-data android:name="lg_ad_name" android:value="%s"/>' % (lg_ad_name)
elif line.find('gactionId') > 0 and gactionId != '' :
print ('gactionId====%s' %line)
line = '<meta-data android:name="gactionId" android:value="%s"/>' % (gactionId)
elif line.find('gappSecretKey') > 0 and gappSecretKey != '' :
print ('gappSecretKey====%s' %line)
line = '<meta-data android:name="gappSecretKey" android:value="%s"/>' % (gappSecretKey)
elif line.find('baiduId') > 0 and baiduId != '' :
print ('baiduId====%s' %line)
line = '<meta-data android:name="baiduId" android:value="%s"/>' % (baiduId)
elif line.find('baiduSecretKey') > 0 and baiduSecretKey != '' :
print ('baiduSecretKey====%s' %line)
line = '<meta-data android:name="baiduSecretKey" android:value="%s"/>' % (baiduSecretKey)
elif line.find('lg_reyun_game_appkey') > 0 and lg_reyun_game_appkey != '' :
print ('lg_reyun_game_appkey====%s' %line)
line = '<meta-data android:name="lg_reyun_game_appkey" android:value="%s"/>' % (lg_reyun_game_appkey)
elif line.find('lg_reyun_game_channelId') > 0 and lg_reyun_game_channelId != '' :
print ('lg_reyun_game_channelId====%s' %line)
line = '<meta-data android:name="lg_reyun_game_channelId" android:value="%s"/>' % (lg_reyun_game_channelId)
elif line.find('lg_reyun_io_appkey') > 0 and lg_reyun_io_appkey != '' :
print ('lg_reyun_io_appkey====%s' %line)
line = '<meta-data android:name="lg_reyun_io_appkey" android:value="%s"/>' % (lg_reyun_io_appkey)
elif line.find('lg_reyun_io_channelId') > 0 and lg_reyun_io_channelId != 0 :
print ('lg_reyun_io_channelId====%s' %line)
line = '<meta-data android:name="lg_reyun_io_channelId" android:value="%s"/>' % (lg_reyun_io_channelId)
elif line.find('eventType') > 0 and eventType != '' :
print ('eventType====%s' %line)
line = '<meta-data android:name="eventType" android:value="%s"/>' % (eventType)
tempXML += line
f.close()
output = open('./temp/AndroidManifest.xml', 'w')
output.write(tempXML)
output.close()
if value < 10 :
unsignApk = r'./bin/%s_0%d_unsigned.apk'% (easyName, value)
#chmod(unsignApk, "777")
else :
unsignApk = r'./bin/%s_%d_unsigned.apk'% (easyName, value)
chmod(unsignApk, "777")
cmdPack = r'java -jar apktool.jar b temp -o %s'% (unsignApk)
os.system(cmdPack)
if value < 10 :
signedjar = r'./bin/%s_0%d.apk'% (easyName, value)
unsignedjar = r'./bin/%s_0%d_unsigned.apk'% (easyName, value)
chmod(signedjar, "777")
chmod(unsignedjar, "777")
else :
signedjar = r'./bin/%s_%d.apk'% (easyName, value)
unsignedjar = r'./bin/%s_%d_unsigned.apk'% (easyName, value)
chmod(signedjar, "777")
chmod(unsignedjar, "777")
cmd_sign = r'jarsigner -verbose -digestalg SHA1 -sigalg MD5withRSA -keystore %s -storepass %s -keypass %s -signedjar %s %s %s'% (keystore, storepass, keypass, signedjar, unsignedjar, alianame)
os.system(cmd_sign)
os.remove(unsignedjar);
channelList = {}
#
os.system('java -jar apkool.jar empty-framework-dir --force')
readChannelfile('./channel')
apkName = ""
apkNameSuffix = "0"
packageId = ""
packageIdSuffix = "0"
easyName = ""
easyNameSuffix = "0"
keystore = ''
storepass = ''
alianame = ''
keypass = ''
owner = ''
packNum = 0
start = 0
#----------------------- third qudao start----------------------
appid = ''
gism_id = ''
gism_name = ''
gism_channel = ''
lg_ad_id = ''
lg_ad_name = ''
gactionId = ''
gappSecretKey = ''
baiduId = ''
baiduSecretKey = ''
lg_reyun_game_appkey = ''
lg_reyun_game_channelId = ''
lg_reyun_io_appkey = ''
lg_reyun_io_channelId = ''
eventType = ''
#----------------------- third qudao end------------------------
print ('-------------------- your channel values --------------------')
#print 'channel list: ', channelList
if 'apkname' in channelList :
print ('apkname==%s' %channelList['apkname'])
print ('apkname value==%s' %channelList['apkname']['value'])
print ('apkname suffix==%s' %channelList['apkname']['suffix'])
apkName = channelList['apkname']['value']
apkNameSuffix = channelList['apkname']['suffix']
else :
apkName = isFindApk('./')
if 'packageId' in channelList :
print ('packageId==%s' %channelList['packageId'])
print ('packageId value==%s' %channelList['packageId']['value'])
print ('packageId suffix==%s' %channelList['packageId']['suffix'])
packageId = channelList['packageId']['value']
packageIdSuffix = channelList['packageId']['suffix']
else :
print ("no key packageId")
if 'leguChannel' in channelList :
print ('leguchannel==%s' %channelList['leguChannel'])
print ('leguchannel value==%s' %channelList['leguChannel']['value'])
print ('leguchannel suffix==%s' %channelList['leguChannel']['suffix'])
easyName = channelList['leguChannel']['value']
easyNameSuffix = channelList['leguChannel']['suffix']
else :
print ("no key leguChannel")
if 'keystore' in channelList :
print ('keystore==%s' %channelList['keystore'])
print ('keystore filename==%s' %channelList['keystore']['filename'])
print ('keystore storepass==%s' %channelList['keystore']['storepass'])
print ('keystore alianame==%s' %channelList['keystore']['alianame'])
print ('keystore keypass==%s' %channelList['keystore']['keypass'])
keystore = channelList['keystore']['filename']
storepass = channelList['keystore']['storepass']
alianame = channelList['keystore']['alianame']
keypass = channelList['keystore']['keypass']
else :
print ("no key keystore")
if 'owner' in channelList :
print ('owner==%s' %channelList['owner'])
print ('owner value==%s' %channelList['owner']['value'])
owner = channelList['owner']['value']
else :
print ("no key owner")
if 'num' in channelList :
print ('num==%s' %channelList['num'])
print ('num value==%s' %channelList['num']['value'])
print ('num start==%s' %channelList['num']['start'])
packNum = int(channelList['num']['value']) + 1
start = int(channelList['num']['start'])
else :
print ("no key num")
if 'appid' in channelList :
print ('appid==%s' %channelList['appid'])
print ('appid value==%s' %channelList['appid']['value'])
gism_id = channelList['appid']['value']
else :
print ("no key appid")
if 'gism_id' in channelList :
print ('gism_id==%s' %channelList['gism_id'])
print ('gism_id value==%s' %channelList['gism_id']['value'])
gism_id = channelList['gism_id']['value']
else :
print ("no key gism_id")
if 'gism_name' in channelList :
print ('gism_name==%s' %channelList['gism_name'])
print ('gism_name value==%s' %channelList['gism_name']['value'])
gism_name = channelList['gism_name']['value']
else :
print ("no key gism_name")
if 'gism_channel' in channelList :
print ('gism_channel==%s' %channelList['gism_channel'])
print ('gism_channel value==%s' %channelList['gism_channel']['value'])
gism_channel = channelList['gism_channel']['value']
else :
print ("no key gism_channel")
if 'lg_ad_id' in channelList :
print ('lg_ad_id==%s' %channelList['lg_ad_id'])
print ('lg_ad_id value==%s' %channelList['lg_ad_id']['value'])
lg_ad_id = channelList['lg_ad_id']['value']
else :
print ("no key lg_ad_id")
if 'lg_ad_name' in channelList :
print ('lg_ad_name==%s' %channelList['lg_ad_name'])
print ('lg_ad_name value==%s' %channelList['lg_ad_name']['value'])
lg_ad_name = channelList['lg_ad_name']['value']
else :
print ("no key lg_ad_name")
if 'gactionId' in channelList :
print ('gactionId==%s' %channelList['gactionId'])
print ('gactionId value==%s' %channelList['gactionId']['value'])
gactionId = channelList['gactionId']['value']
else :
print ("no key gactionId")
if 'gappSecretKey' in channelList :
print ('gappSecretKey==%s' %channelList['gappSecretKey'])
print ('gappSecretKey value==%s' %channelList['gappSecretKey']['value'])
gism_id = channelList['gappSecretKey']['value']
else :
print ("no key gappSecretKey")
if 'baiduId' in channelList :
print ('baiduId ==%s' %channelList['baiduId'])
print ('baiduId value==%s' %channelList['baiduId']['value'])
gism_id = channelList['baiduId']['value']
else :
print ("no key baiduId")
if 'baiduSecretKey' in channelList :
#print 'baiduSecretKey==', channelList['baiduSecretKey']
#print 'baiduSecretKey value==', channelList['baiduSecretKey']['value']
gism_id = channelList['baiduSecretKey']['value']
else :
print ("no key baiduSecretKey")
if 'lg_reyun_game_appkey' in channelList :
#print 'lg_reyun_game_appkey==', channelList['lg_reyun_game_appkey']
#print 'lg_reyun_game_appkey value==', channelList['lg_reyun_game_appkey']['value']
gism_id = channelList['lg_reyun_game_appkey']['value']
else :
print ("no key lg_reyun_game_appkey")
if 'lg_reyun_game_channelId' in channelList :
#print 'lg_reyun_game_channelId==', channelList['lg_reyun_game_channelId']
#print 'lg_reyun_game_channelId value==', channelList['lg_reyun_game_channelId']['value']
gism_id = channelList['lg_reyun_game_channelId']['value']
else :
print ("no key lg_reyun_game_channelId")
if 'lg_reyun_io_appkey' in channelList :
#print 'lg_reyun_io_appkey==', channelList['lg_reyun_io_appkey']
#print 'lg_reyun_io_appkey value==', channelList['lg_reyun_io_appkey']['value']
gism_id = channelList['lg_reyun_io_appkey']['value']
else :
print ("no key lg_reyun_io_appkey")
if 'lg_reyun_io_channelId' in channelList :
#print 'lg_reyun_io_channelId==', channelList['lg_reyun_io_channelId']
#print 'lg_reyun_io_channelId value==', channelList['lg_reyun_io_channelId']['value']
gism_id = channelList['lg_reyun_io_channelId']['value']
else :
print ("no key lg_reyun_io_channelId")
if 'eventType' in channelList :
#print 'eventType==', channelList['eventType']
#print 'eventType value==', channelList['eventType']['value']
gism_id = channelList['eventType']['value']
else :
print ("no key eventType")
output_apk_dir="./bin"
if os.path.exists(output_apk_dir):
shutil.rmtree(output_apk_dir)
print ('apkname--==%s' %apkName)
if apkName == '' :
print ('apkName no, stop ...')
sys.exit()
else :
cmdExtract = r'java -jar apktool.jar d -f -s %s -o temp'% (apkName)
os.system(cmdExtract)
backUpManifest()
for channel in range(start, packNum) :
modifyChannel(channel)
if os.path.exists('./temp'):
shutil.rmtree('./temp')
if os.path.exists('./AndroidManifest.xml'):
os.remove('./AndroidManifest.xml')
print ('Done')