Java Native Interface (JNI)如何導入.dll文件到Android的Java項目(使用Eclipse工作)
Java本地接口(JNI)是由 Java中的野趣接口一個通過 使用Java Native Interface(JNI )您可以使用 和其他應用程序 和庫。
JNI是Java的本地編程接口,它是JDK的一部分。使用JNI,您可以使用其他語言(如C,C++)編寫的其他應用程序和庫進行操作。但是基本問題出現在什麼時候應該使用JNI?
- 您需要一些特定於平臺的信息,標準Java類庫可能不支持應用程序所需的平臺相關功能。
- 您有一些使用其他語言編寫的庫應用程序,並且您想在您的Java應用程序中使用它。
- 您希望Java應該與一些低級編程語言交互。
以下給出簡單的例子;請參閱方法有「本土」關鍵字:
public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);
我們將要使用的DLL是可以通過VC生成firstJNI.DLL這個DLL ++或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,請點擊:文件 - >新建 - > Win32Dynamic鏈接庫 給名稱並選擇 一個簡單的DLL項目 您將有 firstJNI.CPP文件 下面給出firstJNI.cpp文件
// MYVCDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "D:\Kanad\Study\codeToad Articles\firstJNI.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++ DLL\n");
}
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文件有疑問。我正在開發Android應用程序使用Eclipse,我有一些DLL文件,我沒有他們的來源...我怎樣才能在我的項目中使用它們?
所以,你設法Dll文件添加到您的原生Android項目?我有點卡住,看看這裏http:// stackoverflow。com/questions/28393305/using-dll-library-in-android-application – 2015-02-08 14:25:02