Extracts the classes. dex file from the apk file in batches.
Application scenarios
If you need to analyze the apk in batches and the classes. dex file in each apk file. How to extract them? Change the suffix of apkto A. ZIP file, decompress the file, and extract the classes. dex file from each apk file. This is a feasible solution. However, extracting a large number of apk files in the middle will occupy a large amount of disk storage space. How can we extract dex files without interrupting the pressure of files? Here, we use the zipfile class that comes with python to easily solve this problem.
Code implementation:
#!/usr/bin/env python# coding=utf-8'''@author : Chicho@version : 1.0@ date : Jan 4, 2015 01:54@function : extract dexs file from apk files * create a new directory to store all dex files@running : python dex_extract.py'''import osimport zipfilepath=/home/chicho/test/test/ # this is apk files' store pathdex_path=/home/chicho/test/test/dex/ # a directory store dex files apklist = os.listdir(path) # get all the names of appsif not os.path.exists(dex_path): os.makedirs(dex_path)for APK in apklist: portion = os.path.splitext(APK) if portion[1] == .apk: newname = portion[0] + .zip # change them into zip file to extract dex files os.rename(APK,newname) if APK.endswith(.zip): apkname = portion[0] zip_apk_path = os.path.join(path,APK) # get the zip files z = zipfile.ZipFile(zip_apk_path, 'r') # read zip files for filename in z.namelist(): if filename.endswith(.dex): dexfilename = apkname + .dex dexfilepath = os.path.join(dex_path, dexfilename) f = open(dexfilepath, 'w+') # eq: cp classes.dex dexfilepath f.write(z.read(filename))print all work done!
This method can also be used to extract other files in batches from the apk file. For example, the so file.