无法在Android Ndk中构建本地共享库

时间:2014-03-04 06:28:53

标签: android android-ndk android-make

我想为版本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&)'

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

Android NDK支持pthread,但在Linux工具链中不像往常一样提供 libpthread 。如果您使用

,您的第一条错误消息将消失
LOCAL_CFLAGS += -DHAVE_PTHREADS

并且不添加 LOCAL_LDLIBS += -lpthread

关于do_copy()的未定义引用,它来自系统库libutils.so。使用NDK未正式发布的库是不安全的(请参阅更多here),因此您最好重写这段代码。

可能您收到了来自 google source 或其中一个分叉的Android.mk文件。我怀疑生成的库是否可用,因为原始的libbinder.so 要求系统应用程序具有提升的权限将在您的应用程序启动时加载。

无论如何,将系统库称为LOCAL_SHARED_LIBRARIES不能与ndk-build一起使用。而不是 LOCAL_SHARED_LIBRARIES := liblog libcutils libutils ,你应该写

LOCAL_LDLIBS += -llog -lcutils -lutils
相关问题