1、安装cygwin(如果有别的linux系统如ubuntu也可以)
2、安装gcc、make,输入如下命令查看结果:
gcc -v
make -v
注:在安装cygwin的过程中有一步是select packages,选择devel下的gcc和make相关选项然后安装即可
3、安装ndk,我的为android_ndk_r6,它支持stl
4、修改cygwin.bat文件,到cygwin的目录下找到cygwin.bat文件,加入NDKROOT变量,并修改PATH变量,比如我的示例内容如下:
@echo off d: chdir d:\cygwin\\bin set HOME=d:\cygwin\home set PATH = $PATH:/cygdrive/d/android/android_ndk_r6 set NDKROOT=/cygdrive/d/android/android_ndk_r6 bash --login -i
5、建立mk文件及.c、.h文件:
在Eclipse的项目下新建目录jni,并新增Android.mk文件,内容如下:
# 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 := jniDrawCodeLOCAL_SRC_FILES := jniDrawCode.cLOCAL_CFLAGS := -std=c99 -ggdbAPP_STL := stlport_static
include $(BUILD_SHARED_LIBRARY)
mk文件的其他选项可参考网上及sdk的帮助资料。
新增.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 <string.h> #include <jni.h> /* 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_org_xxx_pg_Caller_drawCodeNum(JNIEnv* env, jobject thiz,float* bufferPCM,int bufferLen,long anacount , float bw ) { return (*env)->NewStringUTF(env, “xxx"); }
其中org_xxx_pg为package名称,Caller为调用该函数的java类名
6、进入cygwin的命令行模式,切换到项目的jni目录下,使用ndk-build编译:
$ $NDKROOT/ndk-build NDK_DEBUG=1
7、java调用:
首先加载库文件:
static { System.loadLibrary("jniDrawCode") ; }
声明及调用:
package org_xxx_pg; public class Caller extends xxxx { .... private native String drawCodeNum(float[]bufferPCM,int bufferLen,long anacount , float bw); ..... }
这样就和其他函数一样可以调用了
调试:
跟所有 Unix like 的环境一样,也可以用使用 gdb 来调试。Android NDK 提供了一个命令 ndk-gdb 便于 gdb 的使用。ndk-gdb 是一个 Shell 脚本,它会做一些环境的检查适配,帮我们在目标机上(Android 手机或者模拟器)启动 gdbserver,然后在 PC 上启动 gdb 并连接到 gdbserver,跟通用的 gdb 没有差别。
为了使用 ndk-gdb,有几个地方需要注意:
- 需要在 AndroidManifest.xml 里面给 application 加上属性 android:debuggable="true",充许调式
- JNI 的 Android.mk 编译选项要加调试信息 LOCAL_CFLAGS := -ggdb
- ndk-build 命令加参数 NDK_DEBUG=1
准备好 apk 后,启动 gdb
$ /cygdrive/c/Android/android-ndk-r6b/ndk-gdb --adb=/cygdrive/c/Android/android-sdk/platform-tools/adb.exe -s XXX --force --start
如果出现“ Could not extract package's data directory”提示,这是因为run-as没有root权限,修改ndk-gdb脚本:
查找" DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd"这一行,改为:
DATA_DIR="/data/data/$PACKAGE_NAME"
查找:run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &改为:
run $ADB_CMD shell "(cd $DATA_DIR; lib/gdbserver +$DEBUG_SOCKET --attach $PID)" &然后就和普通的gdb一样可以调试了。