简体   繁体   English

Android Studio中的多个本机模块

[英]Multiple Native Modules in Android Studio

I have a C++ codebase that is currently set up in Visual Studio (to run on Windows), with multiple Projects with inter-dependencies. 我有一个C ++代码库,目前在Visual Studio中设置(在Windows上运行),具有多个具有相互依赖关系的项目。 I'm trying to bring it over to Android Studio, to get it running on Android. 我正试图将它带到Android Studio,以使其在Android上运行。 I'm familiar with Visual Studio and C++, but quite new to Android Studio's Gradle and CMake. 我熟悉Visual Studio和C ++,但对于Android Studio的Gradle和CMake来说却是一个新手。

My (possibly wrong) expectation is to try and treat Android Studio Projects like Visual Studio Solutions, and Android Studio Modules like Visual Studio Projects. 我的(可能是错误的)期望是尝试处理像Visual Studio Solutions这样的Android Studio项目,以及像Visual Studio项目这样的Android Studio模块。 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. 鉴于我的代码库在Visual Studio中使用了多个项目,我试图在Android Studio中创建多个模块 - 每个模块都有自己的build.gradleCMakeLists.txt文件。

The issue is that I cannot get one section of code (AS Module) to link with the other. 问题是我无法将一段代码(AS模块)与另一段进行链接。 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). 我正在使用add_library()将这些不同的部分编译为STATIC (我计划有一个模块创建一个SHARED库,加载到Java中)。

I can easily get the includes to work via include_directories("../OtherModule/src/") . 我可以通过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). 我找不到要链接的.so(或类似)文件(通过target_link_libraries()或等效文件)。 When I extract the .arr file from a given Module, I do not see any .so or anything. 当我从给定的模块中提取.arr文件时,我看不到任何.so或任何东西。

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() ). 我意识到我可以简单地将整个代码库放在一个模块下(使用一个build.gradle和一个CMakeLists.txt - 或使用add_subdirectory()CMakeLists.txt网络)。 I don't know if this is fine, or if it would take more/less time to build. 我不知道这是否正常,或者是否需要更多/更少的时间来构建。

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. 到目前为止,我所做的所有研究都发现,使用Java代码严格地将本机代码添加到同一模块中 - 执行基本的JNI本机桥接。 I haven't been able to find a single article about multiple native Modules linking together. 我找不到一篇关于连接在一起的多个本机模块的文章。

I'm hoping that someone with more experience with native development on Android could help me out. 我希望那些在Android上有更多本地开发经验的人可以帮助我。 Thanks! 谢谢!

TL;DR: Simplified scenario: (Without being concerned with the JNI native bridge) I have two Modules in Android Studio, both with only native code. TL; DR:简化场景:(不关心JNI本机桥)我在Android Studio中有两个模块,两者都只有本机代码。 I would like to have each Module have its own build.gradle and CMakeLists.txt , creating its own STATIC libraries. 我想让每个Module都有自己的build.gradleCMakeLists.txt ,创建自己的STATIC库。 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... 在我看来,AS ......

  1. ...does not actually link the final module-library unless it's SHARED (it does allow static 'sub-libraries' within the module); ...实际上并没有链接最终的模块库,除非它是SHARED (它确实允许模块中的静态'子库'); consider making the final library shared - you will have to System.loadLibrary() it specifically in Java though. 考虑让最终的库共享 - 尽管如此,你必须使用System.loadLibrary()
  2. ...does not allow you to install files to other places (eg, from your native module to your Android app). ...不允许您将文件安装到其他地方(例如,从您的原生模块到Android应用程序)。 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. 我通过set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so )并设置BUILD_TYPE 。的build.gradle。 Not overly elegant though. 虽然不过分优雅。

Overall, this does not seem to be an encouraged use-case in AS... 总的来说,这似乎不是AS中鼓励的用例...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM