Android JNI(實現自己的JNI_OnLoad函數)

來源:互聯網
上載者:User

實現JNI中本地函數註冊可以兩種方式:
(1)採用預設的本地函數註冊流程。
(2)自己重寫JNI_OnLoad()函數。(本文介紹)(Android中採用這種)

Java端代碼:

package com.jni;public class JavaHello {public static native String hello();static {// load library: libtest.sotry {System.loadLibrary("test");} catch (UnsatisfiedLinkError ule) {System.err.println("WARNING: Could not load library!");}}public static void main(String[] args) {String s = new JavaHello().hello();System.out.println(s);}}

本地C語言代碼:

#include <stdlib.h>#include <string.h>#include <stdio.h>#include <jni.h>#include <assert.h>JNIEXPORT jstring JNICALL native_hello(JNIEnv *env, jclass clazz){printf("hello in c native code./n");return (*env)->NewStringUTF(env, "hello world returned.");}#define JNIREG_CLASS "com/jni/JavaHello"//指定要註冊的類/*** Table of methods associated with a single class.*/static JNINativeMethod gMethods[] = {{ "hello", "()Ljava/lang/String;", (void*)native_hello },//綁定};/** Register several native methods for one class.*/static int registerNativeMethods(JNIEnv* env, const char* className,        JNINativeMethod* gMethods, int numMethods){jclass clazz;clazz = (*env)->FindClass(env, className);if (clazz == NULL) {return JNI_FALSE;}if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {return JNI_FALSE;}return JNI_TRUE;}/** Register native methods for all classes we know about.*/static int registerNatives(JNIEnv* env){if (!registerNativeMethods(env, JNIREG_CLASS, gMethods, 
sizeof(gMethods) / sizeof(gMethods[0])))return JNI_FALSE;return JNI_TRUE;}/** Set some test stuff up.** Returns the JNI version on success, -1 on failure.*/JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){JNIEnv* env = NULL;jint result = -1;if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {return -1;}assert(env != NULL);if (!registerNatives(env)) {//註冊return -1;}/* success -- return valid version number */result = JNI_VERSION_1_4;return result;}

編譯及運行流程:

1 設定三個環境變數:
export JAVA_HOME:=/usr/lib/jvm/java-6-sun-1.6.0.15
export JAVA_SRC_PATH:=/home/kortide/Jackey/jni/jni_onload/com/jfo
export NATIVE_SRC_PATH:=/home/kortide/Jackey/jni/jni_onload/jni

2 編譯JavaHello.java:
javac $JAVA_SRC_PATH/JavaHello.java

3. 編譯NativeHello.c,產生共用庫
gcc -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -c -o $NATIVE_SRC_PATH/NativeHello.o  $NATIVE_SRC_PATH/NativeHello.c

gcc -fPIC -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -o $NATIVE_SRC_PATH/libtest.so $NATIVE_SRC_PATH/NativeHello.o

4. 運行
java com/jni/JavaHello

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.