Skip to content

Android: Cross compiling on Windows

Ali Kämäräinen edited this page Jan 15, 2015 · 1 revision

Installing Dependencies

Git, SVN and CMake

The build script will fetch sources from git and svn repositories and build from sources. You will need git and svn executables in the PATH environment variable. patch is also used but that usually ships with git Windows installer.

Make sure any cmd line tools installer options are checked. Especially the Tortoise SVN installer might have the options unchecked by default.

CMake is used as the pre build system for most of the dependencies and Tundra itself. Make sure cmake.exe is also in PATH.

Android SDK

Download and run the Android SDK installer. You can click the "GET THE SDK FOR AN EXISTING IDE" section if you don't want to install Eclipse/Android Studio IDE with the SDK. For building Tundra you wont be needing any Android specific IDEs.

Version note: Version 23.0.5 is the latest version tested to work. Please report an issue if you install a later one which does not work with these instructions.

We will refer to the install directory as <ANDROID_SDK_INSTALL_DIR> from now on. The default Windows location will be C:\Program Files (x86)\Android\android-sdk.

Android NDK

Download and run the Android NDK installer. You should pick the appropriate 32 or 64-bit version for Windows. 64-bit version has been tested to work.

Version note: Version r10c is the latest version tested to work. Please report an issue if you install a later one which does not work with these instructions. Especially on the NDK Google does not provide easy to find links for anything but the latest release.

We will refer to the install directory as <ANDROID_NDK_INSTALL_DIR> from now on.

Ant

Ant is a build tool that is used frequently in Android development. With our C++ code base it will be used to build of the small Java part that will bootstrap the C++ application up and running.

Even if Ant is a essential part of the Android development experience, it is not shipped with the Android SDK. Download and run the Ant installer.

We will refer to the install directory as <ANT_INSTALL_DIR> from now on.

Make

Make is a built tool for C/C++. Android NDK will provide the cross-compilation tools, make will be using tools as instructed by the projects Makefile. Make is traditionally a Linux tool but with cross-compilation we will need a Windows version of it.

Android NDK provides the make tool, however we have experienced the tool freezing in the build if you use -jN where N>0. You can also use MinGW installer.

We will refer to the directory where make is with <MAKE_DIR> from now on. If you are using the NDK it will be <ANDROID_NDK_INSTALL_DIR>\prebuilt\windows-x86_64\bin or with MinGW <MINGW_INSTALL_DIR>\msys\1.0\bin.

MinGW: make freeze workaround

If you experience MinGW make freezing when the build starts. You can try running <MINGW_INSTALL_DIR>\bin\mingw-get upgrade msys-core-bin=1.0.17-1 which should be a version that does not have this bug yet.

This freeze has been seen to happen on 64-bit Windows 8/8.1 with both NDK and MinGW make. You can't do anything about NDK make but you can downgrade the MinGW one.

Setting up the Environment

We need to be able to call a lot of tools during the building from the above dependencies. This means we need to populate a few environment variables and add locations to PATH.

New Environment Variables

  • Set ANDROID_SDK to <ANDROID_SDK_INSTALL_DIR>
  • Set ANDROID_NDK to <ANDROID_NDK_INSTALL_DIR>

PATH

Add the following to end of PATH environment variable: ;%ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%MAKE_DIR%;%ANT_INSTALL_DIR%\bin.

If you think that any of the tools will be found from the incorrect place before in PATH, look at the below example that will prioritize these folders during development.

Example batch script to not pollute PATH permanently

Most developers don't like to pollute their PATH permanently if they are not going to use the tools very frequently. Putting your Android paths at the end of PATH is not recommended. If any of the tools are picked up from earlier directories in PATH, Windows will pick it up and possibly use a tool that will cause bugs for you.

Here is a SetupAndroidEnv.cmd that you can save into <TUNDRA_REPO>\tools\Windows to make life a little easier.

set ANDROID_NDK=<ANDROID_NDK_INSTALL_DIR>
set ANDROID_SDK=<ANDROID_SDK_INSTALL_DIR>

set PATH=%ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%MAKE_DIR%;%ANT_INSTALL_DIR%\bin;%PATH%

:: Work around for make hanging one >1 -j. Comment below line if make freezes on you.
:: set TUNDRA_DEPS_CPUS=1

You will need to run this file once every time you open a new Windows Command Prompt.

Building Tundra Dependencies

This step needs to be done once and subsequently every time the dependencies change. It is good to run this script each time you fetch new code from the upstream Tundra repository just in case.

  1. Open up a new Windows Command Prompt cmd.exe
  2. cd <TUNDRA_REPO>\tools\Windows
  3. SetupAndroidEnv.cmd (skip this step if you configured env variables permanently above)
  4. BuildAndroidDeps.cmd
  • The build script will print similar information than what this page provides. If you did all the above steps correctly you should be ready to continue. Hit any key to continue or Ctrl+C to abort.

This script has now built all the needed dependencies to <TUNDRA_REPO>\deps-android.

Running Tundra CMake

CMake needs to be once before initial build and subsequently any time CMake files are modified, but these runs should always be detected by make and ran automatically.

  1. cd <TUNDRA_REPO>
  2. cmake-android.bat

Building Tundra

  1. cd <TUNDRA_REPO>\src\Android
  2. Run make

Building Android APK

  1. cd <TUNDRA_REPO>\src\Android
  2. Copy assets from the Tundra bin directory by executing CopyData.bat
  3. Create the ant build files: android update project -p . -t <targetAPINumber>
  • Only needed when building the first time
  • Use android list targets to list your installed Android target API numbers
  1. Build the APK: ant debug
  • Use ant release and sign your APK when creating an actual release build
  1. The APK is written to <TUNDRA_REPO>\src\Android\bin directory where it can be uploaded to a device or run on an emulator.

Deploying to a Android device

Once you have the APK you can deploy it to your device with adb -r bin/Tundra-debug.apk. The -r will replace any existing installation if already present on the device.

Note: You have to connect your device and enable "USB Debugging" in your Android devices settings. Where and how this is done many times varies on each device/OS, ask Google for more details. Some devices also allow WiFi debug connections, its up to you get that working if you want to explore it.

Bringing it all together

When doing development, you probably don't want to be running all the above steps separately every time you do C++ code changes. Here is a one liner for you:

  1. cd <TUNDRA_REPO>\src\Android
  2. CopyData.bat && make && ant debug && adb -r bin/Tundra-debug.apk && adb logcat

The last adb logcat will connect to the device logs so you can monitor Tundra log prints.