Skip to content

Instantly share code, notes, and snippets.

@bastionkid
Last active June 15, 2023 07:29
Show Gist options
  • Save bastionkid/9b6c880f583d515ea0e775d55ae24feb to your computer and use it in GitHub Desktop.
Save bastionkid/9b6c880f583d515ea0e775d55ae24feb to your computer and use it in GitHub Desktop.
compare_apks.py - Dictionary Build phase
# generate dictionary of the grouped contents of an apk file
def get_apk_components(apk_file, size_type):
command = f"{apk_analyzer_path} files list --{size_type} {apk_file}"
files_with_size_string = execute_command(command)
files_with_size_list = files_with_size_string.split('\n')
components = {}
for item in files_with_size_list:
size_and_file_name = item.split('\t')
# this will filter out empty lines and just the lines with size and no file name
if len(size_and_file_name) == 2 and len(size_and_file_name[1]) > 1:
size = int(size_and_file_name[0])
file_name = size_and_file_name[1]
if file_name == '/lib/arm64-v8a/':
update_if_present(components, 'Native libraries (arm64-v8a)', size)
elif file_name.startswith('/classes') and file_name.endswith('.dex'):
update_if_present(components, 'Classes', size)
elif file_name == '/resources.arsc' or file_name == '/res/':
update_if_present(components, 'Resources', size)
elif file_name == '/assets/':
update_if_present(components, 'Assets', size)
elif not file_name.startswith('/lib/') and not file_name.startswith('/classes') and not file_name.startswith('/resources.arsc') and not file_name.startswith('/res/') and not file_name.startswith('/assets/') and not file_name.endswith('/'):
update_if_present(components, 'Others', size)
return components
# shell command executor
def execute_command(command):
# Run the command using subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
return output.decode()
# add/update the value (i.e. size) based on the presence of provided key
def update_if_present(components, key, value):
if key in components:
components[key] = components[key] + value
else:
components[key] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment