如何将.dll导入Android java项目(使用eclipse)
Java Native Interface (JNI)
JNI是Java的本机编程接口,是JDK的一部分.使用JNI,您可以使用其他语言(如C,C)编写的其他应用程序和库.但是基本的问题出现在何时才能使用JNI? >您需要一些特定于平台的信息,标准Java类库可能不支持应用程序所需的与平台相关的功能. 下面给出简单示例;看看那些方法有’原生’KeyWord: public native void displayHelloWorld(); public native void displayOther(); private native String getLine(String prompt); 我们将要使用的DLL是firstJNI.DLL这个DLL可以由VC或borland生成.我们稍后会讨论. //firstJNI.java class firstJNI { public native void displayHelloWorld(); public native void displayOther(); private native String getLine(String prompt); static { System.loadLibrary("firstJNI");//This is firstJNI.DLL /*if generated by borland System.loadLibrary("firstjni");//This is firstjni.dll */ } public static void main(String[] args) { firstJNI JN=new firstJNI(); JN.displayHelloWorld(); JN.displayOther(); String input = JN.getLine("Enter Some Thing "); System.out.println("You Entered " + input); } } 使用编译上面的代码(这是什么意思?) prompt>javac firstJNI.java 然后使用创建头文件(这是什么意思?) prompt>javah javah -jni HelloWorld 这将创建firstJNI.h文件.在标题文件中,您将看到 ------------------------------------- JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld (JNIEnv *,jobject); /* * Class: firstJNI * Method: displayOther * Signature: ()V */ JNIEXPORT void JNICALL Java_firstJNI_displayOther (JNIEnv *,jobject); /* * Class: firstJNI * Method: getLine * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_firstJNI_getLine (JNIEnv *,jobject,jstring); ---------------------------------------------- 不要编辑标题文件 现在让我们看看如何使用VC生成DLL,点击:File-> New-> Win32Dynamic-Link Library // MYVCDLL.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "D:KanadStudycodeToad ArticlesfirstJNI.h" #include "jni.h" //can copy or give full path #include <math.h> BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved ) { return TRUE; } extern "C" __declspec(dllexport) int getMemorySize(); //And your function definition should look like this: extern "C" __declspec(dllexport) int getMemorySize() { //do something MEMORYSTATUS memoryStatus; int MB=1024*1024 ; double memSize; memoryStatus.dwLength=sizeof(MEMORYSTATUS); GlobalMemoryStatus(&memoryStatus); __int64 size= memoryStatus.dwTotalPhys; memSize=(double)size/MB; printf("nTotal Memory %.0lf MB",ceil(memSize)); return 0; } JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld(JNIEnv *env,jobject obj) { printf("Hello world! This is using VC++ DLLn"); } JNIEXPORT void JNICALL Java_firstJNI_displayOther(JNIEnv *env,jobject obj) { printf("Hello world! This is using VC++ DLL Other Function n"); getMemorySize(); } JNIEXPORT jstring JNICALL Java_firstJNI_getLine(JNIEnv *env,jobject obj,jstring enter) { char buf[128]; const char *str = env->GetStringUTFChars(enter,0); printf("n%s",str); env->ReleaseStringUTFChars(enter,str); scanf("%s",buf); return env->NewStringUTF(buf); } 现在我对如何在我的java应用程序中使用C/C++编写的.dll文件有疑问.我正在使用Eclipse开发android的应用程序,我有一些dll文件,我没有他们的源…我如何在我的项目中使用它们?
首先是免责声明 – 我对此有点粗略,因为我使用过JNI已经有一段时间了.
许多JNI示例假设您拥有要调用的库的代码,根据我的经验很少这样.在示例中,您可以看到javah util已用于生成头文件,编写了cpp实现 – 这就是为什么您可以在cpp文件中看到jni头文件和各种Java关键字的原因. 为了使用第三方dll,你首先需要该dll的文档,而不是你已经死在水中.您需要文档的原因是您将提供一个简单地委托给第三方dll的包装器dll – 您需要知道如何调用它以及如何执行任何类型映射.显然,这个包装器将包含所有JNI内容,以允许Java调用该包装器,后者又调用第三方dll. 有各种方法可以做到这一点,但我知道最简单的方法是使用SWIG,它将生成包装器DLL所需的所有C代码.它也有助于让有人知道C – 他们将是SWIG用来生成包装器代码的非常宝贵的编写接口文件(.i或.swg文件). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Microsoft是否有关于在不同Windows平台上存储App Data vs用
- 如何从Windows上的Perl CGI脚本分叉后台进程?
- windows-8 – 在Windows 8上禁用了D??irectX控制面板上的某
- 关闭开机硬盘自检的方法
- 有没有找到Windows DLL的MD5或其他校验和的地方?
- windows – 如何向资源管理器右键单击上下文菜单添加新选项
- windows – Webstorm 11中的菜单和工具栏字体大小
- active-directory – 如何登录不信任自己的域控制器
- ActiveX Edge在Microsoft Edge中有替代品吗?
- windows-7 – 如何在Windows 7上使用Python 3和httplib2调用
- hyperledger-fabric – Windows上的Fabric作曲家
- windows-8 – 下载windows phone sdk 8 offline
- 相当于.NET中的Windows错误代码
- Windows 客户端802.1x的一些设置
- 更新Windows Server补丁会导致Exchange工作不正常
- Windows XP中的Python Cx_Freeze错误
- windows-7 – 无法完成操作,因为文件夹或其中的文
- active-directory – 更改不同的域帐户而不是管理
- 窗口 – 警告 – 提供资源不可访问运行wevtutil
- windows-server-2008 – schtasks – 创建没有按