我有一个奇怪的问题。我有一个 aar 库,它包含并使用 native .so 库。现在,我想编写另一个库,它依赖于该库,并且还具有依赖于第一个库中的 native 库的 native 部分。依赖库使用第一个库中的 native 代码和 java 包装器。
我想知道,有什么办法可以通过标准的 gradle 依赖项(使用从第一个库复制的头文件)来做到这一点?还是直接从源代码构建第二个库?
为什么我需要这个: 我们有一个具有基本功能的多平台库,适用于 android 作为 aar。这个库可以在标准的 Android 应用程序中使用,我们在没有其他原生代码的多个项目中使用它。
但在一个应用中,我们希望根据该库编写多平台共享应用代码,我希望将这些库分开。
谢谢!
最佳答案
注意:此答案已弃用,因为 Android Studio 不再提供 explored-aar
目录。
要使用来自 aar
的 header 的更好解决方案,请查看库 androidNativeBundle
Here是基于 OpenCV
的可行示例,您可以对您的第一个库
执行相同的操作。
如下准备第一个库
打包jar
、*.so
和导出的headers
(参见文件OpenCV4Android/opencv/build.gradle
在链接的项目中如何将 header 附加到 aar)。
例如,您可以从 first lib
的构建中获得 first.aar
。
在你的其他项目中使用第一个库
在您需要时在您的其他项目中添加 first.aar
。
allprojects {
repositories {
jcenter()
flatDir {
dirs '/path/to/aar'
}
}
}
// in your app's build.gradle
dependencies {
// ...
compile 'com.example:example-with-header@aar'
// ...
}
从您的本地构建系统将您的本地库链接到 first.aar
。
如果你使用CMake
,它应该是这样的
add_library(first
SHARED
IMPORTED)
set_target_properties(
first
PROPERTIES IMPORTED_LOCATION
../../../../build/intermediates/exploded-aar/org.example/example-debug/jni/${ANDROID_ABI}/libfirst.so
# use find command to figure out the location of the first lib before use it, not sure if it's different in different build environment
# for android studio gradle plugin latest version use
# ../../../../build/intermediates/transforms/mergeJniLibs/debug/folders/2000/1f/main/lib/${ANDROID_ABI}/libfirst.so
)
# also use find command to figure the actual location of the exported header from aar
# this might be different in your environment for different version of gradle plugin
include_directories(build/intermediates/exploded-aar/com.example/example-debug/cpp/include)
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
first
${log-lib} )
关于java - Android native 库从 aar 链接到另一个 native 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31596533/