java代码编写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
class
HelloWorldActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
TextView tv =
new
TextView(
this
);
tv.setText(helloWorldFromJNI());
setContentView(tv);
}
public
native
String helloWorldFromJNI();
// native声明,表示这个方法来自Native层。实现过程已经在native层实现了
static
{
System.loadLibrary(
"HelloWorld"
);
// 加载库,前面的lib和,后缀名不用写
}
}
|
Android.mk文件编写
1
2
3
4
5
6
7
8
|
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#LOCAL_MODULE表示生成的库的名字,前面的lib和后缀名不用写
LOCAL_MODULE := HelloWorld
LOCAL_SRC_FILES := HelloWorld.c
include $(BUILD_SHARED_LIBRARY)
|
c文件编写
1
2
3
4
5
6
7
8
9
|
#include <string.h>
#include <jni.h>
jstring
Java_com_conowen_helloworld_HelloWorldActivity_helloWorldFromJNI( JNIEnv* env,
jobject thiz )
{
return
(*env)->NewStringUTF(env,
"HelloWorld! I am from JNI !"
);
}
|
Cygwin编译
$ndk/ndk-build