1. 安装DNK和cywing 测试成功博客里面有
2.编写程序 Eclipse NDK 程序在D\aws\nhk2目录下
A. 建立Android框架程序
包名:hbut.jdl.qrs 类名 Huihui
Java代码如下:
package hbut.jdl.qrs;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Huihui extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.huihui);
TextView tView = (TextView)findViewById(R.id.textView1);
tView.setText(StringFromJni());
}
public native String StringFromJni();
static {
System.loadLibrary("Native");
}
}
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:text="TextView" />
</RelativeLayout>
我的IDE是Eclipse选择了自动编译 于是在工程目录生成 在bin\classes目录下生成了.class文件
B 这一步用javah来编译C头文件
在bin\classes目录下生成头文件
C.在于src同级目录下新建一个folder,取名jni 加入两个文件 取名Android.mk Native.c
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Native
LOCAL_SRC_FILES := Native.c
include $(BUILD_SHARED_LIBRARY)
Native.c 注意头文件
#include "hbut_jdl_qrs_Huihui.h"
#include <string.h>
#include <jni.h>
JNIEXPORT jstring JNICALL Java_hbut_jdl_qrs_Huihui_StringFromJni
(JNIEnv * env, jobject this){
return (*env)->NewStringUTF(env, "huihui234");
}
E.用cygwin工具 进行ndk-build
然后在真机上运行 HUAWEI T8950 Android4.0.4
结果如下
后记:System.loadLibrary(“A”) A的名字和Android.mk文件里面的LOCAL_MODULE名字一样就可以了