question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] How do you set the NDK path for Android builds in CMakeToolchain?

See original GitHub issue

I’m trying to figure out how to build for Android using the Conan 2.0 CMakeToolchain. I get this error:

ConanException: CMakeToolchain needs tools.android:ndk_path configuration defined

The docs mention tools.android:ndk_path, but not how to set it. There are a few clues in the source code, but all the things I’ve tried in conanfile.py and conan.conf haven’t worked. This was very easy with the cmake generator, I just did this:

cmake.definitions["CMAKE_TOOLCHAIN_FILE"] = os.getenv("ANDROID_NDK_ROOT") + "/build/cmake/android.toolchain.cmake"

…but it doesn’t work anymore.

Currently, I share this Python script with my Conan 1.x packages to define build settings for desktop and mobile platform builds: https://github.com/ssrobins/conan-cmake_utils/blob/217df94/cmake_utils.py.

In addition to being stuck on this, I’m a bit concerned that Conan 2.0 will make it not possible or at least harder to use custom toolchains, which is critical for close-sourced platforms that don’t have any built-in CMake support.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
ssrobinscommented, May 31, 2021

Thanks @memsharded, putting that code in ~/.conan/profiles/default allowed NDK to be found. Unfortunately, it failed right after with:

Invalid Android STL: None.
CMake Error: CMake was unable to find a build program corresponding to "Ninja Multi-Config".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.

I tried adding ANDROID_STL and CMAKE_MAKE_PROGRAM, even though ninja is in my PATH and is working in non-Conan 2.0 packages, but it didn’t help. Here’s my full generate() method:

def generate(self):
    tc = CMakeToolchain(self)
    if self.settings.os == "Android":
        tc.generator = "Ninja Multi-Config"
        tc.variables["CMAKE_MAKE_PROGRAM"] = "/usr/local/bin/ninja"
        tc.variables["ANDROID_STL"] = "c++_static"
    elif self.settings.os == "iOS":
        tc.generator = "Xcode"
        tc.variables["CMAKE_OSX_DEPLOYMENT_TARGET"] = self.settings.os.version
    elif self.settings.os == "Linux":
            tc.generator = "Ninja Multi-Config"
    elif self.settings.os == "Macos":
        tc.generator = "Xcode"
        tc.variables["CMAKE_OSX_DEPLOYMENT_TARGET"] = self.settings.os.version
    tc.generate()
    deps = CMakeDeps(self)
    deps.generate()

The CMAKE_OSX_DEPLOYMENT_TARGET for macOS and iOS are working so this seems to only affect the Android builds.

For Conan 1.x, this is all I had to add to my conanfile.py to build Android:

if settings.os == "Android":
    cmake.generator = "Ninja Multi-Config"
    if settings.arch == "armv7":
        cmake.definitions["ANDROID_ABI"] = "armeabi-v7a"
    elif settings.arch == "armv8":
        cmake.definitions["ANDROID_ABI"] = "arm64-v8a"
    cmake.definitions["ANDROID_TOOLCHAIN"] = "clang"
    cmake.definitions["ANDROID_STL"] = "c++_static"
    cmake.definitions["CMAKE_TOOLCHAIN_FILE"] = os.getenv("ANDROID_NDK_ROOT") + "/build/cmake/android.toolchain.cmake"

I was just able to pass CMake params exactly how it would work without Conan. Yes it’s more code than if it was taken care of for me, but I appreciated the transparency and simplicity. It let me use the ANDROID_NDK_ROOT environment variable as the single definition for NDK on my system that works for CMake and Conan 1.x. It’s also starting to become the standard in cloud CI environments like GitHub Actions.

For the Android toolchain as well as any custom toolchain, I would love for this to be done with a setting in the conanfile.py, like it was before. The cmake interface is excellent in Conan 1.x, I don’t understand why it’s being made less discoverable and spread to multiple files for Conan 2.x.

0reactions
memshardedcommented, Jul 17, 2021

Just created a new one from your comment in https://github.com/conan-io/conan/issues/9282, please track it and provide feedback there. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Install and configure the NDK and CMake - Android Developers
Specify the CMake version you want Gradle to use in your module's build.gradle file. Either add the path to the CMake installation to...
Read more >
android.toolchain.cmake - Google Git
Android CMake toolchain file, for use with the Android NDK r5-r10d ... SET ANDROID_NDK=C:\absolute\path\to\the\android-ndk. # $ mkdir build && cd build.
Read more >
Android NDK - CMake build environment variables
So, what is recommended and correct way to setup ANDROID variables in CMake toolchain? As you've done, from the command line.
Read more >
Basic CMake setup with NDK and MediaNDK - Google Groups
Hello folks, I'm new to the Android NDK, MediaNDK and so on. ... is not supposed to live inside of an Android Studio...
Read more >
How to use Hunter in Android Studio?
CMake can be used as a build tool for native C/C++ libraries in Android Studio. ... You may want to add the paths...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found