MainActivity.java
package com.example.jniload; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); //setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } native String stringFromJNI(); static{ System.loadLibrary("jniload"); } }test-jni.c
#include <jni.h> #include <stdio.h> #include <string.h> #include <assert.h> # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) #define JNIREG_CLASS "com/example/test/Test" JNIEXPORT jstring JNICALL stringFromJNI(JNIEnv *env, jclass clazzi) { return (*env)->NewStringUTF(env, "hello Android3."); } static JNINativeMethod method_table[] = { { "stringFromJNI", "()Ljava/lang/String;", (void*)stringFromJNI },//绑定 }; 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; } int register_ndk_load(JNIEnv *env) { return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table)); } JNIEXPORT jint 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 result; } register_ndk_load(env); return JNI_VERSION_1_4; }jni-load.cpp
#include <jni.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) #define JNIREG_CLASS "com/example/jniload/MainActivity" extern "C" /*jstring Java_com_example_jniload_MainActivity_stringFromJNI( JNIEnv* env,jobject thiz ) { char* str="Hello from JNI."; return (env)->NewStringUTF(str); }*/ JNIEXPORT jstring JNICALL stringFromJNI( JNIEnv* env,jobject thiz ) { char* str="Hello from JNI."; return (env)->NewStringUTF(str); } static JNINativeMethod method_table[] = { { "stringFromJNI", "()Ljava/lang/String;", (void*)stringFromJNI }, }; static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods) { jclass clazz; clazz = env->FindClass(className); if (clazz == NULL) { return JNI_FALSE; } if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { return JNI_FALSE; } return JNI_TRUE; } int register_ndk_load(JNIEnv *env) { return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table)); } jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { return result; } //result=register_ndk_load(env); if ( register_ndk_load(env) < 0){ goto bail; } result = JNI_VERSION_1_4; bail: return result; }