JNA setup in Android Studio

I am trying to import jna.jar into my project, since JNA is a very useful tool for invoking a JNI-based Native library.

OS: Windows 10

IDE: Android Studio 1.5.1

JDK: 1.8.0_73

NDK: r10e

What I did (AS = Android Studio)

  • Create a new AS project using API18.
  • download jna.jar from your github.

    https://github.com/java-native-access/jna

  • copy the jna.jar file to the project folder.

    JNATest \ application \ LIES \ jna.jar

  • In AS, right-click on the jna.jar icon, select Add As Library
  • Wait a few seconds, check β€œFile-> Project Structure-> Application-> Dependencies. We have jna.jar. (Same as application \ build.gradle) build gradle
  • Embed JAVA code about JNA in MainActivity.java
  • Run the application on a real device Sony Z3 (arm)
  • Crash CLibrary.Instance.printf("Hello, JNA");

Android monitor error message

 E/AndroidRuntime: FATAL EXCEPTION: main Process: i3d.jnatest, PID: 1068 java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-arm/libjnidispatch.so) not found in resource path (.) at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:866) at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:826) at com.sun.jna.Native.<clinit>(Native.java:140) .. ... so on 

JAVA code

 package i3d.jnatest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.sun.jna.Library; import com.sun.jna.Native; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CLibrary.Instance.printf("Hello, JNA"); } public interface CLibrary extends Library { CLibrary Instance = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class); void printf(String format, Object... args); } } 

Question

According to the error message, I skip /android-arm/libjnidispatch.so at runtime.

  • Did I put the wrong place for jna.jar?
  • How do I get and use /android-arm/libjnidispatch.so ?

I am new to Android Studio, so maybe there’s a misunderstanding of something key. Please feel free to give me a hint, thanks.

+5
source share
2 answers

put the .so file in the following directory (when using android studio): yourproject\app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so

+3
source

I found this comment in a file in the github repo library - " If you use the Google Eclipse plugin, you need to manually remove libjnidispatch.so from jna.jar / lib / armeabi and add it to the libs / armeabi project directory. "

Since this file was created in 2012, and Android Studio was still at a very early stage, and by that time not very popular, I assume that this may be a valid note for Eclipse, as well as for Android Studio. I suggest you try.

+1
source

Source: https://habr.com/ru/post/1246062/


All Articles