9

I try to import jna.jar into my project since JNA is a very useful tool to call Native library which is base on JNI.

OS: Windows 10

IDE: Android Studio 1.5.1

JDK: 1.8.0_73

NDK: r10e

What I have done (AS = Android Studio)

  1. Create a new project by AS with API18.

  2. Download jna.jar from their GitHub.

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

  3. copy jna.jar into project folder.

    JNATest\app\libs\jna.jar

  4. In AS, right-click on the icon of jna.jar, choose Add as Library
  5. Wait for few seconds, check the File->Project Structure->app->Dependencies. We do have the jna.jar. (Same as app\build.gradle) build gradle
  6. Implement JAVA code about JNA in MainActivity.java
  7. Run app on real device Sony Z3 (arm)
  8. Crash by CLibrary.Instance.printf("Hello, JNA");

Error Message on Android Monitor

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 error message, I miss /android-arm/libjnidispatch.so in runtime.

  1. Did I put the wrong place for jna.jar?

  2. How should I get and use /android-arm/libjnidispatch.so?

I am a newbie about Android Studio, so maybe misunderstanding something key-point.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Pow Lee
  • 91
  • 1
  • 1
  • 3

3 Answers3

13

For Android, reference the JNA library adding @aar at the end of the string instead of downloading the JNA jar:

https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#jna-on-android

Marcelo
  • 131
  • 1
  • 2
11

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

Update I: (up to version <= 4.3.0) Since some of you asked where to find the *.so file:

On the official JNA site you will find all the supported architectures (30+) for download:

https://github.com/java-native-access/jna/tree/master/lib/native

Download the jar of the architecture you'd like and open it with some zip tool. In there you'll find the libjnidispatch.so file (of course only for unix architectures. For windows its a dll)

Update II: (starting from version >= 4.4.0)

Use the jna-X.X.0.aar file, supplied by the JNA project

As mentioned in a comment - starting from version 4.4.0 JNA publishes an AAR to maven central with all the libjnidispatch.so's in it. Users have had better luck using gradle than straight maven here, which doesn't always select or properly handle aars.

fuzzyTew
  • 3,511
  • 29
  • 24
Lonzak
  • 9,334
  • 5
  • 57
  • 88
2

I found this comment in a file in the library github repo - "If you're using Google's Eclipse plugin then you must manually remove libjnidispatch.so from jna.jar/lib/armeabi and add it into your project's libs/armeabi directory."

Since this file was created in 2012 and Android Studio was still in very early phase and not super popular by that time, I assume it might be a valid note for Eclipse and also for Android Studio. I suggest you try it.

Lachezar
  • 6,523
  • 3
  • 33
  • 34
  • Thank you for this information. But I hit the wall when building the JNA source code. http://imgur.com/VQbH1F9 I will keep trying. – Pow Lee Mar 30 '16 at 10:42
  • I build successfully by deleting some characters which are UTF-8 escape char. Trying to do rest of steps.. – Pow Lee Mar 31 '16 at 02:26
  • You don't need to rebuild JNA; just extract the file from jna.jar and place it into your project's native libs directory. – technomage Mar 31 '16 at 14:42