Skip to content

Instantly share code, notes, and snippets.

@spvkgn
Last active September 5, 2024 09:23
Show Gist options
  • Save spvkgn/6edf51452ddfb87f8769375e0b74909b to your computer and use it in GitHub Desktop.
Save spvkgn/6edf51452ddfb87f8769375e0b74909b to your computer and use it in GitHub Desktop.
Install split APK files (App Bundles) using ADB with setting the installer to Google Play
#!/usr/bin/env python3
import os
from subprocess import run, PIPE
def get_apk_paths(folder_path):
"""Get the list of APK paths from the specified folder."""
apk_paths = []
for file_name in sorted(os.listdir(folder_path)):
if file_name.endswith('.apk'):
apk_paths.append(os.path.join(folder_path, file_name))
return apk_paths
def install_apks(folder_name, apk_paths):
"""Install split APKs on the device."""
total_size = sum(os.path.getsize(apk_path) for apk_path in apk_paths)
print(f'Size of APKs: {total_size} bytes')
# Start a new install session
result = run(['adb', 'shell', 'pm', 'install-create', '-i', 'com.android.vending', '--user', '0', '-S', str(total_size)],
capture_output=True, text=True, check=True)
# Extract session ID and remove non-numeric characters
session_id = ''.join(filter(str.isdigit, result.stdout.strip()))
# Stage each APK
for index, apk_path in enumerate(apk_paths):
apk_size = os.path.getsize(apk_path)
with open(apk_path, 'rb') as apk_data:
print(f'Installing {apk_path}')
if run(['adb', 'shell', 'pm', 'install-write', '-S', str(apk_size), session_id, str(index), '-'],
input=apk_data.read(), stderr=PIPE
).returncode != 0:
run(['adb', 'shell', 'pm', 'install-abandon', session_id], check=True)
raise Exception(f'Invalid result: {PIPE}')
# Commit the installation
run(['adb', 'shell', 'pm', 'install-commit', session_id], check=True)
def main():
folder_name = input('Input folder with APKs [default: current]: ')
while len(folder_name.strip()) == 0:
folder_name = '.'
if not os.path.isdir(folder_name):
print(f'Folder {folder_name} is not exist')
return
apk_paths = get_apk_paths(folder_name)
if not apk_paths:
print(f'No APK files found in folder {folder_name}')
return
install_apks(folder_name, apk_paths)
print('Finished installing APKs.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment