Static libraries into Shared One

When compiling several static libraries into one shared using Android NDK I ran into a problem and fixing it required changing the NDK.

Here is what the problem is. Let say I use this Android.mk:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mod1
LOCAL_SRC_FILES := libmod1.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := mod2
LOCAL_SRC_FILES := libmod2.a
include $(PREBUILT_STATIC_LIBRARY)
#...more prebuilt static libs here...
include $(CLEAR_VARS)
LOCAL_MODULE := main
LOCAL_SRC_FILES := main.cpp
LOCAL_WHOLE_STATIC_LIBRARIES := mod1 mod2 <...>
include $(BUILD_SHARED_LIBRARY)

This won’t work in NDK 5 and 5b, specifically the macro LOCAL_WHOLE_STATIC_LIBRARIES won’t do anything.

This is how to fix it:

locate the setup.mk file you use, it’s one of those:

find -name setup.mk
./sources/cxx-stl/system/setup.mk
./toolchains/arm-eabi-4.4.0/setup.mk
./toolchains/x86-4.2.1/setup.mk
./toolchains/arm-linux-androideabi-4.4.3/setup.mk

In my case it was “./toolchains/arm-linux-androideabi-4.4.3/setup.mk”.

In that setup.mk locate defines cmd-build-shared-library and cmd-build-executable.

In these defines look at the call with $(PRIVATE_WHOLE_STATIC_LIBRARIES) as a parameter:

$(call link-whole-archives,$(PRIVATE_WHOLE_STATIC_LIBRARIES))

make sure that “link-whole-archives” matches the one in build/core/definitions/mk.

In my toolchain I had “whole-link-archives-tags” instead of “link-whole-archives”.

Happy Android hacking!