日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java调用jni接口,Java 中通过jni接口调用native code

發布時間:2024/1/23 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用jni接口,Java 中通过jni接口调用native code 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[??? Java語言本身是通過Java的虛擬機解釋執行的,因此對于Java中調用本地動態鏈接庫的問題便提上了日程,為何會存在這樣的需求呢?因為Java本身的機制導致一些要求高效率

在上上篇中已經介紹了如何編譯native code 生成 so動態庫,這里再介紹下Java中如何通過JNI接口來調用動態庫中的native code方法。

通過此篇文章,我們可以認識和了解如何完整地在Android的java code中調用native code。

1 創建一個 android project, 名字叫Why

2 在工程Why中添加一個Java類,class名為Jni。這個類是一個JNI接口的Java類,文件名為Jni.java。

package com.yarin.android.Why;

public class Jni {

public native int getCInt();

public native String getCString();

}

3 將工程Why下的 "src\com\yarin\android\Why" 目錄下的Jni.java文件copy到“Why\bin\classes”下.

4? Generate Jni.class file via the command below:

javac jni.java

Then copy the Jni.class file generated to cover and replace the original Jni.class file in directory “Why\bin\classes\com\yarin\android\Why”.

------The original Jni.class file, you must build the java project first to generate it.

5? Generate c style header file

javah –classpath C:\android-ndk-r6b\myproject\Why\bin\classes? com.yarin.android.Why.Jni

com.yarin.android.Why is my package name appeared in Jni.java file.

com_yarin_android_Why_Jni.h is like below:

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class com_yarin_android_Why_Jni */

#ifndef _Included_com_yarin_android_Why_Jni

#define _Included_com_yarin_android_Why_Jni

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:???? com_yarin_android_Why_Jni

* Method:??? getCInt

* Signature: ()I

*/

JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt

(JNIEnv *env, jobject object); ---you need to supplement the parameter name yourself

/*

* Class:???? com_yarin_android_Why_Jni

* Method:??? getCString

* Signature: ()Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString

(JNIEnv *env, jobject object);

#ifdef __cplusplus

}

#endif

#endif

6???? Add com_yarin_android_Why_Jni.c file corresponding to the above c style header file, then add implemented code.

#include

#include

#include "com_yarin_android_Why_Jni.h"

int add()

{

int x,y;

x = 111;

y = 22;

x += y;

return x;

}

JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt

(JNIEnv *env, jobject object)

{

return add();

}

JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString

(JNIEnv *env, jobject object)

{

(*env)->NewStringUTF(env, " Why is ok ^_^ ----->> ");

}

7 然后實現在工程Why下創建目錄jni,并且copy com_yarin_android_Why_Jni.c /h 到jni目錄下。

8 在"Why\jni"目錄下編寫Android.mk ,在"android-ndk-r6b\jni"下編寫Application.mk , 然后在NDK環境下編譯native code,生成動態庫libWhy.so。這個你已經從上上篇中知道的……。

9 在java工程中加入調用native 動態庫的代碼:[在Android項目中 編寫c++動態鏈接庫, 通過jni調用]

package com.yarin.android.Why;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class WhyActivity extends Activity {

static

{

System.loadLibrary("Why");

}

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Jni jni = new Jni();

TextView view = new TextView(this);

view.setText(jni.getCString() + Integer.toString(jni.getCInt()));

setContentView(view);

}

}

10? 編譯Why工程,然后 run as Android Application, 你就能看到native code的調用結果了。

Tip:

Usage of the command javah.

javah -d -classpath

where:

'outputdir' is the directory where to put the generated header file

'classpath' contains an absolute path to the directory containing your root package (as mentionned by Glen)

'fully_qualified_class' is the name of the class containing native methods without .class extension

-jni option is not required (set by default)

[1. JNIEnv對象???對于本地函數? ?JNIEXPORT void JNICALL Java_video1_TestNative_sayHello(JNIEnv * env, jobject obj)? ?{ ?? ? ? cout? ?} ?? ?? ? ? JNIEnv類型代表Ja

總結

以上是生活随笔為你收集整理的java调用jni接口,Java 中通过jni接口调用native code的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。