Skip to content

Instantly share code, notes, and snippets.

@skibitsky
Created May 24, 2023 03:49
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 skibitsky/efd5b556acf4646d9c5456026f203de1 to your computer and use it in GitHub Desktop.
Save skibitsky/efd5b556acf4646d9c5456026f203de1 to your computer and use it in GitHub Desktop.
This script sideloads an APK and its corresponding OBB file to an Android device.
#!/bin/sh
# This script installs an APK and its corresponding OBB file to an Android device.
# It requires the path to the directory containing the APK and OBB file(s) as an input parameter.
# The script will find the APK and the OBB file with the highest version number in the directory,
# then install the APK and upload the OBB file to the appropriate location on the device.
# It also handles uninstalling the existing APK, creating the OBB directory if necessary,
# and removing any existing OBB files for the APK.
#
# The script relies on Android Debug Bridge (adb) to communicate with the device.
# Ensure that adb is installed and set up correctly on your system before using this script.
#
# Usage:
# ./sideload.sh /path/to/directory
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if directory path is passed
if [ "$#" -ne 1 ]; then
echo "${RED}Error: You must enter the path to the build directory as a parameter.${NC}"
exit 1
fi
# Directory path
DIR_PATH="$1"
# Get the APK file
APK_FILE_PATH=$(find "$DIR_PATH" -type f -name "*.apk")
if [ -z "$APK_FILE_PATH" ]; then
echo "${RED}Error: No APK file found in the build directory.${NC}"
exit 1
fi
echo "${BLUE}APK file found: $APK_FILE_PATH${NC}"
# Get the OBB files and select the one with the highest version number
OBB_FILE_PATHS=$(find "$DIR_PATH" -type f -name "*.obb" | sort -t . -k2 -n -r)
OBB_FILE_PATH=$(echo "$OBB_FILE_PATHS" | head -n 1)
if [ -z "$OBB_FILE_PATH" ]; then
echo "${RED}Error: No OBB file found in the build directory.${NC}"
exit 1
fi
echo "${BLUE}OBB file found: $OBB_FILE_PATH${NC}"
# Extract the package name from the OBB file name
PACKAGE_NAME=$(basename "$OBB_FILE_PATH" | sed -n 's/main\.\([0-9]*\)\.\(.*\)\.obb/\2/p')
if [ -z "$PACKAGE_NAME" ]; then
echo "${RED}Error: Unable to extract package name from OBB file.${NC}"
exit 1
fi
echo "${BLUE}Package name: $PACKAGE_NAME${NC}"
# Uninstall existing app if it exists
echo "${GREEN}Uninstalling existing app with package name $PACKAGE_NAME...${NC}"
adb uninstall "$PACKAGE_NAME"
# Install the APK
echo "${GREEN}Installing APK...${NC}"
adb install "$APK_FILE_PATH" || { echo "${RED}Error: Failed to install APK.${NC}"; exit 1; }
# Wait for the device to be available
adb wait-for-device
# Define the OBB destination path
DEST_OBB_DIR="/sdcard/Android/obb/$PACKAGE_NAME"
# Create OBB directory if it does not exist
echo "${GREEN}Creating OBB directory at $DEST_OBB_DIR...${NC}"
adb shell mkdir -p "$DEST_OBB_DIR"
# Remove existing OBB files if they exist
echo "${GREEN}Removing existing OBB files at $DEST_OBB_DIR...${NC}"
adb shell rm "$DEST_OBB_DIR"/*.obb
# Define the destination OBB file path
DEST_OBB_FILE_PATH="$DEST_OBB_DIR/$(basename "$OBB_FILE_PATH")"
echo "${GREEN}Transferring OBB file to $DEST_OBB_FILE_PATH...${NC}"
adb push "$OBB_FILE_PATH" "$DEST_OBB_FILE_PATH" || { echo "${RED}Error: Failed to transfer OBB file.${NC}"; exit 1; }
echo "${GREEN}APK and OBB files have been successfully installed.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment