I want to build a shared library with different variants / versions that I can use on Android and in Linux applications.
Therefor, I set up an Android Project consisting of an app module and a (native C/C++) library module as outlined in the official Android Studio documentation * , so that my complete build tool chain for the library consists of ** :
build.gradle
CMakeLists.txt
My code is actually portable, eg I used #ifdef __ANDROID__
around Android specific code paths, and I can compile a working Linux shared object by hand , that is, by invoking the compiler from the command line.
Is there a nice way to use the above tool chain to create both the .so
files for Android and Linux, without maintaining two redundant sets of build configuration files?
* I followed the documentation from May 2017, which is almost identical to this archive.org snapshot from February 2017.
** I do not use Android.mk and ndk-build, so that this related question does not apply for me, even though the purpose is the same.
As it turns out, it's easier than I thought. In this case, Gradle is not needed for the Linux version, and the Cmake configuration is mostly portable across Android and Linux. So theoretically, you can just:
cd $AndroidProject/librarymodule/build
cmake -G "Unix Makefiles" .. && make
As said before, this circumvents gradle, meaning that the configurations in the build.gradle
are nerver applied.
The easiest fix is to add those settings to CMakeLists.txt
like this:
# (...) Common defintions for both platforms here (...)
if (ANDROID)
find_library( log-lib log )
target_link_libraries( librarymodule ${log-lib} )
else ( )
find_package( OpenCV REQUIRED )
target_compile_definitions ( librarymodule PUBLIC ENABLE_DEBUG_LOGGING linux HAVE_LIBUDEV)
target_include_directories ( librarymodule PRIVATE src/main/cpp ../otherlib/src/main/cpp )
target_compile_options ( librarymodule PRIVATE -Wall -Wno-unknown-pragmas -O0 -g -std=c++11 -fexceptions )
target_link_libraries( librarymodule pthread ${OpenCV_LIBS} )
)
Theres a disadvantage in having identical / similar settings scattered across build.grade
for Android and CMakeLists.txt
for Linux. Possible solutions:
CMakeLists.txt
. Should be trivial.build.gradle
. Could be complex.I searched for a similar solution, but I think there is currently no way to do it. There is no Android-Studio plugin to do native c/c++ compilation. Java is possible, but not an option for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.