Search code examples
androidc++android-studioandroid-ndkjava-native-interface

Android NDK: Multiple definition of .o and .c files


i want to implement a library with java and native code in my app project in Android Studio. Everytime when I change the c++ code, I have to recompile the c++ files so that the changes take effect in my app. Unfortunately the ndk-build command does not compile completely...

Everytime I run this command, it compiles all the c++ files and when it tries to create the .so-files I get lots of errors that are all looking like this (just with others classes). The changes I do are very simple and did not add any c++ include headers etc.

/Users/jenny/appproject/libraryWithNativeCode/src/main/obj/local/armeabi/objs/libraryWithNativeCode-mobile-example-app/src/WorldPins/WorldPinMessage.o: In function `_STLP_alloc_proxy':
/Users/jenny/NDK/android-ndk-r10e/sources/cxx-stl/stlport/stlport/stl/_string.c:647: multiple definition of `ExampleApp::WorldPins::WorldPinMessage::FocussedModel() const'
/Users/jenny/appproject/libraryWithNativeCode/src/main/obj/local/armeabi/objs/libraryWithNativeCode-mobile-example-app/src/WorldPins/WorldPinMessage.o:/Users/jenny/NDK/android-ndk-r10e/sources/cxx-stl/stlport/stlport/stl/_string.c:647: first defined here

My Android.mk and the Application.mk looks like the following

Android.mk

LOCAL_PATH := $(call my-dir)

$(info TARGET_ARCH_ABI is $(TARGET_ARCH_ABI))

$(info LOCAL_PATH is $(LOCAL_PATH))

PREBUILT_LIBS := $(LOCAL_PATH)/../libs/libraryWithNativeCode/prebuilt/android-$(TARGET_ARCH_ABI)

include $(CLEAR_VARS)
LOCAL_MODULE := libraryWithNativeCode-sdk-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libraryWithNativeCode-sdk.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := png-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libpng.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/png
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := curl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/curl/android-$(TARGET_ARCH_ABI)
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ssl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := crypto-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := http-parser-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libhttp-parser.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/http-parser
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := jpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libjpeg.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := turbojpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libturbojpeg.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/jpeg-turbo
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := libraryWithNativeCode-mobile-example-app
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -lz -lm
LOCAL_LDLIBS += -fuse-ld=bfd
LOCAL_STATIC_LIBRARIES := libraryWithNativeCode-sdk-lib png-lib curl-lib ssl-lib crypto-lib http-parser-lib jpeg-lib turbojpeg-lib android_native_app_glue ndk_helper

LOCAL_CFLAGS += -Wall -Wno-unknown-pragmas -Wno-sign-compare -Wno-format-security -Wno-reorder
#LOCAL_CFLAGS += -Werror

ifdef COMPILE_CPP_11
  $(info Configured for C++11)
  LOCAL_CPPFLAGS += -DCOMPILE_CPP_11=1 -std=c++11
else
  $(info Configured for C++0x)
endif

os_name:=$(shell uname -s)

get_android_cpp_files_cmd := find $(LOCAL_PATH) -type f  -iname "*.cpp"
get_android_includes_cmd  := find $(LOCAL_PATH) -type d
get_shared_cpp_files_cmd  := find $(LOCAL_PATH)/src -type f  -iname "*.cpp"
get_shared_includes_cmd   := find $(LOCAL_PATH)/src -type d
get_platform_includes_cmd := find $(LOCAL_PATH)/../libs/libraryWithNativeCode/platform -type d ! -path "*/OSX/*" ! -path "*/iOS/*"


ifeq ($(os_name),Darwin)
    cppfiles := ${shell ${get_android_cpp_files_cmd}}
    cppfiles += ${shell ${get_shared_cpp_files_cmd}}

    includes := ${shell ${get_android_includes_cmd}}
    includes += ${shell ${get_shared_includes_cmd}}
    includes += ${shell ${get_platform_includes_cmd}}
else
    # assume windows if not specified for now (due to no uname)
    cppfiles := ${shell sh -c '${get_android_cpp_files_cmd}'}
    cppfiles += ${shell sh -c '${get_shared_cpp_files_cmd}'}

    includes := ${shell sh -c '${get_android_includes_cmd}'}
    includes += ${shell sh -c '${get_shared_includes_cmd}'}
    includes += ${shell sh -c '${get_platform_includes_cmd}'}
endif


LOCAL_SRC_FILES := $(cppfiles:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(includes)

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson/internal

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)
$(call import-module,android/ndk_helper)

Application.mk

APP_PLATFORM := android-12
APP_STL := stlport_shared
APP_ABI := armeabi,armeabi-v7a,arm64-v8a

Anyone has an idea how to fix this?


Solution

  • I think the root cause of your problem is that you're including the same files more than once for compilation since get_android_cpp_files_cmd (find $(LOCAL_PATH) -type f -iname "*.cpp") is already returning files that will be returned by get_shared_cpp_files_cmd (find $(LOCAL_PATH)/src -type f -iname "*.cpp") as find is recursive.

    Also, this is unrelated to your issue, but I wonder why you're compiling for armeabi, armeabi-v7a, arm64-v8a, and targeting android-12+ ? honeycomb_mr1 is quite a confidential version, why not directly targeting kitkat+ instead, and also including x86 arch in your list?