Android Studio 中的多个原生模块 [英] Multiple Native Modules in Android Studio

查看:38
本文介绍了Android Studio 中的多个原生模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前在 Visual Studio 中设置的 C++ 代码库(在 Windows 上运行),其中包含多个相互依赖的项目.我正在尝试将它带到 Android Studio,让它在 Android 上运行.我熟悉 Visual Studio 和 C++,但对 Android Studio 的 Gradle 和 CMake 很陌生.

I have a C++ codebase that is currently set up in Visual Studio (to run on Windows), with multiple Projects with inter-dependencies. I'm trying to bring it over to Android Studio, to get it running on Android. I'm familiar with Visual Studio and C++, but quite new to Android Studio's Gradle and CMake.

我(可能是错误的)期望是尝试处理像 Visual Studio 解决方案这样的 Android Studio 项目和像 Visual Studio 项目这样的 Android Studio 模块.鉴于我的代码库在 Visual Studio 中使用多个项目,我试图在 Android Studio 中创建多个模块——每个模块都有自己的 build.gradleCMakeLists.txt 文件.

My (possibly wrong) expectation is to try and treat Android Studio Projects like Visual Studio Solutions, and Android Studio Modules like Visual Studio Projects. Given that my codebase uses multiple Projects in Visual Studio, I am trying to create multiple Modules in Android Studio -- each one with their own build.gradle and CMakeLists.txt files.

问题是我无法让一段代码(AS 模块)与另一段链接.我正在使用 add_library() 将这些不同的部分编译为 STATIC(我计划有一个创建 SHARED 库的模块,以加载到 Java).

The issue is that I cannot get one section of code (AS Module) to link with the other. I am compiling these different sections as STATIC using add_library() (I plan to have one Module that creates a SHARED library, to load into Java).

我可以通过 include_directories("../OtherModule/src/") 轻松地使包含工作.但是,我无法将其链接.我找不到要链接到的 .so(或类似)文件(通过 target_link_libraries() 或等效文件).当我从给定的模块中提取 .arr 文件时,我看不到任何 .so 或任何内容.

I can easily get the includes to work via include_directories("../OtherModule/src/"). However, I cannot get it to link. I cannot find the .so (or similar) file to link to (via target_link_libraries() or equivalent). When I extract the .arr file from a given Module, I do not see any .so or anything.

我意识到我可以简单地将整个代码库放在一个模块下(使用一个 build.gradle 和一个 CMakeLists.txt -- 或者 CMakeLists 网络.txt 使用 add_subdirectory()).我不知道这是否可以,或者是否需要更多/更少的时间来构建.

I realize that I could simply put the entire codebase under one Module (using one build.gradle and one CMakeLists.txt -- or network of CMakeLists.txt's using add_subdirectory()). I don't know if this is fine, or if it would take more/less time to build.

我确信可以有多种设置方式,这可能只是一个偏好问题.到目前为止,我所做的所有研究都只发现将本机代码严格添加到带有 Java 代码的同一模块中——做基本的 JNI 本机桥接工作.我找不到一篇关于多个本机模块链接在一起的文章.

I'm sure that there could be multiple ways to set this up, and it could just be a matter of preference. All research that I've done thus far has only found strictly adding native code to the same module with Java code -- doing basic JNI native bridge stuff. I haven't been able to find a single article about multiple native Modules linking together.

我希望有更多 Android 原生开发经验的人可以帮助我.谢谢!

I'm hoping that someone with more experience with native development on Android could help me out. Thanks!

TL;DR: 简化场景:(不关心 JNI 本机桥)我在 Android Studio 中有两个模块,都只有本机代码.我想让每个模块都有自己的 build.gradleCMakeLists.txt,创建自己的 STATIC 库.一个模块依赖于另一个并且必须设置正确的包含和链接目录.怎么办?!这是否正确(或者应该只有一个带有本机代码的模块)?

TL;DR: Simplified scenario: (Without being concerned with the JNI native bridge) I have two Modules in Android Studio, both with only native code. I would like to have each Module have its own build.gradle and CMakeLists.txt, creating its own STATIC libraries. One Module depends on the other and must set the correct include and link directories. How do?! Is this even correct (or should there ever be only one Module with native code)?

推荐答案

我问了一个相关的问题 此处.在我看来,AS...

I asked a related question here. It seems to me that AS...

  1. ...实际上并不链接最终的模块库,除非它是 SHARED(它确实允许模块内的静态子库");考虑让最终的库共享——不过你必须System.loadLibrary()它专门在 Java 中.
  2. ...不允许您将文件安装到其他位置(例如,从您的本机模块到您的 Android 应用程序).我通过通过 set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so/code> 并在 build.gradle 中设置 BUILD_TYPE.不过也不过分优雅.
  1. ...does not actually link the final module-library unless it's SHARED (it does allow static 'sub-libraries' within the module); consider making the final library shared - you will have to System.loadLibrary() it specifically in Java though.
  2. ...does not allow you to install files to other places (e.g., from your native module to your Android app). I work around this by fetching the library through set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so ) and setting BUILD_TYPE in build.gradle. Not overly elegant though.

总的来说,这在 AS 中似乎不是一个鼓励的用例...

Overall, this does not seem to be an encouraged use-case in AS...

这篇关于Android Studio 中的多个原生模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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