我想为版本 4.0.3 构建库 .so,但我无法这样做。 我的感觉是这些问题是因为我的.mk文件没有 与库链接。
Android.mk文件
Binder.cpp \
BpBinder.cpp \
CursorWindow.cpp \
IInterface.cpp \
IMemory.cpp \
IPCThreadState.cpp \
IPermissionController.cpp \
IServiceManager.cpp \
MemoryDealer.cpp \
MemoryBase.cpp \
MemoryHeapBase.cpp \
MemoryHeapPmem.cpp \
Parcel.cpp \
PermissionCache.cpp \
ProcessState.cpp \
Static.cpp
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS += -lpthread
LOCAL_MODULE := libbinder1
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
LOCAL_SRC_FILES := $(sources)
include $(BUILD_SHARED_LIBRARY)
#include $(CLEAR_VARS)
#LOCAL_CFLAGS += -DHAVE_PTHREADS
#LOCAL_LDLIBS += -lpthread
#LOCAL_MODULE := libbinder
#LOCAL_SRC_FILES := $(sources)
#include $(BUILD_STATIC_LIBRARY)
此文件为我构建静态文件,即 .a 文件,但在构建共享库时显示以下错误。
[armeabi] Compile++ thumb: binder1 <= IPCThreadState.cpp
jni/IPCThreadState.cpp:292:8: error: 'pthread_mutex_t' does not name a type
jni/IPCThreadState.cpp:294:8: error: 'pthread_key_t' does not name a type
jni/IPCThreadState.cpp: In static member function 'static android::IPCThreadState* android::IPCThreadState::self()':
我使用以下方法修复了上述错误 LOCAL_CFLAGS += -DHAVE_PTHREADS
但现在,在生成库时,我收到了一大堆错误。
D:/android-ndk-r9c-windows-x86/android-ndk-r9c/toolchains/arm-linux-androideabi- 4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux- androideabi/bin/ld.exe: error: cannot find -lpthread
D:/android-ndk-r9c-windows-x86/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/binder1/Binder.o: in function android::Vector<android::String16>::do_copy(void*, void const*, unsigned int) const:jni/utils/TypeHelpers.h:142: error: undefined reference to 'android::String16::String16(android::String16 const&)'
我们将不胜感激。
最佳答案
Android NDK 支持 pthread
,但不像 Linux 工具链那样提供 libpthread。如果您使用
LOCAL_CFLAGS += -DHAVE_PTHREADS
并且不添加 LOCAL_LDLIBS += -lpthread
关于do_copy()
的 undefined reference ,来自于系统库libutils.so
。使用未随 NDK 正式发布的库是不安全的(参见更多 here ),因此您最好重写这段代码。
可能您从 google source 收到了您的 Android.mk
文件 或其分支之一。我怀疑生成的库是否可用,因为原始的 libbinder.so
requires system app with elevated permissions 将在您的应用程序启动时加载。
无论如何,将系统库引用为 LOCAL_SHARED_LIBRARIES
不适用于 ndk-build
。而不是 你应该写LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
LOCAL_LDLIBS += -llog -lcutils -lutils
关于android - 无法在 Android Ndk 中构建本地共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22164564/