Table of Contents

Android JNI多线程相关

Android JNI相关

收集一些Android JNI的使用方法。

JNA

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.

Android JNI介绍

  1. Android JNI详述:参考http://hi.baidu.com/zhang_sg1984/blog/item/d0d7fcdd698916d58d1029af.html,讲得比较详细
  2. Android JNI 使用的数据结构JNINativeMethod详解:参考http://hi.baidu.com/caicry/blog/item/fd717a80cf98c7da9123d9da.html
  3. JNI 返回结构体参数:http://andilyliao.javaeye.com/blog/496816 很不错的讲解结构体参数传递的教程。
  4. NDK 应用及扩展介绍:http://wenku.baidu.com/view/361c9a2d2af90242a895e516.html,讲解了如何NDK中如何使用so文件。
  5. JNI tutorial,老外写的教程,http://www.giamungo.com/JNI/JNI.html
  6. JNI Examples for Android:http://android.wooyd.org/JNIExample/

NDK

  1. Porting curl to Android - A success story with NDK 1.6:http://curl.haxx.se/mail/lib-2009-12/0071.html

NDK Tips

#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

Linux 网络编程

  1. Linux网络编程一步一步学+基础:http://blog.chinaunix.net/u1/48325/showart_413841.html

NDK编译命令

安装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编译脚本For windows

简单写了一个脚本,调用ndk-build编译SO文件, 只支持两个参数

nkdbuild build
ndkbuild clean

Bat脚本for Windows

NDK编译脚本For Linux

1.支持编译静态库

2.解析Android.mk:支持指定编译后安装目录,支持编译前拷贝相关静态库到编译目录

NDK Shell For Linux

ndk-gdb调试

利用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

native crash log分析

native crash log分析