从Android Studio/CMake中的子模块导入自己的本机共享库
我有一个带有2个模块的Android Studio(2.3)项目,使用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
将libnative.so
(由Module2构建)链接到Module1中的JNI-wrapper的首选方法是什么?我目前正在使用...
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 )
...其中,BUILD_TYPE
是在Module1的build.gradle
中设置的,具体取决于构建类型.
...where BUILD_TYPE
is set in Module1's build.gradle
, depending on the build type.
如果在构建完整项目之前在AS中使用"使模块为'Module2'",则此方法有效.但是,从gradle的构建文件夹层次结构中提取库似乎不太明智.
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.
另一种选择似乎是指示Module2的CMakeLists.txt
将文件安装到Module1的lib目录中并从那里导入.但是CMake似乎忽略了install
命令.
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.)
谢谢!
替代方法如下:(Module2 CMakeLists.txt
)
The alternative is as below: (Module2 CMakeLists.txt
)
set_target_properties(${SHARED_LIBRARY_NAME}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "<your-prefered-directory>/jniLibs/${ANDROID_ABI}")