默认所有代码和文件在$project/jni下,否则特殊说明。
情况一:编译静态库
文件Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_STATIC_LIBRARY)
文件Application.mk: # 需要在application.mk 中加入模块名(模块列表,否则不能单独生产静态库?!)
APP_MODULES :=hello-jni
情况二:编译动态库
文件Android.mk: #动态库,只需要各自模块中声明模块名即可,不是必须在application中加入模块名(模块列表)才能生成动态库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
情况三:编译动态库+静态库
文件Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib_static
LOCAL_SRC_FILES := src.c
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib_shared
LOCAL_SRC_FILES := src2.c
LOCAL_STATIC_LIBRARIES := mylib_static
include $(BUILD_SHARED_LIBRARY)
情况四:已有第三方静态库(动态库),编译静态库(动态库)
文件Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := thirdlib1 # name it whatever
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libthird1.a # or $(so_path)/libthird1.so
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY) #or PREBUILT_SHARED_LIBRARY
include $(CLEAR_VARS)
LOCAL_MODULE := mylib_use_thirdlib
LOCAL_SRC_FILES := src.c
LOCAL_STATIC_LIBRARIES := thirdlib1 #or LOCAL_SHARED_LIBRARY
include $(BUILD_SHARED_LIBRARY) #如果编译静态库,需要Application.mk
使用第三方的动态库,静态库需要采用预编译( BUILD_SHARED_LIBRARY 或 PREBUILT_STATIC_LIBRARY)
和 LOCAL_STATIC_LIBRARIES LOCAL_SHARED_LIBRARY 来igz加入制订的动态库或者是静态库
而如果使用的系统库(静态库 / 动态库 ) 采用如下即可实现: LOCAL_LDLIBS := -lm -llog -ljnigraphics -lz
二、ndk-gdb 调试方法
http://www.cnblogs.com/yaozhongxiao/archive/2012/03/13/2393959.html
1。 AndroidManifest.xml <application>包含android:debuggable = "true"
2。 执行ndk-build NDK_DEBUG=1
3 。 在java代码调用c之前加一个断点,然后在gdb命令行加b ***.c:main
断点加入方法android.os.Debug.waitForDebugger();
4 。 执行ndk-gdb
ndk-gdb --start --force --verbose 显示更多信息,用于查看步骤中哪里出错。