1Building Android binary 2======================= 3 4In this article, we briefly describe how to build Android binary using 5`Android NDK <https://developer.android.com/ndk/index.html>`_ 6cross-compiler on Debian Linux. 7 8The easiest way to build android binary is use Dockerfile.android. 9See Dockerfile.android for more details. If you cannot use 10Dockerfile.android for whatever reason, continue to read the rest of 11this article. 12 13We offer ``android-config`` and ``android-make`` scripts to make the 14build easier. To make these script work, NDK toolchain must be 15installed in the following way. First, let us introduce 16``ANDROID_HOME`` environment variable. We need to install toolchain 17under ``$ANDROID_HOME/toolchain``. An user can freely choose the path 18for ``ANDROID_HOME``. For example, to install toolchain under 19``$ANDROID_HOME/toolchain``, do this in the the directory where NDK is 20unpacked: 21 22.. code-block:: text 23 24 $ build/tools/make_standalone_toolchain.py \ 25 --arch arm --api 16 --stl gnustl \ 26 --install-dir $ANDROID_HOME/toolchain 27 28The API level (``--api``) is not important here because we don't use 29Android specific C/C++ API. 30 31The dependent libraries, such as OpenSSL, libev, and c-ares should be 32built with the toolchain and installed under 33``$ANDROID_HOME/usr/local``. We recommend to build these libraries as 34static library to make the deployment easier. libxml2 support is 35currently disabled. 36 37Although zlib comes with Android NDK, it seems not to be a part of 38public API, so we have to built it for our own. That also provides us 39proper .pc file as a bonus. 40 41Before running ``android-config`` and ``android-make``, 42``ANDROID_HOME`` environment variable must be set to point to the 43correct path. Also add ``$ANDROID_HOME/toolchain/bin`` to ``PATH``: 44 45.. code-block:: text 46 47 $ export PATH=$PATH:$ANDROID_HOME/toolchain/bin 48 49To configure OpenSSL, use the following script: 50 51.. code-block:: sh 52 53 #!/bin/sh 54 55 if [ -z "$ANDROID_HOME" ]; then 56 echo 'No $ANDROID_HOME specified.' 57 exit 1 58 fi 59 PREFIX=$ANDROID_HOME/usr/local 60 TOOLCHAIN=$ANDROID_HOME/toolchain 61 PATH=$TOOLCHAIN/bin:$PATH 62 63 export CROSS_COMPILE=$TOOLCHAIN/bin/arm-linux-androideabi- 64 ./Configure --prefix=$PREFIX android 65 66And run ``make install_sw`` to build and install without 67documentation. 68 69We cannot compile libev without modification. Apply `this patch 70<https://gist.github.com/tatsuhiro-t/48c45f08950f587180ed>`_ before 71configuring libev. This patch is for libev-4.19. After applying the 72patch, to configure libev, use the following script: 73 74.. code-block:: sh 75 76 #!/bin/sh 77 78 if [ -z "$ANDROID_HOME" ]; then 79 echo 'No $ANDROID_HOME specified.' 80 exit 1 81 fi 82 PREFIX=$ANDROID_HOME/usr/local 83 TOOLCHAIN=$ANDROID_HOME/toolchain 84 PATH=$TOOLCHAIN/bin:$PATH 85 86 ./configure \ 87 --host=arm-linux-androideabi \ 88 --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \ 89 --prefix=$PREFIX \ 90 --disable-shared \ 91 --enable-static \ 92 CPPFLAGS=-I$PREFIX/include \ 93 LDFLAGS=-L$PREFIX/lib 94 95And run ``make install`` to build and install. 96 97To configure c-ares, use the following script: 98 99.. code-block:: sh 100 101 #!/bin/sh -e 102 103 if [ -z "$ANDROID_HOME" ]; then 104 echo 'No $ANDROID_HOME specified.' 105 exit 1 106 fi 107 PREFIX=$ANDROID_HOME/usr/local 108 TOOLCHAIN=$ANDROID_HOME/toolchain 109 PATH=$TOOLCHAIN/bin:$PATH 110 111 ./configure \ 112 --host=arm-linux-androideabi \ 113 --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \ 114 --prefix=$PREFIX \ 115 --disable-shared 116 117To configure zlib, use the following script: 118 119.. code-block:: sh 120 121 #!/bin/sh -e 122 123 if [ -z "$ANDROID_HOME" ]; then 124 echo 'No $ANDROID_HOME specified.' 125 exit 1 126 fi 127 PREFIX=$ANDROID_HOME/usr/local 128 TOOLCHAIN=$ANDROID_HOME/toolchain 129 PATH=$TOOLCHAIN/bin:$PATH 130 131 HOST=arm-linux-androideabi 132 133 CC=$HOST-gcc \ 134 AR=$HOST-ar \ 135 LD=$HOST-ld \ 136 RANLIB=$HOST-ranlib \ 137 STRIP=$HOST-strip \ 138 ./configure \ 139 --prefix=$PREFIX \ 140 --libdir=$PREFIX/lib \ 141 --includedir=$PREFIX/include \ 142 --static 143 144And run ``make install`` to build and install. 145 146After prerequisite libraries are prepared, run ``android-config`` and 147then ``android-make`` to compile nghttp2 source files. 148 149If all went well, application binaries, such as nghttpx, are created 150under src directory. Strip debugging information from the binary 151using the following command: 152 153.. code-block:: text 154 155 $ arm-linux-androideabi-strip src/nghttpx 156