How to tell CMake (invoked through externalNativeBuild) where to look for libraries?

  android, android-ndk, cmake, gradle

I am working on a multi-platform project which, among others, supports Linux and Android. The code is mostly C; the Android port has a Java wrapper for the platform-specific parts, which calls the native code parts for the core functionality.

On Linux we build this project with CMake, followed by make. For Android we essentially use Gradle and call CMake through externalNativeBuild. The CMakeFile.txt can detect an Android build with a simple if (ANDROID), allowing us to

The project relies on some standard libraries, which we detect with the following in our CMakeLists.txt:

find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
    pkg_check_modules ( FOO foo-42.0 )
    if (FOO_FOUND)
        set(HAVE_FOO 1)
        include_directories(${FOO_INCLUDE_DIRS})
        # …

When building for Android, this picks up either nothing or libraries for the host OS (somewhere underneath /usr/include/) rather than those for Android (which would reside somewhere underneath $NDK/sysroot). (Note $NDK just denotes the NDK path, not necessarily an environment variable.)

According to this post, pkg-config relies on a bunch of environment variables to determine where to look for libraries, namely CMAKE_SYSROOT, PKG_CONFIG_DIR, PKG_CONFIG_LIBDIR and PKG_CONFIG_SYSROOT_DIR. I checked them and they are empty (or unset).

Using the gradle/NDK/CMake toolchain, how can I either pass the correct variables to CMake, or otherwise have it find the libraries for which NDK supplies headers?

Source: Android Questions

LEAVE A COMMENT