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


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:

│   │
│   └───com.example.myproject
│       │   MainActivity.kt
|   │   native-lib.cpp
|   │   CMakeLists.txt
|   │
|   └───mylib
|       │   Number.hpp
|       |   Number.cpp
    │   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)
add_library(myproject SHARED native-lib.cpp)
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?


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