public class HelloWorldActivity extends Activity {
private native String getStringFromNative();
static {
System.loadLibrary("HelloWorldJniTest");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView jniContent = (TextView) findViewById(R.id.jni_content);
jniContent.setText(getStringFromNative());
}
}
Step: 2.执行Build->Make Project
Step: 3.javah生成c头文件
(注意:执行此步骤的前提是AndroidStudio已经安装了NDK,若尚未安装,在Step5中有安装说明。。)
javah -d jni -classpath D:\android_studio\sdk\platforms\android-23\android.jar;..\..\build\intermediates\classes\debug c
om.bupt.zhaoyanmei.myapplication.HelloWorldActivity
如图:
最后的生成结果:
Step: 4.编辑c文件
//
// Created by XXX on 2016/4/12.
//
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
#include
#ifndef LOG_TAG
#define LOG_TAG "ANDROID_LAB"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#endif
/* Header for class com_bupt_XXX_myapplication_HelloWorldActivity */
#ifndef _Included_com_bupt_XXX_myapplication_HelloWorldActivity
#define _Included_com_bupt_XXX_myapplication_HelloWorldActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_bupt_XXX_myapplication_HelloWorldActivity
* Method: getStringFromNative
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_bupt_XXX_myapplication_HelloWorldActivity_getStringFromNative
(JNIEnv * env, jobject jObj) {
LOGE("log string from ndk.");
return (*env)->NewStringUTF(env,"This is My First JNI project -- Hello World!");
}
#ifdef __cplusplus
}
#endif
#endif
ndk {
moduleName "HelloWorldJniTest"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
Step: 6.安装运行
查看Log打印:
到这里就结束啦~