Static Analysis of Android APK Files Using Python and Androguard
This article explains how to perform static analysis of Android APK files with Python, covering environment preparation, unpacking the APK, extracting basic metadata, analyzing classes and methods using the Androguard library, and generating a method call graph for deeper reverse‑engineering insights.
In Android development, testers often need to examine an APK without using reverse‑engineering tools; this guide shows how to statically analyze an APK using Python and the androguard library.
Environment preparation : install Python, the androguard package, and use a non‑obfuscated test APK.
Unpacking the APK :
def unzip_file(zip_src, dst_dir):
print(dst_dir)
r = zipfile.is_zipfile(zip_src)
if r:
fz = zipfile.ZipFile(zip_src, 'r')
for file in fz.namelist():
fz.extract(file, dst_dir)
searchDirFile(dst_dir)
else:
print('This is not zip')After extraction you will see directories such as META-INF , res , resources.arsc , classes.dex , and AndroidManifest.xml , each serving specific purposes.
Extracting basic information with Androguard:
from androguard.misc import AnalyzeAPK
apk, dex, dx = AnalyzeAPK(filePath)The apk object provides methods like apk.get_permissions() , apk.get_activities() , apk.get_services() , apk.get_receivers() , apk.get_providers() , apk.get_package() , and apk.get_app_name() to retrieve manifest data.
Analyzing classes and methods : the dex object represents each .dex file (multiple dex files may exist due to the 65 535‑method limit). You can list all classes, methods, fields, and strings.
Generating a method call graph using the dx analysis object:
classAnalysis = dx.classes['Lcom/example/songzekun/myapplication/MainActivity;']
for meth in classAnalysis.get_methods():
for _, call, _ in meth.get_xref_from():
print("from -> {} -- {}".format(call.class_name, call.name))
for _, call, _ in meth.get_xref_to():
print("to -> {} -- {}".format(call.class_name, call.name))Additional filtering can be applied to remove framework APIs and focus on application‑specific calls, as demonstrated by the screenClass helper function that isolates classes belonging to the target package.
Conclusion : By combining APK unpacking, Androguard metadata extraction, and dx‑based call‑graph construction, testers can perform comprehensive static scans, identify unused code, recommend precise test cases, and even assess security risks such as malicious permissions or hidden functionality.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.