如何将.dll导入Android java项目(使用eclipse)

发布于 2024-11-05 13:52:55 字数 4352 浏览 3 评论 0原文

Java 本机接口 (JNI)

Java 本机接口 (JNI) 是其中之一 java 的有趣界面 使用 Java 本机接口 (JNI) 可以与其他应用程序一起运行 和图书馆

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

  1. 您需要一些特定于平台的信息,而标准 Java 类库可能不支持您的应用程序所需的平台相关功能。
  2. 您有一些用其他语言编写的库应用程序,并且您想使用它在你的java应用程序中。
  3. 你希望Java应该与一些低级编程语言交互。

下面给出了简单的例子;看到方法有'native'关键字:

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

Don't edit header File

现在让我们看看如何使用 VC++ 生成 DLL,点击:文件->新建->Win32Dynamic-Link Library 给出名称并选择 一个简单的DLL项目 你将会有 第一个JNI.CPP文件 下面给出了第一个JNI.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 文件有疑问。我正在使用 Eclipse 开发 Android 应用程序,我有一些 dll 文件,但没有它们的源代码...我如何在我的项目中使用它们???

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 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老子叫无熙 2024-11-12 13:52:55

首先是免责声明 - 我对此有点粗略,自从我使用 JNI 以来已经有一段时间了。

许多 JNI 示例假设您拥有要调用的库的代码,但根据我的经验,这种情况很少见。在示例中,您看到 javah util 已用于生成头文件,并针对该头文件编写了 cpp 实现 - 这就是为什么您可以在 cpp 文件中看到 jni 头文件和各种 Java 关键字。

为了使用第 3 方 dll,您首先需要该 dll 的文档,否则您就一事无成。您需要文档的原因是您将提供一个简单委托给第 3 方 dll 的包装器 dll - 您需要知道如何调用它以及如何执行任何类型映射。显然,这个包装器将包含所有 JNI 内容,以允许 Java 调用该包装器,进而调用第 3 方 dll。

有多种方法可以做到这一点,但我知道的最简单的方法是使用 SWIG,它将生成所有 C++ 代码包装器 dll 所需的。拥有了解 C++ 的人也很有帮助 - 他们在编写 SWIG 用于生成包装器代码的接口文件(.i 或 .swg 文件)时将非常有用。

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.

肤浅与狂妄 2024-11-12 13:52:55

我不是这方面的专家,但我认为你应该使用 System.loadLibrary("comparejni");
http://blog.jayway.com/2010/ 01/25/boosting-android-performance-using-jni/

希望有帮助

I'm not a expert on these but i think you should use System.loadLibrary("comparejni");
http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/

Hope it helped

灯角 2024-11-12 13:52:55

我认为您无法在 Android 应用程序中使用 Windows 本机库 (.dll)。要在 Android 上加载,需要将代码构建到 .so 中。

I don't think you're going to be able to use a Windows native library (.dll) in an Android application. To load on Android, the code would need to be built into a .so.

七秒鱼° 2024-11-12 13:52:55

本文适用于简单 Java,不适用于 Android:

调用来自 Java 的 DLL

从 Java 调用 C/C++ DLL

简介

在本演练中,我们将展示如何使用 J/Invoke 从 Java 调用编译为 Windows DLL 的 C/C++ 代码。

我们将使用 Microsoft Visual Studio 2005 创建带有 C++ 函数的 DLL,并在 Eclipse Java IDE 的帮助下编写 Java 代码。其他 IDE 的步骤类似。我们鼓励您与我们一起“演练”此练习,即使您不太熟悉 Visual Studio 或 Eclipse - 这很简单,而且很有趣!

要运行此示例,您应该运行 Windows 2000、XP、Vista 或 2003。J

/Invoke 需要 Java SE 开发人员工具包。由于使用Java注解,因此需要JDK版本5.0或更高版本。在这里获取 Java。

继续阅读...

This Article is for Simple Java not for Android:

Calling DLLs from Java

Calling C/C++ DLLs from Java

Introduction

In this walkthrough, we will show how C/C++ code compiled into a Windows DLL can be called from Java using J/Invoke.

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. The steps for other IDEs will be similar. 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!

To run this example, you should be running Windows 2000, XP, Vista or 2003.

J/Invoke requires a Java SE developer kit. As it uses Java annotations, JDK version 5.0 or higher is needed. Get Java here.

Continue Reading ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文