Skip to content

Instantly share code, notes, and snippets.

@Hattshire
Created March 17, 2019 17:21
Show Gist options
  • Save Hattshire/16bab493a5c8fc40b9dfb44f174039e0 to your computer and use it in GitHub Desktop.
Save Hattshire/16bab493a5c8fc40b9dfb44f174039e0 to your computer and use it in GitHub Desktop.
List used classes on an apktool decompiled application
#!/usr/bin/env python3
# Public Domain
#
# Lists used classes on a apktool decompiled application
# It outputs:
# to stderr provided/compiled classes
# to stdout imported/system classes
# Don't play with it too much, it's a bit shy
# UwU
import re
import os
import sys
## Extract classes from smali file
# Args:
# filename@str: the path of the source file
#
# Returns:
# set(EMPTY): No class found on file
# set(str): Contained unique classes
def extract_classes_from(filename):
extracted_classes=set()
with open(filename, 'r') as smali_file:
get_classes=re.compile(r'\sL([\w/]+);').findall
for line in smali_file:
extracted_classes = extracted_classes.union(
get_classes(line)
)
return extracted_classes
## Extracts classes recurring the files of a directory
# Args:
# directory@str: path to recurse on
#
# Returns:
# set(EMPTY): No class found in directory, or unsupported smali extension
# set(str): Unique classes contained in files
def find_all_classes_in(directory):
classes = set()
for root, _, files in os.walk(directory):
for filename in files:
filename = os.path.join(root, filename)
if ".smali" in os.path.splitext(filename):
classes = classes.union(
extract_classes_from(filename)
)
return classes
if __name__ == "__main__":
classes = find_all_classes_in(os.getcwd())
provided_classes = set()
print_stderr = sys.stderr.write
print_stdout = sys.stdout.write
# Print provided classes
for smali_class in classes:
for directory in os.listdir():
if "smali" in directory:
if os.path.exists(os.path.join(directory, smali_class + ".smali")):
provided_classes.add(smali_class)
print_stderr(smali_class+', ')
# Print imported classes
classes = classes.difference(provided_classes)
print_stdout(', '.join('{}'.format(smali_class) for smali_class in classes))
# Di end
# Written by Hattshire in Kakoune with <3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment