如何导入.dll到Android的java项目(使用eclipse) [英] How to import .dll to Android java project (working with eclipse)

查看:162
本文介绍了如何导入.dll到Android的java项目(使用eclipse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Native Interface(JNI)


Java本机接口(JNI)是由
之一的空间界面由java By
使用Java Native Interface(JNI)你
可以与其他应用程序一起运行
和库


JNI是本地编程接口对于作为JDK的一部分的java。使用JNI,您可以使用其他语言(如C,C ++)编写的其他应用程序和库。但是基本的问题出在什么时候应该使用JNI?


  1. 你想要一些平台特定的信息和标准的Java类库不支持应用程序所需的平台相关功能。

  2. 您有一些使用其他语言编写的库应用程序,并希望在您的java应用程序中使用它。

  3. 您希望Java应该与一些低级编程语言互动。






下面给出了Simple Example;看到那个方法有'native'KeyWord:

  public native void displayHelloWorld(); 
public native void displayOther();
private native String getLine(String prompt);

我们要使用的是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); //这是firstJNI.DLL
/ *如果由borland
生成System.loadLibrary(firstjni ); //这是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(你输入+输入);
}
}

使用编译上述代码这意味着?)

 提示> javac firstJNI.java 
/ pre>

然后使用创建头文件(这是什么意思?)

  prompt> javah javah -jni HelloWorld 

这将创建firstJNI .h文件。在标题文件中,您将看到

  ------------------- ------------------ 
JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld
(JNIEnv *,jobject);

/ *
*类:firstJNI
*方法:displayOther
*签名:()V
* /
JNIEXPORT void JNICALL Java_firstJNI_displayOther
(JNIEnv *,jobject);

/ *
*类:firstJNI
*方法:getLine
*签名:(Ljava / lang / String;)Ljava / lang / String;
* /
JNIEXPORT jstring JNICALL Java_firstJNI_getLine
(JNIEnv *,jobject,jstring);
----------------------------------------------

不要编辑头文件

现在看看如何使用VC ++生成DLL,单击: File-> New-> Win32Dynamic-Link Library
给出名称并选择
一个简单的DLL项目
你将
firstJNI.CPP文件
下面给出了firstJNI.cpp文件

  // MYVCDLL。 cpp:定义DLL应用程序的入口点。 
//

#includestdafx.h
#includeD:\Kanad\Study\codeToad articles\firstJNI.h
#includejni.h//可以复制或给出完整路径
#include< math.h>

BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved

{
return TRUE;
}

externC__declspec(dllexport)int getMemorySize();
//你的函数定义应该是这样的:
externC__declspec(dllexport)int getMemorySize()
{//做某事

MEMORYSTATUS memoryStatus ;
int MB = 1024 * 1024;
double memSize;
memoryStatus.dwLength = sizeof(MEMORYSTATUS);

GlobalMemoryStatus(& memoryStatus);

__int64 size = memoryStatus.dwTotalPhys;

memSize =(double)size / MB;

printf(\\\
Total Memory%.0lf MB,ceil(memSize));

return 0;
}

JNIEXPORT void JNICALL
Java_firstJNI_displayHelloWorld(JNIEnv * env,jobject obj)
{
printf(Hello world!这是使用VC ++ DLL \\ \
);

}

JNIEXPORT void JNICALL
Java_firstJNI_displayOther(JNIEnv * env,jobject obj)
{

printf世界!这是使用VC ++ DLL其他功能\\\
);
getMemorySize();

}

JNIEXPORT jstring JNICALL
Java_firstJNI_getLine(JNIEnv * env,jobject obj,jstring enter)
{

char BUF [128];
const char * str = env-> GetStringUTFChars(enter,0);
printf(\\\
%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的包装器,您需要知道如何调用它以及如何执行任何类型的映射。显然,这个包装器将包含所有的JNI东西,以允许Java调用该包装器,这又调用第三方dll。



有各种方法这样做,但我知道的最简单的方法是使用 SWIG ,它将生成包装器所需的所有C ++代码DLL。它也有助于有人知道C ++在手 - 他们将是无价的编写界面文件(.i或.swg文件),SWIG用来生成包装器代码。


Java Native Interface (JNI)

Java Native Interface (JNI) is one of the intersting interface by java By using Java Native Interface (JNI) you can operate with other applications and libraries.

JNI is the native programming interface for java that is part of JDK. Using JNI you can operate with other applications and libraries written in other language such as C,C++. But the basic question arises when should I use the JNI ?

  1. You want some platform specific information and the standard Java class library may not support the platform-dependent features needed by your application.
  2. You have some library application written in other language and you want to use it in your java application.
  3. You want Java should interact with some low level programming language.


Below is given Simple Example; See that methods have 'native' KeyWord:

public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);

The DLL we are going to use is firstJNI.DLL This DLL can be generated by VC++ or borland. Which we will discuss later.

//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); 
     }
}

Compile the above code using (What does this mean ?)

prompt>javac firstJNI.java

Then create header File using (What does this mean ?)

prompt>javah javah -jni HelloWorld

This will create firstJNI.h file. In the header File you will see

-------------------------------------
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);
----------------------------------------------

Don't edit header File

Now let see how to generate DLL using VC++, Click: File->New->Win32Dynamic-Link Library Give name and Select A simple DLL project You will have firstJNI.CPP file Below is given the firstJNI.cpp file

// 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);

}

Now I have questions about how can I use .dll file written in C++/C in my java application. I am developing application for android using Eclipse and I have some dll files and I haven't their source ... How can I use them in my project ???

解决方案

First a disclaimer - I'm a bit sketchy on this, it's been a while since I've used JNI.

Many JNI examples assume you own the code for the library you want to call, which in my experience is rarely the case. In the example you sight the javah util has been used to generate a header file, against which cpp implementation has been written - this is why you can see the jni header file and various Java keywords in the cpp file.

In order to use a 3rd party dll, you first need the documentation for that dll, without that you're dead in the water. The reason you need the documentation is that you're going to provide a wrapper dll that simply delegates to the 3rd party dll - you need to know how to call it and how to perform any type mappings. Obviously it's this wrapper that will contain all the JNI stuff to allow Java to make the call to that wrapper, which in turn calls the 3rd party dll.

There's various ways to do this but the easiest way I know is to use SWIG, which will generate all the C++ code required for the wrapper dll. It also helps to have someone that knows C++ on hand - they'll be invaluable writing interface files (.i or .swg files) that SWIG uses to generate the wrapper code.

这篇关于如何导入.dll到Android的java项目(使用eclipse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆