Aapo Rantalainen's blog

Experiences with Information Technology and Open source

Cross-compiling Adb for Windows

Posted by Aapo Rantalainen on December 5, 2015

Continuing with same theme than previous posts. Adb can be compiled with Linux for Windows (Cross-compiling).

Context

Adb (Android Debug Bridge) is debugging tool to communicate with Android-device and host. Developer can install whole Android SDK and get adb. But I think it is handy tool for (power-)users too. Personally I think that developing with Linux is easier than with Windows, so I made step-by-step tutorial how to cross-compile adb for Windows using Ubuntu.

How to

This is not the newest version of Adb (because at that point it will start using source code from other part of SDK, and a little bit earlier merging from c to c++ which complicates things too much). Code is somewhere between Android-5.1.1_r29 and Android-6.0.0_r1.

#Needed tools
sudo apt-get install  mingw-w64 git make
 
#Download source code (about 32MB, 2015-12-05)
git clone https://android.googlesource.com/platform/system/core
cd core/adb

#Change last known working commit ( This was made 2015-03-09)
git checkout 9b1fd969a7b7f1c6f1ed19719f21d57001d3c461

#Download some precompiled DLL's and one header from git
wget https://android.googlesource.com/platform/development/+/android-6.0.0_r1/host/windows/prebuilt/usb/AdbWinApi.dll?format=TEXT
base64 -d AdbWinApi.dll\?format\=TEXT > AdbWinApi.dll

wget https://android.googlesource.com/platform/development/+/android-6.0.0_r1/host/windows/prebuilt/usb/AdbWinUsbApi.dll?format=TEXT
base64 -d AdbWinUsbApi.dll?format=TEXT > AdbWinUsbApi.dll

wget https://android.googlesource.com/platform/development/+/android-6.0.0_r1/host/windows/usb/api/adb_api.h?format=TEXT
base64 -d adb_api.h?format=TEXT > adb_api.h

#Compile external libraries
#OPENSSL (latest release (2015-12-05))
wget https://www.openssl.org/source/openssl-1.0.2e.tar.gz
tar xvf openssl-1.0.2e.tar.gz
mv openssl-1.0.2e openssl
cd openssl
CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CPP=i686-w64-mingw32-cpp RANLIB=i686-w64-mingw32-ranlib ./Configure mingw
make
cd ..

#ZLIB (latest release (2015-12-05))
wget http://zlib.net/zlib-1.2.8.tar.gz
tar xvf zlib-1.2.8.tar.gz
mv zlib-1.2.8 zlib
cd zlib
sed 's/--lc//' -i configure     #One fix for configure
CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CPP=i686-w64-mingw32-cpp RANLIB=i686-w64-mingw32-ranlib  ./configure 
make
cd ..
#Create Makefile
echo -e '
# standalone Makefile for adb for windows
SRCS+= adb.c
SRCS+= adb_main.c
SRCS+= sysdeps_win32.c    #replaces fdevent.c 
SRCS+= adb_client.c
SRCS+= commandline.c
SRCS+= console.c
SRCS+= file_sync_client.c
SRCS+= get_my_path_windows.c    #replaces get_my_path_linux.c
SRCS+= services.c
SRCS+= sockets.c
SRCS+= transport.c
SRCS+= transport_local.c
SRCS+= transport_usb.c
SRCS+= usb_windows.c       #replaces  usb_linux.c
SRCS+= adb_auth_host.c
SRCS+= adb_auth.c
SRCS+= adb_listeners.c
 
#VPATH+= ../libcutils  #not used, because of USE_SYSDEPS_WIN32

SRCS+= ../libzipfile/centraldir.c
SRCS+= ../libzipfile/zipfile.c


CPPFLAGS+= -DADB_HOST=1
CPPFLAGS+= -DHAVE_FORKEXEC=1
#CPPFLAGS+= -DHAVE_SYMLINKS    #readlink will fail
#CPPFLAGS+= -DHAVE_TERMIO_H    #No concept of tty in Windows
CPPFLAGS+= -DHAVE_SYS_SOCKET_H
CPPFLAGS+= -DHAVE_OFF64_T
CPPFLAGS+= -D_GNU_SOURCE
CPPFLAGS+= -D_XOPEN_SOURCE

#Added for Windows
CPPFLAGS+= -DHAVE_WINSOCK
CPPFLAGS+= -D_WIN32
CPPFLAGS+= -DHAVE_WIN32_IPC
CPPFLAGS+= -DHAVE_WIN32_PROC

CPPFLAGS+= -I.
CPPFLAGS+= -I../include

#Locally compiled
CPPFLAGS+= -Iopenssl/include/
LDFLAGS+= -Lopenssl/
CPPFLAGS+= -Izlib/
LDFLAGS+= -Lzlib/

#Precompiled
LIBS+= AdbWinUsbApi.dll
LIBS+= AdbWinApi.dll

LIBS+= -lcrypto -lpthread -lz  #lrt dropped, not used in Windows
LIBS+= -lws2_32 -lgdi32

CC= i686-w64-mingw32-gcc
LD= i686-w64-mingw32-gcc
CXX= i686-w64-mingw32-g++

CXXFLAGS+= $(CPPFLAGS)

OBJS= $(SRCS:.c=.o)

all: adb.exe
 
adb.exe: $(OBJS)
\t$(CXX) $(CXXFLAGS) -c -o adb_io.o adb_io.cpp
\t$(LD) -o $@ $(LDFLAGS) $(OBJS) adb_io.o  $(LIBS)
\ti686-w64-mingw32-strip $@
 
clean:
\trm -rf *.o adb.exe
' > standalone_Makefile

#Compile
make -f standalone_Makefile

#Move adb.exe to the Windows and profit.

Credits

Even I based this tutorial on my last blog-post, this old project gave much good extra information: https://code.google.com/p/mingw-and-ndk/

2 Responses to “Cross-compiling Adb for Windows”

  1. wazee said

    Someone Give this guy a medal . Wonderful post man . Really saved my life here .

  2. wazee said

    And here I go ; i found a lot of dependent header file error . Really sad right now .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: