Import own native shared library from sub-module i

2019-04-23 15:13发布

问题:

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!

回答1:

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

set_target_properties(${SHARED_LIBRARY_NAME}
                  PROPERTIES
                  LIBRARY_OUTPUT_DIRECTORY "<your-prefered-directory>/jniLibs/${ANDROID_ABI}")