Android application repackaging Automation using python

Kapil Verma
2 min readFeb 24, 2023

Copy and paste the below script and sabve it as a py file and then execute.

There are some modifications that you might require to do are like giving path to the apktool, keytool , jarsigner, zipalign in the script,

Also there are some pre requisites that need to be there, like java jdk, and jre latest, apktool installed etc.

Also, I have prepared this script for windows host only, so you can try running this on windows PC or else modify according to you Base OS.

import os
import subprocess

apk_filename = input(“Enter APK file name: “)

# Check if the APK file exists
if not os.path.isfile(apk_filename):
print(f”File ‘{apk_filename}’ does not exist.”)
exit()

# Run apktool to decompile the APK
print(f”Decompiling ‘{apk_filename}’ with apktool…”)
subprocess.run([“C:\Windows\/apktool.bat”, “-f”, “d”, apk_filename])

# Get the name of the decompiled folder
apk_folder = os.path.splitext(apk_filename)[0]
print(f”Building ‘{apk_folder}’ with apktool…”)

# Run apktool to build the APK
subprocess.run([“C:\Windows\/apktool.bat”, “b”, apk_folder])

# Check if the keystore file already exists with the same name
keystore_file = “mycustomname.keystore”
if os.path.isfile(keystore_file):
subprocess.run([“rm”, keystore_file])

# Generate the signing key
print(“Generating signing key…”)
subprocess.run([“C:\Program Files\Java\jdk-19\/bin\keytool.exe”, “-genkey”, “-v”, “-keystore”, “mycustomname.keystore”, “-alias”, “test”, “-keyalg”, “RSA”, “-keysize”, “2048”, “-validity”, “10000”, “-storepass”, “password”, “-keypass”, “password”])

# Get the name of the APK file from dist folder
dist_folder = os.path.join(apk_folder, “dist”)
apk_file = os.path.join(dist_folder, os.listdir(dist_folder)[0])

# Sign the APK using jarsigner
print(f”Signing ‘{apk_file}’ with jarsigner…”)
subprocess.run([“C:\Program Files\Java\jdk-19\/bin\/jarsigner.exe”, “-verbose”, “-sigalg”, “SHA1withRSA”, “-digestalg”, “SHA1”, “-keystore”, keystore_file, apk_file, “test”, “-storepass”, “password”])

# Get the name of the resigned APK file
resigned_apk_file = os.path.splitext(apk_file)[0] + “_resigned.apk”

# Zipalign the signed APK file
print(f”Zipaligning ‘{apk_file}’ with zipalign…”)
subprocess.run([“C:/Users/((((USER_NAME))))/AppData/Local/Android/Sdk/build-tools/33.0.0/zipalign.exe”, “-v”, “4”, apk_file, resigned_apk_file])

# Install the APK to Nox Player using adb
print(“Installing APK to Nox Player…”)
subprocess.run([“adb”, “-s”, “127.0.0.1:62001”, “install”, resigned_apk_file])

# Print the path and file name of the resigned APK file
print(f”Resigned APK file saved at ‘{resigned_apk_file}’.”)

print(“APK build, signing, and installation process completed.”)

--

--