開發應用一個關鍵的步驟是調試,對於 NDK 的 C 代碼調試有很多種方法,
修改一下 two-lib 的例子 ,使用 first.c 中的 first 函數實現一個加法計算器
http://wiki.jikexueyuan.com/project/android-ndk-development-tutorial/images/7.1.jpg" alt="picture7.1" />
這裡我們想在調用 first(int x,int y) 顯示出傳入的 x ,y 值。Android NDK 中提供了一個 Log 庫,其頭文件為 android/log.h ,可以提供 Androd Java 代碼中的 Log 功能,也是可以在 LogCat 中列印信息。
具體方法如下:
1.修改 first.c ,添加合適的列印語句
#include "first.h"
#include <android/log.h>
int first(int x, int y)
{
__android_log_print(ANDROID_LOG_INFO, "MYPROG", "x = %d, y =%d", x,y);
return x + y;
}
2.修改 android.mk 文件,添加需要鏈接的 Log 庫
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE:= libtwolib-first
LOCAL_SRC_FILES := first.c
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE:= libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_LDLIBS := -llog
LOCAL_STATIC_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)
然後就可以編譯 Native C 代碼,運行這個例子,可以在 LogCat 看到列印的信息:
http://wiki.jikexueyuan.com/project/android-ndk-development-tutorial/images/7.2.jpg" alt="picture7.2" />
本例下載