Search code examples
androidccmakeandroid-ndkndk-build

Using ndk-build and cmake both for different libaries in NDK


I have an android library that contains my native code and I import that library into my app. I already had some native C/C++ code written in this library and had set up the compilation using ndk-build. Here is the code that I had added to my library's gradle file:

externalNativeBuild {
    ndkBuild {
        path 'src/main/jni/Android.mk'
    }
}

And this was working perfectly fine. Now, I want to include an external native C/C++ library for which I have instruction as to how to set it up using CMake. I wrote the code to include this external library into my top-level CMakeLists.txt and included it into my gradle which not looks like

externalNativeBuild {
    ndkBuild {
        path 'src/main/jni/Android.mk'
    }

    cmake {
        // Provides a relative path to your CMake build script.
        path "CMakeLists.txt"
    }
}

When I try to build this, I get an error saying

More than one externalNativeBuild path specified

Is there a way to build this or do I have to use only one of ndk-build or CMake.


Solution

  • Both cmake and gnu make (which underlies ndk-build) support custom targets and external tools, so you can start your cmake part fom Android.mk or vice versa.

    But this won't help with Android Studio integration. To have full IDE support for both native subprojects, you can either rewrite one or another to use the same external native build, or extract the part that depends on one of them, from application module to a library module.