Skip to content

Instantly share code, notes, and snippets.

@bastionkid
Last active June 16, 2023 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastionkid/d3cc8cba46c41cdb329b7faa5158144c to your computer and use it in GitHub Desktop.
Save bastionkid/d3cc8cba46c41cdb329b7faa5158144c to your computer and use it in GitHub Desktop.
compare_apks.py - Generate HTML report phase
# generate html file containing size diff in HRF
def generate_size_diff_html():
html = "<html>"
html += "<body><h1>Download Size Diff Report</h1><h3>Affected Products</h3>"
html += "<ul><li><h4><code>release</code></h4><table>"
html += f"<tr><th>Component</th><th>Base ({apk_1_sha})</th><th>Merge ({apk_2_sha})</th><th>Diff</th></tr>"
# print diff of each components of both of the apk files
for component in set(components_1.keys()) | set(components_2.keys()):
size_1 = components_1.get(component, 0)
size_2 = components_2.get(component, 0)
html += f"<tr><td>{component}</td><td>{format_size(size_1)}</td><td>{format_size(size_2)}</td><td>{format_size_with_indicator(size_2 - size_1)}</td></tr>"
# calculate size of the apk files
apk_1_download_size = apk_size(apk_1_name, 'download-size')
apk_2_download_size = apk_size(apk_2_name, 'download-size')
html += f"<tr><td>apk (Download Size)</td><td>{format_size(apk_1_download_size)}</td><td>{format_size(apk_2_download_size)}</td><td>{format_size_with_indicator(apk_2_download_size - apk_1_download_size)}</td></tr>"
html += "</li></ul></table></body></html>"
with open("apk_size_diff_report.html", "w") as file:
file.write(html)
# format bytes to KB or MB. Any size less than a KB is treated as 0KB
def format_size(size):
if abs(size) > mb_in_bytes:
return f"{round(size / mb_in_bytes, 2)} MB"
elif abs(size) > kb_in_bytes:
return f"{round(size / kb_in_bytes, 2)} KB"
else:
return "0 KB"
# add an indicator to highlight the size diff
def format_size_with_indicator(size):
size_indicator = "🔴" if size > kb_in_bytes else "🟢"
return f"{format_size(size)} {size_indicator}"
# get apk size based on size_type i.e. file-size or download-size
def apk_size(apk_file, size_type):
command = f"{apk_analyzer_path} apk {size_type} {apk_file}"
return int(execute_command(command))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment