Skip to content

Instantly share code, notes, and snippets.

@jsonfry
Last active September 8, 2023 19:45
Show Gist options
  • Save jsonfry/62d59c9cc0295cc8de4f to your computer and use it in GitHub Desktop.
Save jsonfry/62d59c9cc0295cc8de4f to your computer and use it in GitHub Desktop.
Connect to adb over a network. Useful for running jenkins in 'the cloud', but testing on real devices locally.

Connect your devices to a pi that has been configured like below.

Pi Setup

Download raspbian etc, go through normal setup.

Setup networking

This will let you find your pi locally using it's name, e.g. ping davepi.local

sudo apt-get update
sudo apt-get install avahi-daemon

Set a static ip

sudo nano /etc/network/interfaces

Then do something clever in here. Aftwards, make sure your ports are forwarded in your router, so the pi can be seen from the outside world.

Compile adb

Because you can't download adb for the pi.

Your development server adb will need to be the same version as the pi adb.

sudo apt-get install git build-essential libncurses5-dev libssl-dev
mkdir aosp
cd aosp
git clone https://android.googlesource.com/platform/system/core.git system/core
git clone https://android.googlesource.com/platform/external/zlib.git external/zlib

You might need to checkout a speific brach, tag, or commit here. See this page for branches / tags https://android.googlesource.com/platform/system/core/

cd system/core/adb
wget -O Makefile https://gist.github.com/jsonfry/62d59c9cc0295cc8de4f/raw/405031a85521364f3b04cfac435768d6bbc39408/Makefile
make adb
sudo cp adb /usr/local/bin/

Set correct permissions on adb

sudo chown root:mylovelyusername /usr/local/bin/adb
sudo chmod 4550 /usr/local/bin/adb

SSH Login

mkdir ~/.ssh
nano ~/.ssh/authorized_keys

Run cat ~/.ssh/id_rsa.pub on your development machine / ci server / who ever will be connecting to the pi and copy output into the open file

# Kill and local adb
adb kill-server
# Forward adb port
ssh -f user@example.com -L 5037:localhost:5037 -N
# Make sure remote adb server is running, and wait for it to start
ssh user@example.com "adb start-server"
# List the devices, just for fun!
adb devices
# Uninstall your app
adb devices | tail -n +2 | awk '{print $1}' | xargs -I {} adb -s {} uninstall com.example.app
# Run the tests, but still exit with success so jenkins carries on
./gradlew connectedAndroidTest lintDebug | true
# Grab the exit code of the tests, so we can fail the build later if we need to
export MY_EXIT=${PIPESTATUS[0]}
# Close the ssh tunnel
ps aux | grep "ssh -f" | grep "5037:" | awk '{print $2}' | xargs kill
# Fail the build, if the tests failed
(exit $MY_EXIT)
# Taken from this lovely post http://forum.xda-developers.com/showpost.php?p=55619695&postcount=70
SRCS+= adb.c
SRCS+= adb_client.c
SRCS+= commandline.c
SRCS+= console.c
SRCS+= file_sync_client.c
SRCS+= fdevent.c
SRCS+= get_my_path_linux.c
SRCS+= services.c
SRCS+= sockets.c
SRCS+= transport.c
SRCS+= transport_local.c
SRCS+= transport_usb.c
SRCS+= usb_linux.c
SRCS+= usb_vendors.c
SRCS+= adb_auth_host.c
VPATH+= ../libcutils
SRCS+= socket_inaddr_any_server.c
SRCS+= socket_local_client.c
SRCS+= socket_local_server.c
SRCS+= socket_loopback_client.c
SRCS+= socket_loopback_server.c
SRCS+= socket_network_client.c
SRCS+= load_file.c
VPATH+= ../libzipfile
SRCS+= centraldir.c
SRCS+= zipfile.c
VPATH+= ../../../external/zlib/src
SRCS+= adler32.c
SRCS+= compress.c
SRCS+= crc32.c
SRCS+= deflate.c
SRCS+= infback.c
SRCS+= inffast.c
SRCS+= inflate.c
SRCS+= inftrees.c
SRCS+= trees.c
SRCS+= uncompr.c
SRCS+= zutil.c
CPPFLAGS+= -DADB_HOST=1
CPPFLAGS+= -DHAVE_FORKEXEC=1
CPPFLAGS+= -DHAVE_SYMLINKS
CPPFLAGS+= -DHAVE_TERMIO_H
CPPFLAGS+= -DHAVE_OFF64_T
CPPFLAGS+= -D_GNU_SOURCE
CPPFLAGS+= -D_XOPEN_SOURCE
CPPFLAGS+= -DWORKAROUND_BUG6558362
CPPFLAGS+= -I.
CPPFLAGS+= -I../include
CPPFLAGS+= -I../../../external/zlib
CFLAGS+= -O2 -g -Wall -Wno-unused-parameter
#LDFLAGS= -static
LIBS= -lrt -lpthread -lcrypto -lssl
TOOLCHAIN=
CC= $(TOOLCHAIN)gcc
LD= $(TOOLCHAIN)gcc
OBJS= $(SRCS:.c=.o)
all: adb
adb: $(OBJS)
$(LD) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
clean:
rm -rf $(OBJS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment