在Android Studio中设置JNA [英] Setting up JNA in Android Studio
问题描述
我尝试将jna.jar导入到我的项目中,因为 JNA 是一个非常有用的工具,可以调用基于JNI的Native库。
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.
操作系统:Windows 10
OS: Windows 10
IDE:Android Studio 1.5.1
IDE: Android Studio 1.5.1
JDK:1.8.0_73
JDK: 1.8.0_73
NDK:r10e
我做了什么
(AS = Android Studio)
What I have done (AS = Android Studio)
- 用AS创建一个带有API18的新项目。
- 从他们的网站下载jna.jar GitHub上。
- Create a new project by AS with API18.
- download jna.jar from their GitHub.
JNATest\app\libs\ jna.jar
JNATest\app\libs\jna.jar
构建gradle
CLibrary.Instance.printf(Hello,JNA);
CLibrary.Instance.printf("Hello, JNA");
Android监视器上的错误消息
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
的 JA VA代码
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);
}
}
问题
根据错误消息,我在运行时错过 /android-arm/libjnidispatch.so
。
According to error message, I miss /android-arm/libjnidispatch.so
in runtime.
- 我为jna.jar写错了地方吗?
- 我应该如何使用和使用
/android-arm/libjnidispatch.so
?
- Did I put the wrong place for jna.jar ?
- How should I get and use
/android-arm/libjnidispatch.so
?
我是一个新手Android Studio,所以可能会误解一些关键点。
请随时给我一个提示,谢谢。
I am a newbie about Android Studio, so maybe misunderstanding something key-point. Please feel free to give me a hint, thank you.
推荐答案
将.so文件放在以下目录中(当使用android studio时): yourproject\app\src\main\jniLibs\armeabi-v7a \libjnidispatch.so
Put the .so file in the following directory (when using android studio): yourproject\app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so
更新:由于你们中的一些人询问在哪里找到* .so文件:
Update: Since some of you asked where to find the *.so file:
在官方JNA网站您将找到所有支持的架构(30+)供下载:
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
下载你喜欢的架构的jar并用一些拉链工具打开它。在那里你会找到libjnidispatch.so文件(当然只适用于unix架构。对于windows来说它是一个dll)
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)
这篇关于在Android Studio中设置JNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!