Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NDK-compiled JNI library in a normal non-Android Java application?

I have a JNI library that was made to be used by Android app with NDK. I do not have sources, only compiled .so files for some archs.

I want to call functions from this library in a simple console Java application on a 64-bit x86 linux PC.

What I've done:

  1. I took the library from x86_64 folder and loaded it in my Java program using System.loadLibrary
  2. Tried to launch the program and got UnsatisfiedLinkError with details liblog.so: cannot open shared object file: No such file or directory.
  3. So I took liblog.so for x86_64 arch from official NDK and loaded it too, then got a same error with libstdc++.so: cannot open shared object file: No such file or directory
  4. Did the same thing for libstdc++.so library and got this /usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header.
  5. Opened libc.so and it appeared to be just a text file... Replaced it with the proper libc.so file
  6. Finally got UnsatisfiedLinkError for my library with undefined symbol: __stack_chk_guard comment. Looks like my library is strongly tightened with Android things

What should I do to use this library in my normal program without anything Android-specific?

like image 369
Anna Prosvetova Avatar asked Sep 21 '16 22:09

Anna Prosvetova


People also ask

How does Android APK execute compiled native code using the Java native interface?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

What is NDK and JNI?

JNI is just the way that Java handles calling into native/C++ code, and calling back into Java from there. It has nothing to say about Android - it is a Java language feature. The Android NDK is a way to write Android applications using code called by JNI.


1 Answers

Just in few words - Android and desktop Linux binaries are incompatible, even if they are targeted to the same CPU architecture.

like image 137
Sergio Avatar answered Nov 14 '22 21:11

Sergio