apk0 2012-04-16
#! /usr/bin/env python #coding=utf-8 import os import subprocess import time import shutil,sys import zipfile apksrc="C:\\test\\test.apk" apkdest="" copyzip="" dex2dir="" jadsrc="" projectcodepath="" apktoolpath=r"F:\\bishe\android\apktool\apktool.bat" dex2jarpath=r"F:\\bishe\android\dex2jar-0.0.9.8\dex2jar-0.0.9.8\dex2jar.bat" jadexepath=r"F:\\bishe\android\jad158g\jad.exe" def apktools(src,dest): cmd_list=apktoolpath+r" d -f "+src+" "+dest print cmd_list process = subprocess.Popen(cmd_list) while 1: ret1 = subprocess.Popen.poll(process) if ret1 == 0: print process.pid,'end' break elif ret1 is None: time.sleep(1) else: print sub2.pid,'term' break print "apk tools end" def jad(src,dest): codesrc=dest+"src\\" if((os.path.exists(codesrc))==False): os.makedirs(codesrc) cmd_list=jadexepath+" -o -r -d "+codesrc+" -s java "+src+"**\\*.class" print cmd_list process = subprocess.Popen(cmd_list) while 1: ret1 = subprocess.Popen.poll(process) if ret1 == 0: print process.pid,'end' break elif ret1 is None: time.sleep(1) else: print sub2.pid,'term' break print "apk jad code successfully" def dex2jar(src,dest): cmd_list=dex2jarpath+" "+src+"classes.dex" print cmd_list process = subprocess.Popen(cmd_list) while 1: ret1 = subprocess.Popen.poll(process) if ret1 == 0: print process.pid,'end' break elif ret1 is None: time.sleep(1) else: print sub2.pid,'term' break print "apk dex2jar.bat successfully" def unzip(src,dest): print src f = zipfile.ZipFile(src) f.extractall(dest) f.close() print "apk unzip successfully" def GenerateProCode(dest): print jadsrc+"src" print dest shutil.copytree(jadsrc+"src\\",dest) shutil.copytree(apkdest,dest) def adbinstall(src): cmd_list="adb install "+src print cmd_list process = subprocess.Popen(cmd_list) while 1: ret1 = subprocess.Popen.poll(process) if ret1 == 0: print process.pid,'end' break elif ret1 is None: time.sleep(1) else: print sub2.pid,'term' break print "apk install successfully" def init(src,dest): global apkdest global dex2dir global copyzip global jadsrc global projectcodepath copyzip=os.path.dirname(src)+"\\"+"dex2jar.zip" if dest=="": apkdest=os.path.dirname(src)+"\\ProjectCode\\" shutil.copy2(src,copyzip) dex2dir=os.path.dirname(src)+"\\dex\\" jadsrc=os.path.dirname(src)+"\\jad\\" init(apksrc,apkdest) apktools(apksrc,apkdest) unzip(copyzip,dex2dir) dex2jar(dex2dir,apkdest) unzip(dex2dir+"classes_dex2jar.jar",jadsrc) jad(jadsrc,apkdest) adbinstall(apksrc)
代码如上,就是对一个apk文件自动解压缩,生成最终代码在projectcode中,包括了解压缩后的AndroidManifest.xml,反编译的java文件,资源文件等等