从 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 包装器的首选方法是什么?我目前使用...
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 中使用Make Module '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}")