加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-13 20:43:50 所属栏目:Windows 来源:网络整理
导读: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是Java的本机编程接口,是JDK的一部分.使用JNI,
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是Java的本机编程接口,是JDK的一部分.使用JNI,您可以使用其他语言(如C,C)编写的其他应用程序和库.但是基本的问题出现在何时才能使用JNI?

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

下面给出简单示例;看看那些方法有’原生’KeyWord:

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

不要编辑标题文件

现在让我们看看如何使用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:KanadStudycodeToad ArticlesfirstJNI.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++ DLLn");

}

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文件,我没有他们的源…我如何在我的项目中使用它们?

首先是免责声明 – 我对此有点粗略,因为我使用过JNI已经有一段时间了.

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

为了使用第三方dll,你首先需要该dll的文档,而不是你已经死在水中.您需要文档的原因是您将提供一个简单地委托给第三方dll的包装器dll – 您需要知道如何调用它以及如何执行任何类型映射.显然,这个包装器将包含所有JNI内容,以允许Java调用该包装器,后者又调用第三方dll.

有各种方法可以做到这一点,但我知道最简单的方法是使用SWIG,它将生成包装器DLL所需的所有C代码.它也有助于让有人知道C – 他们将是SWIG用来生成包装器代码的非常宝贵的编写接口文件(.i或.swg文件).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读