public native String stringFromJNI();
public native String unimplementedStringFromJNI();
可以看到这两个方法的声明中有 native 关键字, 这个关键字表示这两个方法是本地方法,也就是说这两个方法是通过本地代码(C/C++)实现的,在java代码中仅仅是声明。
eclipse自动编译该工程,生成相应的.class文件,这步必须在下一步之前完成,因为生成.h文件需要用到相应的.class文件。
Step2编写相应的C/C++代码
利用javah这个工具生成相应的.h文件,然后根据这个.h文件编写相应的C/C++代码。
2.1 生成相应.h文件:
2.1.1首先我们在工程目录下建立一个jni文件夹:
braincol@ubuntu:~/workspace/android/NDK/hello-jni$ mkdir jni
braincol@ubuntu:~/workspace/android/NDK/hello-jni$ ls
AndroidManifest.xml assets bin default.properties gen jni res src
2.2.2以生成相应的.h文件:
项目路径$ javah -classpath bin/classes -d jni com.example.hellojni.HelloJni
-classpath bin/classes:表示类的路径
-d jni: 表示生成的头文件存放的目录
com.example.hellojni.HelloJni 则是完整类名
这一步的成功要建立在已经在 bin/com/example/hellojni/ 目录下生成了 HelloJni.class的基础之上。
现在可以看到jni目录下多了个.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_example_test_MainActivity */
#ifndef _Included_com_example_test_MainActivity
#define _Included_com_example_test_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
2.2 编写相应的.c文件
hello-jni.c :
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include
#include
/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
*/
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
2.3. 编译c文件生成相应的库
2.3.1 编写Android.mk文件
在jni目录下(即hello-jni.c 同级目录下)新建一个Android.mk文件,Android.mk 文件是Android 的 makefile文件,内容如下:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
除去模块名(hello-jni)外,文件中其它东西都不用关心 对NDK的build过程来说,Android.mk至关重要,它用来区分NDK模块。在我们这个例子中,模块的名字是hello-jni,它告诉build工具它包含了一个源文件hello-jni.c我们到jni文件夹里创建hello-jni
2.3.2 生成.so共享库文件
运行:$NDK/ndk-build。
注意:该命令$NDK和/ndk-build是连接到一起的,中间没有空格,实际使用时输入完整命令$NDK/ndk-build,不能只输入ndk-build。$NDK/ndk-build成功运行后,将会在项目libs/armeabi/ 目录下创建一个.so文件。这个.so文件就是二进制库,它将被包含到应用的.apk包中,并可以被Java代码链接。在Eclipse中,你只需要在选中项目根节点后,按F5键,就可以将在Cygwin控制台中所做的更改,更新到Eclipse项目中。