1.下载Ndk 并且配置
2.新建Android工程 HelloNdk
3.在MainActivity 同目录下新建Ndktest类并且编写本地方法
package com.example.lenovo.hellondk;
public class Ndktest {
public static native String getChar();
}
4.点击AS Terminal 切换到main目录调用javah 生成头文件
以我的为例:
执行成功后会在main目录下生成jni文件夹 并且生成了响应的 头文件
5.根据头文件方法编写.c文件
#include "com_example_lenovo_hellondk_Ndktest.h"
//
// Created by lenovo on 2016/4/13.
//
JNIEXPORT jstring JNICALL Java_com_example_lenovo_hellondk_Ndktest_getChar(JNIEnv * env,jobject jobject)
{
return (*env)->NewStringUTF(env,"HelleNdk");
}
6.gradle.properties中添加
android.useDeprecatedNdk=true
(可以省略)
在app目录下build.gradle.添加.so名字
7.在MainActivity中获取文本运行
package com.example.lenovo.hellondk;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
static{
System.loadLibrary("MyJni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView= (TextView) findViewById(R.id.id);
textView.setText(Ndktest.getChar());
}
}
8.结果: