Android Studio中的多个本机模块 [英] Multiple Native Modules in Android Studio

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

问题描述

我有一个C ++代码库,当前在Visual Studio中设置(可以在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 Projects之类的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或使用add_subdirectory()CMakeLists.txt网络).我不知道这是否还行,或者是否需要花费更多/更少的时间来构建.

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)?

推荐答案

我问了一个相关问题

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

  1. ...实际上并不链接最终的模块库,除非它是SHARED(它确实允许模块内有静态的子库");考虑将最终的库共享-尽管您必须专门在Java中System.loadLibrary()它.
  2. ...不允许您将文件安装到其他位置(例如,从本机模块安装到Android应用).我通过通过set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so )获取库并在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天全站免登陆