Search code examples
androidc++candroid-ndklinkage

Android NDK doesn't link prebuilt library


I'm trying to link a C static library to a C++ shared library, but it seems that the shared library completely ignores the LOCAL_STATIC_LIBRARIES line, and doesn't link the static lib.

TextureEngine is the static lib, and CustomTexture is the shared library. Here are the make files


Static library - TextureEngine

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := TextureEngine
LOCAL_SRC_FILES := ../TextureEngine.c
# LOCAL_SHARED_LIBRARIES := 
#LOCAL_LDLIBS := -llog -landroid -lGLESv2
include $(BUILD_STATIC_LIBRARY)

Application.mk:

APP_ABI := all
APP_PLATFORM := android-23
APP_MODULES := TextureEngine

Shared library - CustomTexture

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := TextureEngine
LOCAL_SRC_FILES := \
    ../TextureEngine/obj/local/$(TARGET_ARCH_ABI)/libTextureEngine.a
LOCAL_LDLIBS += -landroid -lGLESv2 -lm -lz -llog 
include $(PREBUILT_STATIC_LIBARAY)


include $(CLEAR_VARS)
LOCAL_MODULE    := CustomTexture
LOCAL_STATIC_LIBRARIES := TextureEngine
LOCAL_C_INCLUDES := ../TextureEngine
LOCAL_SRC_FILES := \
    ../CustomTexture.cpp \
    ../TextureProvider.cpp \
    ../Logfile.c \
    ../SineImage.cpp
LOCAL_LDLIBS += -landroid -lGLESv2 -lm -lz -llog 
include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL := gnustl_static
APP_CPPFLAGS += -std=c++11
APP_ABI := all
APP_PLATFORM := android-23

When I run ndk-build on TextureEngine, I get libTextureEngine.a files on all architecture folders. When I run ndk-build on CustomTexture however, I get undefined references.

Running ndk-build with V=1 on CustomTexture gives

[arm64-v8a] SharedLibrary  : libCustomTexture.so
/opt/android-ndk-r12b/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-g++ -Wl,-soname,libCustomTexture.so -shared --sysroot=/opt/android-ndk-r12b/platforms/android-23/arch-arm64 ./obj/local/arm64-v8a/objs/CustomTexture/__/CustomTexture.o ./obj/local/arm64-v8a/objs/CustomTexture/__/TextureProvider.o ./obj/local/arm64-v8a/objs/CustomTexture/__/Logfile.o ./obj/local/arm64-v8a/objs/CustomTexture/__/SineImage.o /opt/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a -lgcc -no-canonical-prefixes  -Wl,--build-id -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings  -L/opt/android-ndk-r12b/platforms/android-23/arch-arm64/usr/lib -landroid -lGLESv2 -lm -lz -llog -lc -lm -o ./obj/local/arm64-v8a/libCustomTexture.so

As you can probably see - TextureEngine doesn't appear in the linker line. Sure enough, I can change the LOCAL_SRC_FILES on the prebuilt part of CustomTexture's Android.mk - And no new errors arise.

Why doesn't ndk-build try and link TextureEngine?


Solution

  • include $(PREBUILT_STATIC_LIBARAY)
    

    Because of the typo here. LIBRARY, not LIBARAY :)

    I actually discovered a bug last night where modules in LOCAL_STATIC_LIBRARIES or LOCAL_SHARED_LIBRARIES that don't exist don't actually cause errors, which explains why its easy to make this kind of mistake and not have any clear errors in your build.