androidandroid-ndkandroid-make

Cannot build local shared library in Android Ndk


I want to build library .so for version 4.0.3 but I am unable to do so. What I feel is that these problems are caused because my .mk file is not linking with the libraries.

Android.mk file

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)

This file builds static i.e .a file for me but shows following errors while building shared library.

[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()':

I fixed above errors using LOCAL_CFLAGS += -DHAVE_PTHREADS

But now, at the time of generating library I am getting a huge list of errors.

   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&)'

Any help will be appreciated.


Solution

  • Android NDK supports pthreads, but does not provide libpthread as usual in Linux toolchains. Your first error message will be gone if you use

    LOCAL_CFLAGS += -DHAVE_PTHREADS
    

    and not add LOCAL_LDLIBS += -lpthread

    Regarding the undefined reference to do_copy(), it comes from system library libutils.so. It is not safe to use libraries that are not officially published with NDK (see more here), so you better rewrite this piece of code.

    Probably you received your Android.mk file from the google source or one of its forks. I doubt that the resulting library will be useable, because the original libbinder.so requires system app with elevated permissions will be loaded when your app starts.

    Anyways, referring to system libraries as LOCAL_SHARED_LIBRARIES does not work with ndk-build. Instead of LOCAL_SHARED_LIBRARIES := liblog libcutils libutils you are expected to write

    LOCAL_LDLIBS += -llog -lcutils -lutils