Android NDK - 包含预建静态库会产生未定义的引用错误

Android NDK - Including prebuilt static libraries gives undefined reference errors

我正在尝试使用预构建的静态库构建共享库。由于某种原因,构建总是失败并给出 undefined reference 错误。如果我添加LOCAL_ALLOW_UNDEFINED_SYMBOLS := true,构建成功但是共享库只有几千字节而静态库大约3兆字节......所以链接静态库似乎出了问题。我不知道它可能是什么。我只使用 C++ 代码。

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := kes
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := libkes.a

include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE    := PortaKES
LOCAL_SRC_FILES := PortaKES.cpp
LOCAL_STATIC_LIBARIES := kes

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_ABI := all
APP_STL := stlport_static

JNI目录结构:

JNI folder structure

输出:

./obj/local/arm64-v8a/objs/PortaKES/PortaKES.o: In function `Java_com_kurzweil_portakes_KESFile_createPOLEFile':
C:\Users\niels.mylle\Documents\Kurzweil Mobile Android\PortaKES/jni/PortaKES.cpp:18: undefined reference to `CPoleFile::CPoleFile(char const*, bool, bool, CEncrypter*)'
./obj/local/arm64-v8a/objs/PortaKES/PortaKES.o: In function `Java_com_kurzweil_portakes_KESFile_createKSection':
C:\Users\niels.mylle\Documents\Kurzweil Mobile Android\PortaKES/jni/PortaKES.cpp:33: undefined reference to `CKSection::CKSection(char const*, CPoleFile*, CPoleLeaf*)'
./obj/local/arm64-v8a/objs/PortaKES/PortaKES.o: In function `Java_com_kurzweil_portakes_POLEFile_destroy':
C:\Users\niels.mylle\Documents\Kurzweil Mobile Android\PortaKES/jni/PortaKES.cpp:48: undefined reference to `CPoleFile::~CPoleFile()'
./obj/local/arm64-v8a/objs/PortaKES/PortaKES.o: In function `Java_com_kurzweil_portakes_POLEFile_exists':
C:\Users\niels.mylle\Documents\Kurzweil Mobile Android\PortaKES/jni/PortaKES.cpp:100: undefined reference to `CPoleFile::exists(char const*)'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [obj/local/arm64-v8a/libPortaKES.so] Error 1

也许这只是问题的一部分,但至少这个引用是错误的:

LOCAL_SRC_FILES := libkes.a

您正在为所有 ndk 兼容架构构建,但始终引用为每个架构预构建的相同静态,这是行不通的。

您应该为每个目标架构设置不同的 .a,即 ./armeabi/libkes.a./x86/libkes.a./armeabi-v7a/libkes.a 等。在这种情况下,您将能够使用 TARGET_ARCH_ABI 变量引用您的 .a,如下所示:

LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libkes.a