[Solved] Compilation error: UIWidget undefined reference

I’m trying to compile my game for Android Studio, and I’m getting the following errors:

/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:172: error: undefined reference to 'vtable for cocos2d::ui::Widget'
/Users/matthewlehew/Developer/Android/ndk/android-ndk-r10c/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9/../../../../arm-linux-androideabi/bin/ld: the vtable symbol may be undefined because the class is missing its key function
/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:177: error: undefined reference to 'vtable for cocos2d::ui::Widget'
/Users/matthewlehew/Developer/Android/ndk/android-ndk-r10c/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9/../../../../arm-linux-androideabi/bin/ld: the vtable symbol may be undefined because the class is missing its key function
/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:265: error: undefined reference to 'typeinfo for cocos2d::ui::Widget'
/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:551: error: undefined reference to 'typeinfo for cocos2d::ui::Widget'
/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:712: error: undefined reference to 'typeinfo for cocos2d::ui::Widget'
/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/../cocos2d/cocos/ui/UIWidget.cpp:1196: error: undefined reference to 'typeinfo for cocos2d::ui::Widget'
./obj/local/armeabi/libui.a(UILayout.o):UILayout.cpp:vtable for cocos2d::ui::Layout: error: undefined reference to 'cocos2d::ui::Widget::setGlobalZOrder(float)'
./obj/local/armeabi/libui.a(UIListView.o):UIListView.cpp:vtable for cocos2d::ui::ListView: error: undefined reference to 'cocos2d::ui::Widget::setGlobalZOrder(float)'
./obj/local/armeabi/libui.a(UIPageView.o):UIPageView.cpp:vtable for cocos2d::ui::PageView: error: undefined reference to 'cocos2d::ui::Widget::setGlobalZOrder(float)'
./obj/local/armeabi/libui.a(UIScrollView.o):UIScrollView.cpp:vtable for cocos2d::ui::ScrollView: error: undefined reference to 'cocos2d::ui::Widget::setGlobalZOrder(float)'
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1
make: Leaving directory `/Users/matthewlehew/Documents/CocosProjects/RueRunner/proj.android-studio/app'
Error running command, return code: 2.

The game compiles in Xcode for iOS, I’m only getting the error when attempting to compile for Android. My Android.mk file is as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/extensions)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/ui)
$(call import-add-path,$(LOCAL_PATH))


LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

CPP_FILES := $(shell find $(LOCAL_PATH)/../../../Classes -name *.cpp)
CPP_FILES += $(shell find $(LOCAL_PATH)/../../../Classes/GameSparksSDK -name *.c)
LOCAL_SRC_FILES := hellocpp/main.cpp
LOCAL_SRC_FILES += $(CPP_FILES:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(shell find $(LOCAL_PATH)/../../../Classes -type d)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../cocos2d/extensions/PELoader 

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END

LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_STATIC_LIBRARIES += cocos_ui_static

LOCAL_WHOLE_STATIC_LIBRARIES := PluginIAP
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_ui_static

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)
$(call import-module, ./sdkbox)
$(call import-module, ./pluginiap)
$(call import-module,ui)

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END

Anyone got any tips? Have I forgotten something? Thanks!

For what it’s worth, I’m also experiencing this problem on a hello world project generated by Cocos.

How are you initiating the build? using cocos command line?

Yep! Using the following: sudo cocos compile -p android --android-studio

Also, I’m using Cocos2d-x 3.10 source, created by using the Cocos app, on OS X.

I don’t think you should need to use sudo at all.

You used the Cocos GUI tool, created a new project and this does not compile straight out of the box?

I ended up using sudo because I was having some permissions errors…some of the folders in the proj.android-studio/app folder were assigned to root. However, I just did a chown reassignment and ran without sudo, and wound up with the same error.

This is correct. Right now, I’m reinstalling the Cocos GUI tool in case something happened to the copy of the 3.10 engine that was on my computer. I’ll let you know how it goes.

Confirmed, it looks like the problem was in my local copy of the 3.10 engine, which caused the problem to reproduce in any new project created in the Cocos GUI tool. A reinstall fixed this particular issue.