从Android Studio/CMake中的子模块导入自己的本机共享库 [英] Import own native shared library from sub-module in Android Studio / CMake

查看:220
本文介绍了从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}")

这篇关于从Android Studio/CMake中的子模块导入自己的本机共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆