[FIXED] Android NDK/JNI — shared library incompatible with armelf_linux_eabi

Issue

I’m trying to use Android NDK and JNI to call a C++ function from Kotlin in my Android Studio project.

Here’s my Android project structure:

myproject/app/src/main  
│
└───java
│   │
│   └───com.example.myproject
│       │   MainActivity.kt
│   
└───cpp
|   │   native-lib.cpp
|   │   CMakeLists.txt
|   │
|   └───mylib
|       │   Number.hpp
|       |   Number.cpp
│   
└───jniLibs
    │   libmylib.so

Notice, I’ve already built this program into libmylib.so, and saved it to src/main/jniLibs. I want to use this built library, rather than Number.cpp directly.

My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.10.2)
project("myproject")
add_library(myproject SHARED native-lib.cpp)
add_library(MYLIB SHARED IMPORTED)
set_target_properties(MYLIB PROPERTIES IMPORTED_LOCATION jniLibs/libmylib.so)
target_link_libraries(myproject MYLIB)

Yet I get this error when building:

C/C++: ld: error: jniLibs/libmylib.so is incompatible with armelf_linux_eabi

How can I get cmake to properly link the library to my project?

Solution

The linker error suggests that you are building myproject for a different ABI than the one that libmylib.so was built for.

You can tell Gradle to only build your native code for one or more specific ABI(s) by adding an ABI filter:

android {
    defaultConfig {
        ndk {
            abiFilters 'arm64-v8a'
        }
    }
    ... other stuff ...
}

Answered By – Michael

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published