适用于本机库的Android AAR软件包 [英] Android AAR package for native library

查看:207
本文介绍了适用于本机库的Android AAR软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将本机库打包到AAR包中的方法,因此可以通过gradle脚本中的依赖项声明来使用它.

I'm looking for a way to package a native library into an AAR package, so it would be possible to use it via dependencies declaration in gradle script.

通过本机库,我的意思是指一组.cpp文件或已编译的静态库以及一组头文件.因此,我的意思是应用程序本身将通过本机代码而不是Java调用该库.换句话说,该库需要编译应用程序的本机代码.这样就可以轻松管理本机代码的依赖性.

By native library I mean set of .cpp files or compiled static library and a set of header files. So, I mean that the app itself will call the library from native code, not from Java. In other words, the library needed to compile app's native code. So that it will be possible to easily manage dependencies of native code.

有可能吗?

到目前为止,我只能找到很多问题/示例,说明如何使用.so文件及其Java接口对JNI本机库进行AAR,因此该库只是具有本机实现的Java库,但这不是事实.我需要.

So far I could only find a lot of questions/examples of how to make an AAR of JNI native library with .so file and its Java interface, so the lib just a Java lib with native implementation, but this is not what I need.

推荐答案

手动修改gradle脚本是可行的,但是很痛苦且容易出错.

Manually hacking the gradle scripts works, but is painful and error-prone.

我最近发现了一个插件,可以在添加依赖项时将标头神奇地捆绑到AAR文件中,并提取它们并设置构建脚本: https://github.com/howardpang/androidNativeBundle

I've recently found a plugin that magically bundles the headers into the AAR files and extracts them and sets up the build scripts when adding the dependency: https://github.com/howardpang/androidNativeBundle

  • 添加导出插件:

  • Add the export plugin:

apply plugin: 'com.ydq.android.gradle.native-aar.export'

  • 定义头文件的位置:

  • Define where the header files are:

    nativeBundleExport {
        headerDir = "${project.projectDir}/src/main/jni/include"
    }
    

    • 添加导入插件:

    • Add the import plugin:

    apply plugin: 'com.ydq.android.gradle.native-aar.import'
    

  • include ${ANDROID_GRADLE_NATIVE_BUNDLE_PLUGIN_MK}添加到Android.mk中依赖于它的每个模块:

  • add include ${ANDROID_GRADLE_NATIVE_BUNDLE_PLUGIN_MK} to each module that depends on it in your Android.mk:

    include $(CLEAR_VARS)
    LOCAL_SRC_FILES := myapp.cpp \
    LOCAL_MODULE := myapp
    LOCAL_LDLIBS += -llog
    include ${ANDROID_GRADLE_NATIVE_BUNDLE_PLUGIN_MK}
    include $(BUILD_SHARED_LIBRARY)
    

  • 这篇关于适用于本机库的Android AAR软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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