收集一些Android JNI的使用方法。
JNA provides Java programs easy access to native shared libraries without writing anything but Java code - no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. JNA allows you to call directly into native functions using natural Java method invocation. The Java call looks just like the call does in native code. Most calls require no special handling or configuration; no boilerplate or generated code is required.
#APP_BUILD_SCRIPT := $(APP_PROJECT_PATH)/jni/Android.mk
APP_BUILD_SCRIPT := $(APP_PROJECT_PATH)/Android.mk * undefined reference to `wcstombs'
默认的NDK,在用到wcstombs函数时(已经include <stdlib.h>),编译会报错,在网上找了一下,是NDK的问题:http://blog.sina.com.cn/s/blog_4b650d650100m07j.html 可以在这里下载打过patch的版本,http://www.crystax.net/android/ndk-r4.php
安装cygwin后,把cygwin/bin路径添加到PATH路径,执行下面的命令可以编译SO文件。这样就不需要每次都进入cygwin环境下再执行ndk-build。
再简单一点将这行命令写成一个bat文件,这样只需要在cmd下面直接运行这个bat文件就行了。
bash D:\android\ndk\android-ndk-r4-windows\android-ndk-r4-windows\android-ndk-r4\ndk-build
利用ndk-gdb可以直接调试native code.
命令:
bash /cygdrive/d/android/ndk/android-ndk-r4-windows/android-ndk-r4/ndk-gdb --start --force --verbose
但在cygwin下调试时遇到了一个问题,
D:\workspace\DDTui>bash /cygdrive/d/android/ndk/android-ndk-r4-windows/android-ndk-r4/ndk-gdb GNU gdb 6.6 Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "--host=i686-pc-cygwin --target=arm-elf-linux". /cygdrive/d/workspace/DDTui/bin/ndk/local/armeabi/app_process: No such file or directory.找不到app_process文件,Google了一下, 找到类似的问题:http://code.google.com/p/android/issues/detail?id=8894
方法是修改ndk-gdb:
# Get the app_server binary from the device APP_PROCESS=$APP_OUT/app_process #run adb pull /system/bin/app_process $APP_PROCESS #Add by Jianqing Peng,20101122 if [ -s "${COMSPEC}" ] ; then run adb pull /system/bin/app_process "`cygpath -w $APP_PROCESS`" else run adb pull /system/bin/app_process $APP_PROCESS fi