Extracts the classes. dex file from the apk file in batches.

Source: Internet
Author: User

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.

 

 

 

 

 

 

 

 

 

 

 

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.