Android NDK:如何将Android.mk包含到另一个Android.mk(分层项目结构)中?

看起来有可能,但我的脚本会产生奇怪的结果:

LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) include $(LOCAL_PATH)/libos/Android.mk include $(LOCAL_PATH)/libbase/Android.mk include $(LOCAL_PATH)/utils/Android.mk LOCAL_MODULE := native include $(BUILD_SHARED_LIBRARY) 

只有第一个包被解析正常,其他Android.mk文件正在奇数路径上被查询。 建议?

更新:我打破了我的建筑环境……在办公室没问题,但在家里LOCAL_PATH:= $(调用my-dir)将LOCAL_PATH定义为NDK目录而不是项目目录。 这是我的建筑批次:

 set BASHPATH=K:\cygwin\bin\bash set PROJECTDIR=/cygdrive/h/Alex/Alex/Work/Android/remote-android set NDKDIR=/cygdrive/h/Alex/Programming_Docs/Android/android-ndk-r6/ndk-build set APP_BUILD_SCRIPT=/cygdrive/h/Alex/Alex/Work/Android/project/jni/Android.mk set DEV_ROOT=h:/Alex/Alex/Work/Android/project %BASHPATH% --login -c "cd %PROJECTDIR% && %NDKDIR%" 

更新:我完全不明白这件事是如何构成路径的。 我收到路径错误,如“/cygdrive/d/project/jni//cygdrive/d/Soft/project/jni/libos/src/libos.cpp”。这是在我决定指定根目录中的所有文件之后Android.mk而不是包含子模块。

更新2:没有运气,这也不起作用:

 LOCAL_PATH:= $(call my-dir) # Include makefiles here. include $(LOCAL_PATH)/libos/Android.mk include $(LOCAL_PATH)/libbase/Android.mk include $(LOCAL_PATH)/utils/Android.mk # Clear variables here. include $(CLEAR_VARS) 

Solutions Collecting From Web of "Android NDK:如何将Android.mk包含到另一个Android.mk(分层项目结构)中?"

在这里很晚,但是如果有人读到这个问题,一种方法来解决破坏路径的问题(指向你的文件从jni中输入的ndk)是在你的jni文件夹中:

 include $(call all-subdir-makefiles) 

然后在它的每个子文件夹中(在OP的情况下为libos,libbase和ustils)这个forms的Android.mk:

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_MODULE := utils LOCAL_SRC_FILES := one.c LOCAL_SRC_FILES += two.c 

第二个Android.mk在jni文件夹中find子文件夹中的one.c和two.c文件。

请注意尝试的东西

 LOCAL_PATH_BIS_WEIRD_OTHER_NAME := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES := $(LOCAL_PATH_OTHER_FUNKY_NAME) LOCAL_MODULE := utils LOCAL_SRC_FILES := one.c LOCAL_SRC_FILES += two.c 

将再次导致一个混乱的编译器寻找ndk所在的源代码。

因此,在jni的每个子目录中以这种forms使用LOCAL_PATH:= $(调用my-dir) ,并在jni本身中包含$(call all-subdir-makefiles) ,你应该没有问题。

希望这会对某人有所帮助。

编辑:发生此行为是因为包含$(CLEAR_VARS)不会删除LOCAL_PATH中保留的内容。

我就是这样做的。

需要注意的是,我没有在构建目录中放置任何C ++源代码,因为大部分内容都与平台无关。 这只是意味着LOCAL_PATH不是项目目录的子目录,而/ jni中的唯一文件是2 .mk文件。

无论如何,这里是一个完整的顶级Android.mk和一个真实项目的包含:

顶层:

 LOCAL_PATH := $(abspath $(call my-dir)/../../../src) # utility to create paths for included makefiles local-rel-path = $(patsubst /%,%,$(subst $(LOCAL_PATH),,$(abspath $1))) include $(CLEAR_VARS) LOCAL_MODULE := NativeApp LOCAL_LDLIBS := -lGLESv1_CM # As opposed to "thumb" LOCAL_ARM_MODE := arm LOCAL_SRC_FILES := # # includes # # Note that LOCAL_C_INCLUDE is relative to thr NDK root, unlike source paths # (or you can just make 'em absolute) # STL_INC_DIR = /cygdrive/c/STLport-5.2.1/stlport MY_LOCAL_C_INCLUDES := core satcalc bruce/bruce/inc bruce/gfx/inc bruce/ui/inc bruce/unzip bruce/libpng LOCAL_C_INCLUDES := $(addprefix $(LOCAL_PATH)/,$(MY_LOCAL_C_INCLUDES)) $(STL_INC_DIR) ifeq ($(APP_OPTIM),debug) # debug LOCAL_CFLAGS = -DPLATFORM_ANDROID -D_DEBUG -fvisibility=hidden else #release LOCAL_CFLAGS = -DPLATFORM_ANDROID -fvisibility=hidden endif LOCAL_STATIC_LIBRARIES := # # Code # include $(LOCAL_PATH)/core/Android.mk include $(LOCAL_PATH)/satcalc/Android.mk include $(LOCAL_PATH)/bruce/bruce/src/Android.mk include $(LOCAL_PATH)/bruce/gfx/src/Android.mk include $(LOCAL_PATH)/bruce/ui/src/Android.mk include $(LOCAL_PATH)/bruce/unzip/Android.mk include $(LOCAL_PATH)/bruce/libpng/Android.mk # # Build it # include $(BUILD_SHARED_LIBRARY) 

…和一个包含Android.mk:

 MY_PATH = $(call my-dir) MY_LOCAL = $(call local-rel-path, $(MY_PATH)) MY_SRC_FILES = Font.cpp Gfx2d_ogles.cpp SgaState.cpp \ Sprite.cpp TImage.cpp TImageOgles.cpp LOCAL_SRC_FILES += $(addprefix $(MY_LOCAL)/,$(MY_SRC_FILES)) 

我的认可是这样的:

 LOCAL_PATH:= $(call my-dir) # Clear variables here. include $(CLEAR_VARS) # Current module settings. LOCAL_MODULE := native # setup some source files LOCAL_SRC_FILES := file1.c file2.c # setup some includes LOCAL_C_INCLUDES := $(LOCAL_PATH)/libos/include # setup the included libs for the main module LOCAL_STATIC_LIBRARIES := libos libbase utils # note that order matters here include $(BUILD_SHARED_LIBRARY) # Include makefiles here. Its important that these # includes are done after the main module, explanation below. # create a temp variable with the current path, because it # changes after each include ZPATH := $(LOCAL_PATH) include $(ZPATH)/libos/Android.mk include $(ZPATH)/libbase/Android.mk include $(ZPATH)/utils/Android.mk 

注意,包括在设置当前模块variables之后完成。 这是必需的,因为每个include修改了LOCAL_PATHvariables(实际上它修改了$(调用my-dir)返回的内容),这就是为什么包含必须在最后完成。

这将自动编译所有包含的模块(如果使用clean调用则清理),然后链接所有包含的库。

此设置已在实际项目中进行测试并正常运行。

从这里回答: https : //docs.google.com/document/d/1jDmWgVgorTY_njX68juH5vt0KY_FXWgxkxmi2v_W_a4/edit

你走在正确的轨道上。 这是将Android.mk文件包含在另一个文件中的正确方法 – 它实际上是Android make系统所必需的。 需要注意的一点是,清除variables的行应该出现在 – 包括其他makefile之后 – 如下所示:

 LOCAL_PATH:= $(call my-dir) # Include makefiles here. include $(LOCAL_PATH)/libos/Android.mk include $(LOCAL_PATH)/libbase/Android.mk include $(LOCAL_PATH)/utils/Android.mk # Clear variables here. include $(CLEAR_VARS) # Final settings. LOCAL_MODULE := native include $(BUILD_SHARED_LIBRARY) 

我还要提到你可能想要或不想设置的其他重要标志,包括以下内容(我的一个makefile中的示例):

 # Settings. LOCAL_C_INCLUDES := $(MY_INCLUDES) LOCAL_STATIC_LIBRARIES := $(MY_MODULES) LOCAL_WHOLE_STATIC_LIBRARIES := $(MY_WHOLE_MODULES) LOCAL_LDLIBS := -lz -llog -lGLESv1_CM -lGLESv2 LOCAL_ARM_MODE := arm LOCAL_MODULE := game 

最后,我发现Android ndk中嵌入的文档特别有用。 矿井位于以下位置:

 android-ndk-r6/documentation.html 

如果您还有其他问题,请与我们联系。 希望这可以帮助! 🙂

这里的答案很晚,但我遇到了这个问题,这些解决方案都没有用。 解决方案结果很简单:如此处详述,设置MY_LOCAL_PATHvariables并每次重新分配LOCAL_PATH

 MY_LOCAL_PATH := $(call my-dir) LOCAL_PATH := $(MY_LOCAL_PATH) ... declare one module include $(LOCAL_PATH)/foo/Android.mk LOCAL_PATH := $(MY_LOCAL_PATH) ... declare another module 

我测试下面的代码确定。

 # I want only second-level mk files, that is the direct sub-directories # in the current path. include $(wildcard */*/Android.mk) # include $(call all-subdir-makefiles) ## $(wildcard $(call my-dir)/*/Android.mk) # include $(call all-makefiles-under,$(LOCAL_PATH)) 

Android.mk

 # I dunno why it's an empty result for $(call all-subdir-makefiles). # $(info [^-^ print-test] all-subdir-makefiles = "$(call all-subdir-makefiles) ") $(info [print-test] assert "jni/Android.mk" = "$(wildcard */Android.mk)") # print: jni/Android.mk $(info [print-test] $$(wildcard */*/Android.mk) = "$(wildcard */*/Android.mk)") # print: jni/xxdir/Android.mk 

我打印结果:

 $ cd your_project_path $ ndk-build [print-test] assert "jni/Android.mk" = "jni/Android.mk" [print-test] (wildcard */*/Android.mk) = "jni/HelloWorld/Android.mk jni/MessagePack/Android.mk"