Linking shared library in Android project
Asked Answered
R

1

1

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:

https://i.sstatic.net/JD3Pt.jpg

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!

Raynor answered 27/4, 2017 at 16:47 Comment(2)
There's a more detailed error log somewhere. I'm not a Studio/gradle user myself so idk where, but similar questions wrt "finished with non-zero exit value" pop up all the time.Background
Possible duplicate of How to include *.so library in Android Studio?Diphyllous
C
2

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'
    }
Clanton answered 28/4, 2017 at 7:1 Comment(1)
How libfinal.so files are generated. Basically I have jni source code!!! Thanks in advance.Phylissphyll

© 2022 - 2024 — McMap. All rights reserved.