Linking shared library in Android project

佐手、 提交于 2019-12-17 20:31:19

问题


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

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

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

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.

Here is the tree hierarchy:

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..

Thanks a lot for the help!


回答1:


Your hierarchy is wrong. Follow these steps:

  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'.

  2. 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.

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

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

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


来源:https://stackoverflow.com/questions/43663633/linking-shared-library-in-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!