Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import own native shared library from sub-module in Android Studio / CMake

I have a Android Studio (2.3) project with 2 modules, using CMake for native code.

Project
  --> Module1 (app): java + native JNI-wrapper, linking to libnative.so
  --> Module2 (libnative): native c++ code, producing libnative.so

What is the preferred way to link libnative.so (build by Module2) into the JNI-wrapper in Module1? I currently use...

Module1-CMakeLists.txt:

add_library( native SHARED IMPORTED )
set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so )

...where BUILD_TYPE is set in Module1's build.gradle, depending on the build type.

This works if I use "Make Module 'Module2'" in AS before building the full project. However, it seems rather inelegant to fetch the library out of gradle's building folder-hierarchy.

The alternative seemed to be to instruct Module2's CMakeLists.txt to install the file into Module1's lib-directory and import from there. But CMake seems to ignore the install command.

(I'm aware that I could just put the modules together under one tree.)

Thanks!

like image 572
Fimagena Avatar asked Mar 14 '17 21:03

Fimagena


1 Answers

The alternative is as below: (Module2 CMakeLists.txt)

set_target_properties(${SHARED_LIBRARY_NAME}
                  PROPERTIES
                  LIBRARY_OUTPUT_DIRECTORY "<your-prefered-directory>/jniLibs/${ANDROID_ABI}")
like image 69
shizhen Avatar answered Oct 29 '22 04:10

shizhen