Search code examples
androidandroid-ndkmakefileandroid-ndk-r7

Changes to imported makefile don't trigger rebuild in android-ndk


I have an Android JNI project I'd like to compile with ndk-build. The project contains of multiple third-party sub projects.

+- jni
   +- Android.mk
   +- my-proj.mk
   +- other-proj.mk
   +- my-proj
      +- a.cpp
      +- b.cpp
   +- other-proj  (third-party)
      +- c.cpp
      +- d.cpp

The idea now is to include/import the makefiles of all sub projects in Android.mk, like this:

LOCAL_PATH := $(call my-dir)

include $(LOCAL_PATH)/my-proj.mk
include $(LOCAL_PATH)/other-proj.mk

other-proj is built as static library. my-proj.mk depends on other-proj and is built as shared library.

Building this project works. However, modifying either my-proj.mk or other-proj.mk doesn't trigger a rebuild of the respective project. Is there a way to do this?

I though I could list the makefiles as dependencies of Android.mk but I couldn't figure out a way. (Listing them under LOCAL_SRC_FILES doesn't work.)

I also read about $(call import-module,foo/bar) which seems to do exactly what I want. However, in this case I had to place the makefiles in a directory adjacent to the project directories (e.g. jni/makefiles/other-proj/Android.mk) but I couldn't figure out how to specify the LOCAL_SRC_FILES. They don't seem to like to be specified with an absolute path or with a .. inside the path. (I can't place the makefiles directly in the sub project directories as they're third-party projects.)


Solution

  • I figured out a way to do it, although not with simple includes but rather with $(call import-module,foo/bar).

    First change, you need to decide on one of the sub projects being the main project. Let's take my-proj for that. Then the directory structure changes like this:

    +- jni
       +- Android.mk
       +- my-proj
          +- a.cpp
          +- b.cpp
       +- other-proj  (third-party)
          +- Android.mk
          +- c.cpp
          +- d.cpp
    

    Basically I renamed my-proj.mk to /Android.mk and other-proj.mk to other-proj/Android.mk.

    The main Android.mk then changes to something like this:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    ...
    LOCAL_STATIC_LIBRARIES := other-proj
    include $(BUILD_SHARED_LIBRARY)
    
    $(call import-add-path,$(LOCAL_PATH))
    $(call import-module,other-proj)
    

    The other-proj is included in the last line.

    Note that the line before the last line sets the module path. Setting the module path from within the makefile via

    NDK_MODULE_PATH := $(LOCAL_PATH)
    

    does not work. (If NDK_MODULE_PATH is to be used, it needs to be defined as an environment variable outside of the makefile).