标签:没有 而且 jnienv har ati 好的 method java api main
首先须要新建一个android项目。然后在主函数那里声明一个native方法,代码例如以下
public class MainActivity extends Activity { public static native String test(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
javah -d ../jni 包名.MainActivity这时候刷新项目就会发现多了一个jni目录,里面有个.h的文件,打开就是一个c头文件
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_exercise_MainActivity */ #ifndef _Included_com_example_exercise_MainActivity #define _Included_com_example_exercise_MainActivity #ifdef __cplusplus extern "C" { #endif #undef com_example_exercise_MainActivity_MODE_PRIVATE #define com_example_exercise_MainActivity_MODE_PRIVATE 0L #undef com_example_exercise_MainActivity_MODE_WORLD_READABLE #define com_example_exercise_MainActivity_MODE_WORLD_READABLE 1L #undef com_example_exercise_MainActivity_MODE_WORLD_WRITEABLE #define com_example_exercise_MainActivity_MODE_WORLD_WRITEABLE 2L #undef com_example_exercise_MainActivity_MODE_APPEND #define com_example_exercise_MainActivity_MODE_APPEND 32768L #undef com_example_exercise_MainActivity_MODE_MULTI_PROCESS #define com_example_exercise_MainActivity_MODE_MULTI_PROCESS 4L #undef com_example_exercise_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING #define com_example_exercise_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L #undef com_example_exercise_MainActivity_BIND_AUTO_CREATE #define com_example_exercise_MainActivity_BIND_AUTO_CREATE 1L #undef com_example_exercise_MainActivity_BIND_DEBUG_UNBIND #define com_example_exercise_MainActivity_BIND_DEBUG_UNBIND 2L #undef com_example_exercise_MainActivity_BIND_NOT_FOREGROUND #define com_example_exercise_MainActivity_BIND_NOT_FOREGROUND 4L #undef com_example_exercise_MainActivity_BIND_ABOVE_CLIENT #define com_example_exercise_MainActivity_BIND_ABOVE_CLIENT 8L #undef com_example_exercise_MainActivity_BIND_ALLOW_OOM_MANAGEMENT #define com_example_exercise_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L #undef com_example_exercise_MainActivity_BIND_WAIVE_PRIORITY #define com_example_exercise_MainActivity_BIND_WAIVE_PRIORITY 32L #undef com_example_exercise_MainActivity_BIND_IMPORTANT #define com_example_exercise_MainActivity_BIND_IMPORTANT 64L #undef com_example_exercise_MainActivity_BIND_ADJUST_WITH_ACTIVITY #define com_example_exercise_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L #undef com_example_exercise_MainActivity_CONTEXT_INCLUDE_CODE #define com_example_exercise_MainActivity_CONTEXT_INCLUDE_CODE 1L #undef com_example_exercise_MainActivity_CONTEXT_IGNORE_SECURITY #define com_example_exercise_MainActivity_CONTEXT_IGNORE_SECURITY 2L #undef com_example_exercise_MainActivity_CONTEXT_RESTRICTED #define com_example_exercise_MainActivity_CONTEXT_RESTRICTED 4L #undef com_example_exercise_MainActivity_RESULT_CANCELED #define com_example_exercise_MainActivity_RESULT_CANCELED 0L #undef com_example_exercise_MainActivity_RESULT_OK #define com_example_exercise_MainActivity_RESULT_OK -1L #undef com_example_exercise_MainActivity_RESULT_FIRST_USER #define com_example_exercise_MainActivity_RESULT_FIRST_USER 1L #undef com_example_exercise_MainActivity_DEFAULT_KEYS_DISABLE #define com_example_exercise_MainActivity_DEFAULT_KEYS_DISABLE 0L #undef com_example_exercise_MainActivity_DEFAULT_KEYS_DIALER #define com_example_exercise_MainActivity_DEFAULT_KEYS_DIALER 1L #undef com_example_exercise_MainActivity_DEFAULT_KEYS_SHORTCUT #define com_example_exercise_MainActivity_DEFAULT_KEYS_SHORTCUT 2L #undef com_example_exercise_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL #define com_example_exercise_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L #undef com_example_exercise_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL #define com_example_exercise_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L /* * Class: com_example_exercise_MainActivity * Method: test * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_example_exercise_MainActivity_test (JNIEnv *, jclass); /* * Class: com_example_exercise_MainActivity * Method: updateFile * Signature: (Ljava/lang/String;)V */ <pre name="code" class="java">JNICALL Java_com_example_exercise_MainActivity_updateFile (JNIEnv *, jclass, jstring)#ifdef __cplusplus}#endif#endif
JNICALL Java_com_example_exercise_MainActivity_updateFile (JNIEnv *, jclass, jstring)这就是依据我们一開始在mainactivity定义的那个native方法生成的一个方法。
有了头文件,我们就能够開始写.c文件了。即实现文件,新建一个文件main.c。然后输入代码例如以下
#include<stdio.h> #include<stdlib.h> JNIEXPORT jstring JNICALL Java_com_example_exercise_MainActivity_test (JNIEnv * env, jobject obj){ return (*env)->NewStringUTF(env, "Hello world !"); }
LOCAL_PATH :=$(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := main LOCAL_SRC_FILES := main.c include $(BUILD_SHARED_LIBRARY)
public class MainActivity extends Activity { public static native String test(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView t = (TextView)findViewById(R.id.jnitextview); t.setText(test()); } static { System.loadLibrary("main"); } }
static { System.loadLibrary("main"); }main是我们在android.mk中配置的一个名字,如今万事俱备,仅仅差编译生成so文件了。我们打开cmd而且切换到项目的文件夹下,执行ndk-build。中间是减号,不是下划线,刷新项目就能够看到libs中多了个文件夹和里面的一个libmain.so文件,这时候就能够执行项目了!
假设没有意外就会出现helloworld在手机频幕上。
LOCAL_PATH :=$(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := main LOCAL_SRC_FILES := main.c LOCAL_LDLIBS += -llog include $(BUILD_SHARED_LIBRARY)
标签:没有 而且 jnienv har ati 好的 method java api main
原文地址:http://www.cnblogs.com/yfceshi/p/6893463.html