Android JNI 調用 C/C++
來源:程序員人生 發布時間:2015-02-04 08:35:15 閱讀次數:2785次
Android JNI 調用 C/C++ 接口
Android 使用 NDK 原生支持調用 c/c++ 接口的代碼,只需要在程序中依照 android jni 規范編程就能夠直接使用。
C 語言版本
JNI 調用 c 語言相對簡單,命名1個 jni 函數,系統會自動注冊到 Java 虛擬機,然后 Java 代碼里面可以直接調用:
Native 代碼:
#include <jni.h>
int add(int x, int y) {
return x + y;
}
jint
Java_com_example_android_simplejni_SimpleJNI_add( JNIEnv* env,
jobject this,
jint x,
jint y )
{
return add(x, y);
需要注意的是函數名必須是跟 Java 類對應的,比如例子中的包為: package com.example.android.simplejni; 類名為: public class SimpleJNI
方法名為: add
然后 Java 類代碼里面可以直接加載庫,聲明為自己的方法:
package com.example.android.simplejni;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SimpleJNI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
System.loadLibrary("simplejni");
int sum = add(2, 3);
tv.setText("2 + 3 = " + Integer.toString(sum));
setContentView(tv);
}
public native int add(int x, int y);
}
C++ 版本
c++ 語言由于跟 Java 1樣是面向對象的設計語言,具有更多的特性,只是傳遞函數就不需要 c++ 了。 那末使用 C++ 代碼注冊到 Java 虛擬機就相對麻煩1些。
需要手動注冊方法: 直接給 代碼 吧:development/samples/SimpleJNI/jni/native.cpp
#define LOG_TAG "simplejni native.cpp"
#include <utils/Log.h>
#include <stdio.h>
#include "jni.h"
static jint
add(JNIEnv *env, jobject thiz, jint a, jint b) {
int result = a + b;
ALOGI("%d + %d = %d", a, b, result);
return result;
}
static const char *classPathName = "com/example/android/simplejni/Native";
static JNINativeMethod methods[] = {
{"add", "(II)I", (void*)add },
};
/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = env->FindClass(className);
if (clazz == NULL) {
ALOGE("Native registration unable to find class '%s'", className);
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
ALOGE("RegisterNatives failed for '%s'", className);
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Register native methods for all classes we know about.
*
* returns JNI_TRUE on success.
*/
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env, classPathName,
methods, sizeof(methods) / sizeof(methods[0]))) {
return JNI_FALSE;
}
return JNI_TRUE;
}
// ----------------------------------------------------------------------------
/*
* This is called by the VM when the shared library is first loaded.
*/
typedef union {
JNIEnv* env;
void* venv;
} UnionJNIEnvToVoid;
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
UnionJNIEnvToVoid uenv;
uenv.venv = NULL;
jint result = ⑴;
JNIEnv* env = NULL;
ALOGI("JNI_OnLoad");
if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("ERROR: GetEnv failed");
goto bail;
}
env = uenv.env;
if (registerNatives(env) != JNI_TRUE) {
ALOGE("ERROR: registerNatives failed");
goto bail;
}
result = JNI_VERSION_1_4;
bail:
return result;
}
Java :SimpleJNI/src/com/example/android/simplejni/SimpleJNI.java
package com.example.android.simplejni;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SimpleJNI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
int sum = Native.add(2, 3);
tv.setText("2 + 3 = " + Integer.toString(sum));
setContentView(tv);
}
}
class Native {
static {
// The runtime will add "lib" on the front and ".o" on the end of
// the name supplied to loadLibrary.
System.loadLibrary("simplejni");
}
static native int add(int a, int b);
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈