Android NDK: how to include into another (hierarchical project structure)?-ThrowExceptions

Exception or error:

Looks like it’s possible, but my script produces odd results:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

include $(LOCAL_PATH)/libos/
include $(LOCAL_PATH)/libbase/
include $(LOCAL_PATH)/utils/

LOCAL_MODULE := native

Only the first include is being parsed fine, other files are being seacrhed at odd paths.

Update: I have broken my building environment… It was OK in the office, but at home LOCAL_PATH:= $(call my-dir) defines LOCAL_PATH to NDK dir instead of project dir. This is my batch for building:

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/
set DEV_ROOT=h:/Alex/Alex/Work/Android/project

%BASHPATH% --login -c "cd %PROJECTDIR% && %NDKDIR%"

Update: I absolutely don’t understand how does this thing compose paths. I’m getting errors with paths like “/cygdrive/d/project/jni//cygdrive/d/Soft/project/jni/libos/src/libos.cpp’. This is after I decided to specify all files in the root instead of including submodules.

Update 2: No luck, this doesn’t work either:

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

# Clear variables here.
 include $(CLEAR_VARS)
How to solve:

Pretty late here, but in case somebody reads this question, one way to get past the problem of broken paths(pointing to the ndk insted of your files from the jni) is to have in your jni folder:

include $(call all-subdir-makefiles)

and then in every subfolder of it (libos, libbase and ustils inthe case of OP) an of this form:

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

where this second in with the one.c and two.c files in a subfolder found in the jni folder.

Note that trying something as

include $(CLEAR_VARS)
LOCAL_MODULE            := utils
LOCAL_SRC_FILES         := one.c
LOCAL_SRC_FILES         += two.c

will lead again to a confused compiler looking for your source code where the ndk is.

So, use the LOCAL_PATH := $(call my-dir) EXACTLY in this form in every subdirectory of the jni and include $(call all-subdir-makefiles) in the jni itself and you shouldn’t have problems.

Hope this will help someone.

Edit: this behaviour happens because what is kept in LOCAL_PATH is not deleted by include $(CLEAR_VARS).


Here’s how I do it.

One caveat is that I don’t put any of my C++ source code in the build dir, since most of it is platform-independent. This just means that LOCAL_PATH is not a subdir of the project dir, and the only files in /jni are the 2 .mk files.

Anyway, here’s a full top-level and one of the included ones for a real project:

Top level:

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)



# As opposed to "thumb"


# 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


ifeq ($(APP_OPTIM),debug)
# debug


# Code
include $(LOCAL_PATH)/core/
include $(LOCAL_PATH)/satcalc/
include $(LOCAL_PATH)/bruce/bruce/src/
include $(LOCAL_PATH)/bruce/gfx/src/
include $(LOCAL_PATH)/bruce/ui/src/
include $(LOCAL_PATH)/bruce/unzip/
include $(LOCAL_PATH)/bruce/libpng/

# Build it

…and an included

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))


My approah is like this:

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 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

include $(ZPATH)/libos/
include $(ZPATH)/libbase/
include $(ZPATH)/utils/

Note, that includes are done after the setup of current module variables. This is needed because each include modifies the LOCAL_PATH variable (actually it modifes what $(call my-dir) returns) and thats why includes must be done last.

This will automatically compile all included modules (or clean then if called with clean) and then link with all the included libraries.

This setup was tested in a real project and works correctly.

answer taken from here:


You’re on the right track. That is the proper way to include files within another – it’s actually required by the Android make system. One thing to note is that the line to clear variables should appear -after- you include the other makefiles, like this:

LOCAL_PATH:= $(call my-dir)

# Include makefiles here.
include $(LOCAL_PATH)/libos/
include $(LOCAL_PATH)/libbase/
include $(LOCAL_PATH)/utils/

# Clear variables here.
include $(CLEAR_VARS)

# Final settings.
LOCAL_MODULE := native

I’d also mention that there’s other important flags you may or may not want to set, including the following (an example from one of my makefiles):

# Settings.
LOCAL_LDLIBS                 := -lz -llog -lGLESv1_CM -lGLESv2 
LOCAL_ARM_MODE               := arm
LOCAL_MODULE                 := game

Finally, I’ve found the documentation that comes embedded within the Android ndk to be especially helpful. Mine is found in the following location:


Let me know if you have further questions. Hope this helps! 🙂


Very late answer here, but I had this problem and none of these solutions were useful. The solution turns out to be straightforward: as detailed here, set a MY_LOCAL_PATH variable and reallocate LOCAL_PATH every time:

MY_LOCAL_PATH := $(call my-dir)


... declare one module

include $(LOCAL_PATH)/foo/


... declare another module


I test the below code ok.

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

# 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/" = "$(wildcard */") # print: jni/
$(info [print-test] $$(wildcard */*/ = "$(wildcard */*/") # print: jni/xxdir/

I print the result :

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

Leave a Reply

Your email address will not be published. Required fields are marked *