现在我正在开发一个原生 Android 项目,我想使用 DevIL。我不想将所有 NDK 代码放在一个文件中,因为 DevIL 需要多个其他库(libpng/libjpeg/...),而 Android.mk 最终会变得臃肿。
我在我的 jni 目录中创建了一个名为 Devil 的子目录。我将 DevIl 所需的所有代码(+ 库)放在此目录中。我还在这个目录中创建了一个额外的 Android.mk 文件。在我的“主”Android.mk 文件中,我添加了 $(call import-module,DevIL) 以及:LOCAL_SHARED_LIBRARIES += libdevil。在 DevIL Android.mk 中,我创建了一个 libdevil 模块,并使用 LOCAL_EXPORT_C_INCLUDES 链接到 DevIL 头文件。当我将 #include 放入我的 main.cpp 文件时,ndk-build 失败并就包含的标头抱怨“没有这样的文件或目录”。如果我不将#include 放入代码中,则代码编译得很好,并且所有共享库都会被创建并放入 libs 文件夹中。我究竟做错了什么?
我的主要 Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include cflags.mk
LOCAL_MODULE := arm
LOCAL_SRC_FILES := main.cpp
LOCAL_SHARED_LIBRARIES += libdevil
include $(BUILD_SHARED_LIBRARY)
$(call import-module,DevIL)
用于 DevIl 的 Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include cflags.mk
JPEG_SRC_PATH := Libjpeg/
DEVIL_SRC_PATH := DevIL/
#many more
#libjpeg
include $(CLEAR_VARS)
LOCAL_MODULE := libjpeg
LOCAL_C_INCLUDES := ${JPEG_SRC_PATH}
FILE_LIST := $(wildcard $(LOCAL_PATH)/JPEG_SRC_PATH/*.c)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
include $(BUILD_SHARED_LIBRARY)
#many other libraries; basically the same as libjpeg
#libdevil
include $(CLEAR_VARS)
LOCAL_MODULE := libdevil
LOCAL_EXPORT_C_INCLUDES := \
${DEVIL_SRC_PATH}include \
${DEVIL_SRC_PATH}src-IL/include \
#many more
FILE_LIST := $(wildcard $(LOCAL_PATH)/DEVIL_SRC_PATHsrc-IL/src/*.c)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_STATIC_LIBRARIES := \
libjpeg \
#many more
LOCAL_LDLIBS += -lz
include $(BUILD_SHARED_LIBRARY)
cflags.mk 文件
LOCAL_CFLAGS := -DANDROID_NDK
LOCAL_CFLAGS += -Werror
LOCAL_CFLAGS += -Wall
LOCAL_CFLAGS += -Wextra
LOCAL_CFLAGS += -Wno-strict-aliasing
LOCAL_CFLAGS += -Wno-unused-parameter
LOCAL_CFLAGS += -Wno-missing-field-initializers
LOCAL_CFLAGS += -Wno-multichar
LOCAL_CPPFLAGS := -Wno-type-limits
LOCAL_CPPFLAGS += -Wno-invalid-offsetof
LOCAL_CPPFLAGS += -std=c++11
LOCAL_CFLAGS += -std=c++11
LOCAL_CFLAGS += -fexceptions
LOCAL_CFLAGS += -Wno-error=deprecated-declarations
LOCAL_ARM_MODE := arm
目录结构
jni
|
+-- main.cpp
+-- Android.mk <- main Android
+-- Application.mk
+-- cflags.mk
|
+--DevIL
|
+-- Android.mk <- the Android.mk for DevIL
+-- cflags.mk
+--DevIL <-Directory containing DevIL source code
+--Libjpeg <-Directory containing code of specific DevIL dependency
+--...<-Directory containing code of specific DevIL dependency
编辑:
我注意到我没有添加我的 Application.mk,因此我的 Application.mk
应用程序.mk
APP_PLATFORM := android-19
APP_ABI := armeabi-v7a
APP_STL:=c++_static
APP_LDFLAGS := -Wl,--build-id
NDK_TOOLCHAIN_VERSION := 4.9
ROOT_DIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
NDK_MODULE_PATH := $(ROOT_DIR)