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

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

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

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