如何导入.dll文件到Android的Java项目(使用Eclipse工作)文件、项目、工作、dll

2023-09-05 04:13:21 作者:丿噉檤灬 丿夨呿灬

Java本地接口(JNI)

Java Native Interface (JNI)

Java本地接口(JNI)是一个   该野趣接口在Java通过   使用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是JDK的一部分的本地编程接口的Java。使用JNI,你可以与其他应用程序,并用其他语言如C,C ++库操作。但是,当我应该使用JNI的基本问题是?

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 ?

您需要一些平台的信息和标准的Java类库可能不支持应用程序所需的平台相关的功能。 您有一些库应用程序用其它语言,你想用它在你的Java应用程序。 您希望Java应该有一些低层次的编程语言交互。 You want some platform specific information and the standard Java class library may not support the platform-dependent features needed by your application. You have some library application written in other language and you want to use it in your java application. 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);

我们将使用的DLL是firstJNI.DLL这个DLL可以通过VC ++或Borland的产生。我们将在后面讨论。

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

使用编译上面的code (这是什么意思?)

prompt>javac firstJNI.java

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

prompt>javah javah -jni HelloWorld

这将创建firstJNI.h文件。在头文件,你会看到

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

现在让我们看看如何使用VC ++生成的DLL,请点击:文件 - >新建 - > Win32Dynamic链接库 给出名称和选择 一个简单的DLL项目 你将会拥有 firstJNI.CPP文件 下面给出的firstJNI.cpp文件

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

}

现在我对我怎么可以用写在C ++ / C .dll文件在我的Java应用程序的问题。我开发的应用程序使用Eclipse Android和我有一些dll文件,我还没有自己的源代码...我如何使用他们在我的项目???

推荐答案

首先免责声明 - 我对这个有点简略,它已经有一段时间,因为我用JNI

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

许多JNI例子假设你拥有$ C $下要打电话,这在我的经验是很少的情况下,图书馆。在这个例子中,你的视线javah的UTIL已经被用来生成一个头文件,针对其实施CPP已被写入 - 这就是为什么你可以看到在CPP文件中的JNI头文件和各种Java关键字

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.

为了使用第三方的dll,首先需要的文档,该DLL,不,你是死在水中。你所需要的文档的原因是,你要提供一个简单的委托给第三方DLL中的包装DLL - 你需要知道如何称呼它,以及如何执行任何类型的映射。显然,这是该包装将包含所有的JNI的东西,以允许Java拨打电话到包装,这反过来又调用第三方的DLL。

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.

有各种方法可以做到这一点,但我知道最简单的方法是使用痛饮,这将产生所有所需包装DLL的C ++ code。它还可以帮助有别人的手知道C ++ - 他们将是非常宝贵的写作接口文件(.I或.swg文件),痛饮用来生成包装code

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.