Android Setup (Command Line)

From Crypto++ Wiki
Jump to navigation Jump to search

This page will show you how to setup a build machine to test Android. They are the same steps the Crypto++ project uses when setting-up for Android. They are implicitly used in wiki articles like Android (Command Line) and Android.mk. The instructions should help with questions that arise when building for Android.

There are three steps to the process. First, an NDK is installed in /opt. Second, the SDK is installed in /opt. Third, environmental variables like ANDROID_NDK_ROOT and ANDROID_SDK_ROOT are set. Once ANDROID_NDK_ROOT, ANDROID_SDK_ROOT and ANDROID_HOME are set you should be ready to build the library.

The steps below were performed on a Debian Stretch x86_64 machine because it was handy. The machine will have to meet minimum requirements, like Java 8 installed. The instructions also assume the Bash shell. If you don't use Bash then you will have to modify things to suite your taste.

Each NDK needs about 2.5 to 3 GB of space, so be sure to have enough storage. You must use a 64-bit version of the NDK for Linux. The last version of the NDK to support 32-bit Linux was r10.

The process will mostly be the same for CentOS, Debian, OS X and Fedora. At the time of this writing, the latest NDK is r19c, and the latest SDK is revision 4333796. Android-28 is the API level available with NDK r19.

In your Android builds, you should always set ANDROID_NDK_ROOT and ANDROID_SDK_ROOT because the Android tools use them internally. Also see Recommended NDK Directory? on the Android NDK mailing list.

The SDK Manager no longer provides a GUI. If you want a GUI then see Is GUI for Android SDK manager gone? on Stack Overflow.

Android NDK

The first step to setup an Android build machine is install an NDK. The NDK download is available at Android Developers | NDK | Downloads. Below are the steps to perform the task. Note that the zip includes a directory structure with android-ndk-r19c, so the unzip only needs to use /opt.

$ curl -k -L -o android-ndk-r19c-linux-x86_64.zip https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
$ sudo unzip android-ndk-r19c-linux-x86_64.zip -d /opt

Next, check /opt.

$ ls -Al /opt/
lrwxrwxrwx  1 root root   17 Jan 20   2019 android-ndk -> android-ndk-r18b/
drwxr-xr-x 12 root root 4096 Dec  1   2017 android-ndk-r16b
drwxr-xr-x 12 root root 4096 Jun 27   2018 android-ndk-r17c
drwxr-xr-x 12 root root 4096 Jan 20   2019 android-ndk-r18b
drwxr-xr-x 13 root root 4096 Mar  1  00:45 android-ndk-r19c

Create or modify the android-ndk symlink as required.

$ sudo ln -s /opt/android-ndk-r19c/ /opt/android-ndk
$ ls -Al /opt/android-ndk
lrwxrwxrwx 1 root root 22 Apr 10 12:41 /opt/android-ndk -> /opt/android-ndk-r19c/

OS X users may need to attrib -r -d com.apple.quarantine /opt/android-ndk-r19c to remove quarantine bits.

Android SDK

The second step to setup an Android build machine is install the SDK. The SDK download is available at Android Developers | Android Studio | Downloads. Below are the steps to perform the task. Note that the zip includes a directory structure with tools, so the unzip needs to use /opt/android-sdk.

$ wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
$ sudo unzip sdk-tools-linux-4333796.zip -d /opt/android-sdk

OS X users may need to attrib -r -d com.apple.quarantine /opt/android-sdk to remove quarantine bits.

Executables in the SDK lack the execute bit, so your next step is to enable the permission.

sudo find /opt -type f -executable -exec chmod +x {} \;

With the SDK tools installed you can update the SDK and install APIs. Be sure to install build-tools. You can use sdkmanager --list to show available downloads.

$ sudo /opt/android-sdk/tools/bin/sdkmanager --update
$ sudo /opt/android-sdk/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"

The SDK Manager no longer provides a GUI. If you want a GUI then see Is GUI for Android SDK manager gone? on Stack Overflow.

Env Variables

The last step to setup an Android build machine is set or modify environmental variables and paths. The variables include ANDROID_NDK_ROOT, ANDROID_SDK_ROOT, ANDROID_HOME and PATH. Below are the steps to perform the task.

Open /etc/profile on Debian and Ubuuntu, and add the following to the end of the file. The changes provide environmental variables for Android, but do not change the path. Many folks don't want to modify /etc/profile and apply the changes to all users. If desired, you can add the changes to a user's $HOME/.bash_profile.

if [ -d /opt/android-ndk ]; then
   export ANDROID_NDK_ROOT=/opt/android-ndk
fi

if [ -d /opt/android-sdk ]; then
   export ANDROID_SDK_ROOT=/opt/android-sdk
   export ANDROID_HOME=/opt/android-sdk
fi

if [ -e "/usr/libexec/java_home" ]; then
   export JAVA_HOME=$(/usr/libexec/java_home)
elif [ -d "/usr/lib/jvm/java-8-openjdk-amd64" ]; then
   export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
fi

Next, add the following to $HOME/.bash_profile to place the NDK and SDK tools on path.

if [ -d "$ANDROID_NDK_ROOT" ]; then
   export PATH="$PATH:$ANDROID_NDK_ROOT"
fi

if [ -d "$ANDROID_SDK_ROOT/tools/bin" ]; then
   export PATH="$PATH:$ANDROID_SDK_ROOT/tools/bin"
fi

if [ -d "$ANDROID_SDK_ROOT/platform-tools" ]; then
   export PATH="$PATH:$ANDROID_SDK_ROOT/platform-tools"
fi

The following directories are placed on path so the Android NDK and SDK tools are available:

  • ANDROID_NDK_ROOT - tools such as ndk-build and ndk-gdb
  • ANDROID_SDK_ROOT/tools/bin - tools such as sdkmanager
  • ANDROID_NDK_ROOT/platform-tools - tools such as ???

Once the variables are set, you should logout, login and then check the variables are expected.

$ echo $ANDROID_NDK_ROOT
/opt/android-ndk
$ echo $ANDROID_SDK_ROOT
/opt/android-sdk
$ echo $ANDROID_HOME
/opt/android-sdk
$ echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

Optionally, you can add Java's binaries to path with the following. However, your distribution should have already performed the work for you. They likely created softlinks in /usr/bin.

export PATH="$PATH:$JAVA_HOME/bin"