Ndk build library outside the main tree of the source project

I want to create a third-party avahi library using ndk. avahi already has an Android port (with current Android.mk).

What I did: I successfully created a project and copied all the source code into jni / folder and ran ndk-build. He creates libavahi.so

What I want to do: Instead of copying all the source code to jni / folder, I would like to save it in the folder with the project source tree. What should I do? I looked at the NDK DOCUMENTATION / Import Module, but nothing similar to my case.

New to ndk and any suggestion is welcome.

+7
source share
1 answer

You are right, this does not apply to the import module. The way you reference the avihi library from your own code will still be LOCAL_SHARED_LIBRARIES (see the NDK module-exports example). But in your jni/Android.mk file, you can use the include command for another file. This command is very similar to the #include statement in C. This file should not be inside your project tree. Taking the same sample, here is how it can work:

Original Android.mk from samples / module-exports / jni :

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := foo LOCAL_SRC_FILES := foo/foo.c LOCAL_CFLAGS := -DFOO=2 LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo LOCAL_EXPORT_CFLAGS := -DFOO=1 LOCAL_EXPORT_LDLIBS := -llog include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := bar LOCAL_SRC_FILES := bar/bar.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar LOCAL_STATIC_LIBRARIES := foo include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := zoo LOCAL_SRC_FILES := zoo/zoo.c LOCAL_SHARED_LIBRARIES := bar include $(BUILD_SHARED_LIBRARY) 

The modified file will look like this:

 ZOO_LOCAL_PATH := $(call my-dir) include ~/projects/bar/jni/Android.mk LOCAL_PATH := $(ZOO_LOCAL_PATH) include $(CLEAR_VARS) LOCAL_MODULE := zoo LOCAL_SRC_FILES := zoo/zoo.c LOCAL_SHARED_LIBRARIES := bar include $(BUILD_SHARED_LIBRARY) 

And the external bar /jni/Android.mk:

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := foo LOCAL_SRC_FILES := foo/foo.c LOCAL_CFLAGS := -DFOO=2 LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo LOCAL_EXPORT_CFLAGS := -DFOO=1 LOCAL_EXPORT_LDLIBS := -llog include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := bar LOCAL_SRC_FILES := bar/bar.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar LOCAL_STATIC_LIBRARIES := foo include $(BUILD_SHARED_LIBRARY) 

Now both bar.c and foo.c files can be stored outside the tree of the zoo project!

+5
source

All Articles