首页 > 代码库 > Android NDK调试方式之一: adb logcat

Android NDK调试方式之一: adb logcat

查看程序执行过程中所打印的log信息,用于辅助调试排除代码错误。

一、采用NDK安装包下Samples/hello-jni工程做实验

1)修改jni/hello-jni.c文件

#include <android/log.h>__android_log_print(ANDROID_LOG_INFO, "HelloJniTag", "The value is %d", 1000);

2)修改jni/Android.mk文件

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hello-jniLOCAL_SRC_FILES := hello-jni.cLOCAL_LDLIBS    := -lloginclude $(BUILD_SHARED_LIBRARY)

3)使用eclipse打开当前项目,运行测试,Debug As...

4)在eclipse的logcat监控窗口观察日志信息

 

二、NDK开发的一般步骤

1)启动eclipse创建android application工程;

2)创建activity应用类(可仿照HelloJni中HelloJni.java);

3)在activity应用类中加入native函数声明及调用;

     public native String  stringFromJNI();    /* This is another native method declaration that is *not*     * implemented by ‘hello-jni‘. This is simply to show that     * you can declare as many native methods in your Java code     * as you want, their implementation is searched in the     * currently loaded native libraries only the first time     * you call them.     *     * Trying to call this function will result in a     * java.lang.UnsatisfiedLinkError exception !     */    public native String  unimplementedStringFromJNI();    /* this is used to load the ‘hello-jni‘ library on application     * startup. The library has already been unpacked into     * /data/data/com.example.hellojni/lib/libhello-jni.so at     * installation time by the package manager.     */    static {        System.loadLibrary("HelloJni1");    }

4)在命令行模式下使用javah生成jni头文件,并创建c或c++实现文件;

javah -jni -d jni -cp bin/classes:Android-20.jar  com.example.hellojni.HelloJni

5)在jni目录下创建Android.mk、Application.mk编辑脚本

#Android.mkLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hello-jniLOCAL_SRC_FILES := hello-jni.cLOCAL_LDLIBS    := -lloginclude $(BUILD_SHARED_LIBRARY)
#Application.mkAPP_ABI := all

6)修改AndroidManifest.xml文件,仿照HelloJni添加工程启动的Activity配置项。

7)eclipse启动Debug As...进行调试。

  

 

Android NDK调试方式之一: adb logcat