cmake with multiple inter-dependent ndk modules

528 views
Skip to first unread message

Sheldon Neuberger

unread,
Feb 1, 2018, 12:55:45 PM2/1/18
to android-ndk
I have a repository with multiple modules that have various inter-dependencies. These modules contain C++ and Java code. I have them separated into modules so that consuming projects can take dependencies on only what they need. Is it possible to setup an android project with cmake in this way? Here's what I would like:

x/
--CMakeLists.txt
--build.gradle
--java/
----x.java
--cpp/
----x.cpp
----x.hpp
y/
--CMakeLists.txt
--build.gradle
--java/
----y.java
--cpp/
----y.cpp
----y.hpp

in this example, suppose that: x and y are modules; each module builds a shared library (libx.so and liby.so); and x's cpp code depends on y's cpp code. So now an android app could depend on y/build.gradle and they would only get module y, or they could depend on x/build.gradle and they would get both x and y (because x depends on y).

Given that structure, how would I express x's C++-level dependency on y?

I do have a working example that gets me halfway there, but it has one super build.gradle:

build.gradle
CMakeLists.txt
x/
--CMakeLists.txt
--java/
----x.java
--cpp/
----x.cpp
----x.hpp
y/
--CMakeLists.txt
--java/
----y.java
--cpp/
----y.cpp
----y.hpp

In this example, the top-level build.gradle sets externalNativeBuild to the top-level CMakeLists.txt. The top-level CMakeLists.txt calls add_subdirectory for each module. This builds everything as expected and allows x's C++ to take a dependency on y, but it requires a consumer to pull all modules in (via the top-level build.gradle).

Alex Cohn

unread,
Feb 4, 2018, 1:57:00 PM2/4/18
to android-ndk
You could set two library modules, one for libx and another for liby. This way, you not only have two independent C++ directories with CMakeLists.txt each, but also the JNI wrappers (the Java classes that work with each of these libraries) are isolated from each other. It's easier to maintain JNI this way. In my experience, keeping Java and C++ sides in sync becomes a challenge when the development team grows beyond one person.

Now your Android Studio project will have three modules: app and two libraries. Another project can only use one library. Note that it is possible to wrap such library module in maven, and include it as an external aar - which contains both Java and C++ precompiled.

BR,
Alex
Reply all
Reply to author
Forward
0 new messages