Skip to content

Instantly share code, notes, and snippets.

@timrijckaert
Last active April 12, 2023 20:27
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 timrijckaert/63b0f6d5cd5a6c9a938495a6f8a6a4e4 to your computer and use it in GitHub Desktop.
Save timrijckaert/63b0f6d5cd5a6c9a938495a6f8a6a4e4 to your computer and use it in GitHub Desktop.
Download a signed APK from Google Play with Python's Android Publisher.

What

Download a signed APK from Google Play with Python's Android Publisher.
This can be helpful when you upload an app bundle that is signed by Google

How to run?

Download your apps service-account.json to the same directory as the Python script.

Download the dependencies

pip install -r requirements.txt

Run the script to download the APK for a specified packagename

python3 apk_downloader.py "be.vmma.vtm.zenderapp"
import os
import sys
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Script that downloads the latest signed production APK for a published app.
#
# Example
# python3 apk_downloader.py be.vmma.vtm.zenderapp
TRACK_NAME = 'production'
PACKAGE_NAME = sys.argv[1]
# Set up the service account credentials
# Ask your most trusted Android developer. Tip it is stored in Bitrise under code signing and files.
credentials = service_account.Credentials.from_service_account_file('service-account.json')
# Set up the Android Publisher API client
service = build('androidpublisher', 'v3', credentials=credentials)
# Get a new EDIT ID
edit_service = service.edits()
edit_id = edit_service.insert(packageName=PACKAGE_NAME).execute()["id"]
print(f"New edit id {edit_id}")
# Get a track by PACKAGE_NAME + EDIT ID + TRACK NAME
track_service = edit_service.tracks()
track = track_service.get(packageName=PACKAGE_NAME, editId=edit_id, track=TRACK_NAME).execute()
track_name = track["track"]
print(f"Found track '{track_name}'")
# Get VERSION_CODE in TRACK
latest_release = track["releases"][0]
version_code = latest_release["versionCodes"][0]
track_version_name = latest_release["name"]
print(f"{track_version_name}_{version_code} is active in '{track_name}'")
# Get the DOWNLOAD_ID for the PACKAGE + VERSION_CODE
generated_apk_service = service.generatedapks()
download_id = generated_apk_service.list(
packageName=PACKAGE_NAME,
versionCode=version_code
).execute()["generatedApks"][0]["generatedUniversalApk"]["downloadId"]
print(f"Found downloadID: {download_id}")
# Download APK from PACKAGE_NAME + VERSION_CODE + DOWNLOAD_ID
output_file_name = f"{PACKAGE_NAME}-v{track_version_name}_{version_code}.apk"
output_file_path = os.path.join(os.getcwd(), output_file_name)
apk_file_stream = generated_apk_service.download_media(
packageName=PACKAGE_NAME,
versionCode=version_code,
downloadId=download_id
).execute()
with open(output_file_path, 'wb') as f:
f.write(apk_file_stream)
print(f"APK saved to {output_file_path}")
google-api-python-client>=2.85.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment