Created
December 10, 2024 05:57
-
-
Save mauro1998/332c42e5f93542dc04249ae727efc7fd to your computer and use it in GitHub Desktop.
Android ADB Reverse Port Mapping Script: A bash script that automatically sets up reverse port mapping between an Android device/emulator and your local development machine. Useful for mobile app development when you need your Android app to communicate with a locally running development server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script sets up reverse port mapping between an Android device/emulator and the local development machine. | |
# It is used to allow the Android app to connect to a local development server running on the host machine. | |
# | |
# When running the app in development mode on an Android device/emulator, the app tries to connect to | |
# localhost:$PORT. However, 'localhost' on the device refers to the device itself, not the development | |
# machine. This script uses ADB (Android Debug Bridge) to set up reverse port mapping so that when the | |
# app tries to connect to localhost:$PORT on the device, the connection is reversed back to port $PORT | |
# on the development machine where the actual server is running. | |
# | |
# Usage: | |
# 1. Make sure ADB is installed and in your system PATH | |
# 2. Connect an Android device via USB or start an emulator | |
# 3. Run this script: ./adb_reverse_port.sh | |
# 4. The script will automatically detect connected devices and set up reverse port mapping | |
# | |
# Note: If multiple Android devices/emulators are connected, this script will | |
# prompt you to select which device to use for port mapping. This ensures the | |
# correct device is configured when working with multiple test devices. | |
# Port number for reverse mapping between local machine and Android device/emulator | |
PORT=3000 | |
# Check if adb command exists in path | |
if ! command -v adb &> /dev/null; then | |
echo -e "\033[31mError: 'adb' command not found. Please add Android Debug Bridge (adb) to your system PATH\033[0m" | |
exit 1 | |
fi | |
# Get list of devices and remove header and empty lines | |
DEVICES=($(adb devices | grep -v "List" | cut -f 1 | grep -v '^$')) | |
DEVICE_COUNT=${#DEVICES[@]} | |
if [ $DEVICE_COUNT -eq 0 ]; then | |
echo -e "\033[31mNo devices connected!\033[0m" | |
exit 1 | |
elif [ $DEVICE_COUNT -eq 1 ]; then | |
SELECTED_DEVICE=${DEVICES[0]} | |
echo -e "\033[32mAutomatically selected only connected device: $SELECTED_DEVICE\033[0m" | |
else | |
echo -e "\033[33mMultiple devices found. Please select a device:\033[0m" | |
for i in "${!DEVICES[@]}"; do | |
echo "[$i] ${DEVICES[$i]}" | |
done | |
read -p "Enter the number of the device to use: " DEVICE_INDEX | |
if [[ ! $DEVICE_INDEX =~ ^[0-9]+$ ]] || [ $DEVICE_INDEX -ge $DEVICE_COUNT ]; then | |
echo -e "\033[31mInvalid selection!\033[0m" | |
exit 1 | |
fi | |
SELECTED_DEVICE=${DEVICES[$DEVICE_INDEX]} | |
fi | |
# Show current reverse port mappings | |
REVERSE_LIST=$(adb -s $SELECTED_DEVICE reverse --list) | |
echo -e "\033[36mCurrent reverse port mappings for device $SELECTED_DEVICE:\033[0m" | |
if [ -z "$REVERSE_LIST" ]; then | |
echo -e "\033[90mNo existing port mappings\033[0m" | |
else | |
echo "$REVERSE_LIST" | |
fi | |
echo "" | |
# Only set up reverse port mapping if it doesn't already exist | |
if ! echo "$REVERSE_LIST" | grep -q "tcp:$PORT tcp:$PORT"; then | |
echo -e "\033[33mSetting up reverse port mapping for device $SELECTED_DEVICE...\033[0m" | |
if adb -s $SELECTED_DEVICE reverse tcp:$PORT tcp:$PORT > /dev/null; then | |
echo -e "\033[32mReverse port mapping set up - device $SELECTED_DEVICE will connect to localhost:$PORT\033[0m" | |
else | |
echo -e "\033[31mFailed to set up port mapping\033[0m" | |
exit 1 | |
fi | |
else | |
echo -e "\033[32mPort mapping already exists for port $PORT\033[0m" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment