Cross building to Android with the NDK

In this example, we are going to see how to cross-build a Conan package to Android.

First of all, download the Android NDK from the download page and unzip it. In MacOS you can also install it with brew install android-ndk.

Then go to the profiles folder in the conan config home directory (check it running conan config home) and create a file named android with the following contents:

  1. include(default)
  2. [settings]
  3. os=Android
  4. os.api_level=21
  5. arch=armv8
  6. compiler=clang
  7. compiler.version=12
  8. compiler.libcxx=c++_static
  9. compiler.cppstd=14
  10. [conf]
  11. tools.android:ndk_path=/usr/local/share/android-ndk

You might need to modify:

  • compiler.version: Check the NDK documentation or find a bin folder containing the compiler executables like x86_64-linux-android31-clang. In a Macos installation it is found in the NDK path + toolchains/llvm/prebuilt/darwin-x86_64/bin. Run ./x86_64-linux-android31-clang --version to check the running clang version and adjust the profile.

  • compiler.libcxx: The supported values are c++_static and c++_shared.

  • compiler.cppstd: The C++ standard version, adjust as your needs.

  • os.api_level: You can check here the usage of each Android Version/API level and choose the one that fits better with your requirements. This is typically a balance between new features and more compatible applications.

  • arch: There are several architectures supported by Android: x86, x86_64, armv7, and armv8.

  • tools.android:ndk_path conf: Write the location of the unzipped NDK.

If you are in Windows, it is necessary to have a make-like build system like MinGW-Make or Ninja. We can provision for Ninja directly in our profile with [tool_requires]:

  1. ...
  2. [conf]
  3. tools.android:ndk_path=C:\ws\android\android-ndk-r23b # Use your path here
  4. tools.cmake.cmaketoolchain:generator=Ninja
  5. [tool_requires]
  6. ninja/[*]

Use the conan new command to create a “Hello World” C++ library example project:

  1. $ conan new cmake_lib -d name=hello -d version=1.0

Then we can specify the android profile and our hello library will be built for Android:

  1. $ conan create . --profile android
  2. [ 50%] Building CXX object CMakeFiles/hello.dir/src/hello.cpp.o
  3. [100%] Linking CXX static library libhello.a
  4. [100%] Built target hello
  5. ...
  6. [ 50%] Building CXX object CMakeFiles/example.dir/src/example.cpp.o
  7. [100%] Linking CXX executable example
  8. [100%] Built target example

Both the library and the test_package executable are built for Android, so we cannot use them in our local computer.

Unless you have access to a root Android device, running the test application or using the built library is not possible directly so it is more common to build an Android application that uses the hello library.

It is also possible to use the android-ndk from a Conan tool-requires. There is already a Conan package in ConanCenter containing the AndroidNDK, so writing a profile like:

  1. [settings]
  2. os=Android
  3. os.api_level=21
  4. arch=armv8
  5. compiler=clang
  6. compiler.version=18
  7. compiler.libcxx=c++_static
  8. compiler.cppstd=14
  9. build_type=Release
  10. # You might need Ninja conf and tool-requires in Windows too
  11. [tool_requires]
  12. android-ndk/[*]

And this will download automatically the latest android-ndk from ConanCenter and inject and apply it automatically to build the package. Note that to use packages from ConanCenter in production the following approach is recommended

See also