简体   繁体   English

如何将.dll导入Android Java项目(使用Eclipse)

[英]How to import .dll to Android java project (working with eclipse)

Java Native Interface (JNI) Java本机接口(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 . Java本机接口(JNI)是java的有趣接口之一,通过使用Java本机接口(JNI),您可以与其他应用程序和库一起使用

JNI is the native programming interface for java that is part of JDK. JNI是Java的本地编程接口,它是JDK的一部分。 Using JNI you can operate with other applications and libraries written in other language such as C,C++. 使用JNI,您可以使用以其他语言(例如C,C ++)编写的其他应用程序和库进行操作。 But the basic question arises when should I use the JNI ? 但是基本的问题出现在何时应该使用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. 您需要特定于平台的信息,而标准Java类库可能不支持您的应用程序所需的依赖于平台的功能。
  2. You have some library application written in other language and you want to use it in your java application. 您有一些用其他语言编写的库应用程序,并且想要在Java应用程序中使用它。
  3. You want Java should interact with some low level programming language. 您希望Java应该与某种底层编程语言进行交互。

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. 我们将使用的DLL是firstJNI.DLL。此DLL可以由VC ++或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. 这将创建firstJNI.h文件。 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 现在让我们看看如何使用VC ++生成DLL,单击: File-> New-> Win32Dynamic-Link Library输入名称并选择一个简单的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);

}

Now I have questions about how can I use .dll file written in C++/C in my java application. 现在,我对如何在Java应用程序中使用以C ++ / C编写的.dll文件有疑问。 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 ??? 我正在使用Eclipse开发适用于android的应用程序,并且我有一些dll文件,但我没有它们的源代码……如何在我的项目中使用它们?

First a disclaimer - I'm a bit sketchy on this, it's been a while since I've used JNI. 首先是免责声明-我对此有些粗略,自从我使用JNI已有一段时间了。

Many JNI examples assume you own the code for the library you want to call, which in my experience is rarely the case. 许多JNI示例假定您拥有要调用的库的代码,以我的经验,这种情况很少见。 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. 在该示例中,您可以看到javah util已用于生成头文件,并针对该头文件编写了cpp实现-这就是为什么您可以在cpp文件中看到jni头文件和各种Java关键字的原因。

In order to use a 3rd party dll, you first need the documentation for that dll, without that you're dead in the water. 为了使用第3方dll,首先需要该dll的文档,否则您将一无所获。 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. 需要文档的原因是,您将提供一个仅委托给第三方dll的包装dll-您需要知道如何调用它以及如何执行任何类型映射。 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. 显然,此包装器将包含所有JNI内容,以允许Java对该包装器进行调用,而该包装器又将调用第三方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. 有多种方法可以执行此操作,但我知道最简单的方法是使用SWIG ,它将生成包装dll所需的所有C ++代码。 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. 这也有助于拥有一个精通C ++的人-他们将非常宝贵地编写SWIG用来生成包装器代码的接口文件(.i或.swg文件)。

I'm not a expert on these but i think you should use System.loadLibrary("comparejni"); 我不是这些方面的专家,但我认为您应该使用System.loadLibrary("comparejni"); http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/ http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/

Hope it helped 希望能有所帮助

I don't think you're going to be able to use a Windows native library (.dll) in an Android application. 我认为您将无法在Android应用程序中使用Windows本机库(.dll)。 To load on Android, the code would need to be built into a .so. 要在Android上加载,需要将代码内置到.so中。

This Article is for Simple Java not for Android: 本文适用于简单Java,不适用于Android:

Calling DLLs from Java 从Java调用DLL

Calling C/C++ DLLs from Java 从Java调用C / C ++ DLL

Introduction 介绍

In this walkthrough, we will show how C/C++ code compiled into a Windows DLL can be called from Java using J/Invoke. 在本演练中,我们将展示如何使用J / Invoke从Java调用编译为Windows DLL的C / C ++代码。

We will be using Microsoft Visual Studio 2005 to create a DLL with C++ functions, and writing Java code with the help of the Eclipse Java IDE. 我们将使用Microsoft Visual Studio 2005创建具有C ++函数的DLL,并在Eclipse Java IDE的帮助下编写Java代码。 The steps for other IDEs will be similar. 其他IDE的步骤将相似。 We encourage you to 'walk-through' this exercise with us, even if you are not very familiar with Visual Studio or Eclipse - it is easy, and it is fun! 即使您对Visual Studio或Eclipse不太熟悉,我们也鼓励您与我们一起“逐步”完成本练习-这很容易,也很有趣!

To run this example, you should be running Windows 2000, XP, Vista or 2003. 要运行此示例,您应该运行Windows 2000,XP,Vista或2003。

J/Invoke requires a Java SE developer kit. J / Invoke需要Java SE开发人员工具包。 As it uses Java annotations, JDK version 5.0 or higher is needed. 由于它使用Java批注,因此需要JDK 5.0或更高版本。 Get Java here. 在这里获取Java。

Continue Reading ... 继续阅读 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM