1

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.

ortisenderos
  • 370
  • 4
  • 15
Mohit
  • 505
  • 8
  • 31

1 Answers1

2

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
Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 1
    What you seem to be suggesting is that the poster is trying to use platform/AOSP-internal functionality not exported to the NDK. However, the way you are saying it isn't quite accurate and seems to confuse the issues of *exports* vs *permissions*, for example, if you examine the process of a 3rd-party app you will find that libbinder.so is a dependency (one already loaded by zygote) - that does not actually "require a system app with elevated permissions" though it is not intended for 3rd party code to directly interact with, and will be tricky to make any useful use of. – Chris Stratton Mar 04 '14 at 17:31
  • @ChrisStratton thank you for clarification. I confirm that `libbinder.so` may be loaded in user process. – Alex Cohn Mar 04 '14 at 18:41