在Android项目中链接共享库 [英] Linking shared library in Android project

查看:160
本文介绍了在Android项目中链接共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要导入在android studio项目中用C文件编写的单个函数.该函数调用位于另一个文件中的其他函数(总共50个以上的C文件和头文件).

I need to import a single function written in a C file in an android studio project. This function call others functions located in anothers files (50+ C files and headers in total).

该项目已经包含一个C ++文件,因为我正在使用NDK编译OpenCV4android.

This project already contains a single C++ file as I am using NDK to compile OpenCV4android.

我已经使用Mingw和GCC来编译共享库(libfinal.so),但是一旦我尝试导入它们,这要归功于NDKbuild,我得到了这个毫无意义的错误:

I've used Mingw and GCC to compile a shared libraries (libfinal.so) but once i try to import them thanks to NDKbuild i got this meaningless error :

Error:Execution failed for task ':app:ndkBuild'.
> Process 'command 'C:/SDK/ndk-bundle/ndk-build.cmd'' finished with non-zero exit value 2

这是Android.mk文件:

Here is the Android.mk file :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

/some opencv stuff/

include $(CLEAR_VARS)
LOCAL_MODULE := final
LOCAL_SRC_FILES := libfinal.so
LOCAL_EXPORT_C_INCLUDES := C:\SDK\NDKOpencvTest1\app\src\main\jni\include
include $(PREBUILT_SHARED_LIBRARY)

最后一行是给我错误的那一行.

The last line is the one giving me the error.

这是树的层次结构:

http://imgur.com/a/G3I0y

我也尝试过此解决方案,但未成功:

I've also tried this solution without success: How to build FFmpeg (ver 3.1.1) on Android Studio (ver 2.1.2)

我一直在搜索我做错了几个小时的东西.

I've been searching what I am doing wrong for hours..

非常感谢您的帮助!

推荐答案

您的层次结构错误. 请按照下列步骤操作:

Your hierarchy is wrong. Follow these steps:

  1. 在jni文件夹中创建"lib"文件夹,然后根据目标文件夹放置共享库.看起来应该像:"jni/lib/armeabi-v7a/libfinal.so".

  1. Create 'lib' folder in jni folder and put your shared libraries according target folders. This should look like: 'jni/lib/armeabi-v7a/libfinal.so'.

仅预构建jni/lib文件夹中的这些.so库.为此,将此行LOCAL_SRC_FILES := libfinal.so更改为LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libfinal.so.这将在jni文件夹中搜索lib文件夹,然后根据您的cpu体系结构在目标文件夹中搜索libfinal.so lib.

Prebuilt only these .so libs which are in jni/lib folder. For this, change this line LOCAL_SRC_FILES := libfinal.so to LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libfinal.so. This will search lib folder in jni folder, and then this will search libfinal.so lib in targetted folder according your cpu architecture.

请注意您的gradle脚本.您应该像这样添加您的Android.mk文件,

Be aware of your gradle scripts. You should add your Android.mk file like this,

externalNativeBuild {
ndkBuild {
    path 'src/main/jni/Android.mk'
    }
}

构建Android.mk文件后,gradle会根据目标拱门将您预先构建的库放入main/jni文件夹中.为此,将此行添加到gradle中,

After building Android.mk file, gradle puts your prebuilt libraries in main/jni folder according to targetted archs. For this, add this line to gradle,

sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir 'src/main/libs'
    }

这篇关于在Android项目中链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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