Skip to content

Instantly share code, notes, and snippets.

@MasonFlint44
Created March 5, 2023 19:27
Show Gist options
  • Select an option

  • Save MasonFlint44/4b32d86da40f79d12355bb993fe23953 to your computer and use it in GitHub Desktop.

Select an option

Save MasonFlint44/4b32d86da40f79d12355bb993fe23953 to your computer and use it in GitHub Desktop.
Install split APKs on Android with ADB

Install split APKs on Android with ADB

These two scripts allow you to install split APK files on an Android device using ADB (Android Debug Bridge).

Prerequisites

Before you can use these scripts, you'll need the following:

  • An Android emulator or device
  • ADB installed
  • The split APK files to be installed

Usage

  1. Copy the install_apks.sh and adb-install-split-apks.sh scripts to a directory on your computer.

  2. Open a terminal and navigate to the directory where the scripts are located.

  3. Use the install_apks.sh script to install the split APK files on the emulator:

    ./install_apks.sh <path_to_apk_directory>

    Replace <path_to_apk_directory> with the path to the directory containing the split APK files you want to install. Note that the directory should not contain any APK files that do not belong to the split APK bundle to be installed.

Troubleshooting

If you encounter permission errors when running the adb-install-split-apks.sh script, you may need to fix the permissions using the chmod +x command:

chmod +x adb-install-split-apks.sh

If you encounter errors when pushing the APK files or the adb-install-split-apks.sh script to the emulator, make sure that the emulator is running and that ADB is connected to the emulator. You can check if ADB is connected to the emulator using the adb devices command.

#!/bin/sh
# Calculate the total size of all APKs
TOTAL_SIZE_ALL_APKS=0
for APK_PATH in "$@"; do
APK_SIZE=$(wc -c < "$APK_PATH") # get the size of the APK in bytes
TOTAL_SIZE_ALL_APKS=$((TOTAL_SIZE_ALL_APKS + APK_SIZE)) # add the APK size to the total size
done
# Create an install session
SESSION_ID=$(pm install-create -S $TOTAL_SIZE_ALL_APKS | sed -n -e 's/.*\[\(.*\)\]/\1/p')
# Loop through each APK and install it
INDEX=0
for APK_PATH in "$@"; do
APK_SIZE=$(wc -c < "$APK_PATH") # get the size of the APK in bytes
pm install-write -S $APK_SIZE $SESSION_ID $INDEX $APK_PATH # install the APK
INDEX=$((INDEX+1))
done
# Commit the install session
pm install-commit $SESSION_ID
#!/bin/bash
# Check if the input directory exists
if [ ! -d "$1" ]; then
echo "Error: Directory '$1' does not exist."
exit 1
fi
# Push the APK files to the emulator
for APK_PATH in "$1"/*.apk; do
adb push "$APK_PATH" /data/local/tmp/
done
# Push the adb-install-split-apks.sh script to the emulator
adb push adb-install-split-apks.sh /data/local/tmp/
# Get the APK filenames in the input directory
APK_FILENAMES=""
for APK_PATH in "$1"/*.apk; do
APK_FILENAME=$(basename "$APK_PATH")
APK_FILENAMES="$APK_FILENAMES $APK_FILENAME"
done
# Open an adb shell
adb shell <<EOF
# Change to the /data/local/tmp directory
cd /data/local/tmp/
# Fix permissions on adb-install-split-apks.sh
chmod +x adb-install-split-apks.sh
# Execute the adb-install-split-apks.sh script with the APK filenames
./adb-install-split-apks.sh $APK_FILENAMES
# Exit the adb shell
exit
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment