Android本机库从aar链接到另一个本机库 [英] Android native library linking against another native library from aar

查看:149
本文介绍了Android本机库从aar链接到另一个本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我有一个aar库,它包含并使用本机.so库。现在,我想编写另一个lib,它依赖于该库,并且还具有本机部分,具体取决于来自第一个lib的本机lib。依赖库使用第一个lib中的本机代码和java包装器。

I have a curious question. I have an aar library, which contains and uses native .so library. Now, I want to write another lib, which depends on that library and also has native part depending on native lib from the first lib. The dependent library uses both the native code and java wrappers from the first lib.

我想知道,有没有办法,如何通过标准gradle依赖(使用第一个lib中复制的头文件)来做到这一点?或者我是否要直接从源代码构建第二个库?

I wonder, is there any way, how to do this by standard gradle dependency(with copied headers files from the first lib)? Or have I to build the second lib directly from the sources?

为什么我需要这个:
我们有一个具有基本功能的多平台lib,用于android as aar 。这个lib可以在标准的android应用程序中使用,我们在多个项目中使用它,没有其他本机代码。

Why I need this: We have a multiplatform lib with basic functionality, for android as aar. This lib can be used in standard android app and we use it in multiple projects, which have no another native code.

但是在一个应用程序中我们想要编写多平台的共享应用程序代码,取决于那个lib,我希望将这些库分开。

But in one app we want to write multiplatform shared app code, depending on that lib and I want to have these libs separated.

谢谢!

推荐答案

这里是基于 OpenCV的可行示例,你可以为你的第一个lib 做同样的事情。

Here is a workable example basic on OpenCV, you can do the same for your first lib.

打包 jar *。so ,并导出 headers (请参阅链接项目中的文件 OpenCV4Android / opencv / build.gradle 如何将标题附加到aar) 。

Package the jar, *.so, and exported headers (see the file OpenCV4Android/opencv/build.gradle in the linked project how to append the headers to aar).

你得到 first.aar 例如从第一个lib的构建

添加 first.a ar 在您需要的其他项目中。

Add the first.aar in your other projects when you needed.

allprojects {
    repositories {
        jcenter()

        flatDir {
            dirs '/path/to/aar'
        }
    }
}

// in your app's build.gradle
dependencies {
    // ...
    compile 'com.example:example-with-header@aar'
    // ...
}

首先将本地库链接到。来自您的本机构建系统的aar

Link against your native library to the first.aar from your native build system.

如果您使用 CMake ,它应该看起来像这样

If you use CMake, it should look like this

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

这篇关于Android本机库从aar链接到另一个本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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