Android NDK with multiple pre-built libraries

I am currently playing a bit with the NDK and every step seems to be draining an hour to figure out stuff.
This time, I need to compile a shared library, which depends on pre-built multiple static and shared libraries. The NDK documentation example seems naive enough to be straightforward to have it working, but it took me some time to figure out how to do it.

Here’s a valid Android.mk file which references two other pre-built static libraries. It took me some time to realize that:

  • The LOCAL_STATIC_LIBRARIES reference the name of the LOCAL_MODULE for the pre-built libraries, not the name of the library or anything alike. The name LOCAL_STATIC_LIBRARIES can be a bit misleading, and thus, lpw must be specified instead of the library name.
  • The LOCAL_SRC_FILES for each module can only reference one library, and the path will be relative to the point you invoke the ndk-build command, but absolute paths will work like a charm.
  • The $(TARGET_ARCH_ABI) will reference the valid library for the currently compiling architecture, so it is a must have.

LOCAL_PATH := $(call my-dir)

########################
# prepare lib1
include $(CLEAR_VARS)
LOCAL_MODULE    := lpw
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libPluginWhatever.a
include $(PREBUILT_STATIC_LIBRARY)
########################

########################
# prepare lib2
include $(CLEAR_VARS)
LOCAL_MODULE    := lib2
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib2.a 
include $(PREBUILT_STATIC_LIBRARY)
########################

... your other stuff

LOCAL_STATIC_LIBRARIES := android_native_app_glue lib2 lpw

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)

Normally, when the ndk-build command ends, your libraries will be installed in the libs directory, a folder created in the same directory you invoked the nkd-build in. If you invoke ndk-build manually, Gradle will miserably fail to include the generated library files since it expects the installed libraries to be in the folder jniLibs instead of libs. If have a convenient symbolic link jniLibs->libs for this inconvenience.

Leave a comment