[SOLVED] COCOS2DX V3.9 can't build android apk with my own class added

I have created a new HELLOWORLD project with cocos2dX v3.9, NDK r9d, cocos console; by using cocos console compile method in terminal (MAC), it can produce the apk and install and run on a android device. The iOS part can be run on Xcode with no problems too.

Then I have created a SpriteExt class which just extends Sprite class (and do nothing else).

SpriteExt.h

#ifndef __TestProject__SpriteExt__
#define __TestProject__SpriteExt__

#include <iostream>
#include "cocos2d.h"

USING_NS_CC;

class SpriteExt: public Sprite {
public:
    SpriteExt(void);
    virtual ~SpriteExt();
    static SpriteExt* create(std::string filename);
    
    
};

#endif /* defined(__TestProject__SpriteExt__) */

SpriteExt.cpp

#include "SpriteExt.h"

SpriteExt::SpriteExt(void) {

}
SpriteExt::~SpriteExt(void) {
    
}

SpriteExt* SpriteExt::create(std::string filename) {
    SpriteExt* pS=new SpriteExt();
    pS->initWithFile(filename);
    pS->autorelease();
    return pS;
}

I used it to replace the cocos2dx icon sprite class, it works correctly on iOS, but I cannot build the apk for android anymore.

helloworld.cpp:

SpriteExt *sprite = SpriteExt::create("HelloWorld.png");

Here’s the error I got on terminal:

    .
    .
    .    
    [armeabi] StaticLibrary  : libcpufeatures.a
    [armeabi] Gdbserver      : [arm-linux-androideabi-4.8] libs/armeabi/gdbserver
    [armeabi] Gdbsetup       : libs/armeabi/gdb.setup
    [armeabi] SharedLibrary  : libcocos2dcpp.so
    jni/../../Classes/HelloWorldScene.cpp:72: error: undefined reference to 'SpriteExt::create(std::string)'
    collect2: error: ld returned 1 exit status
    make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1
    make: Leaving directory `/Users/kitdashxt/Desktop/TestProject/proj.android'
    Error running command, return code: 2.

tried to remove all folders in proj.android except jni, res and src to force terminal to rebuild it from scratch but it still didn’t work.
Anyone any tips/hints would be appreciated!
Thank you in advanced.

Did you import file SpriteExt.cpp to Android.mk file?

No I didn’t. Can u show me how, if the SpriteExt.cpp is at the classes folder ? Thank you ! @wanboouit

there is a Android.mk file in proj.android/jni, you have to add your .cpp files directory in that file

@calamitysir @wanboouit thank you so much it is working

here’s the Android.mk inside proj.android/jni folder
(added this line : …/…/Classes/SpriteExt.cpp under LOCAL_SRC_FILES)

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)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
		   ../../Classes/SpriteExt.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END


LOCAL_STATIC_LIBRARIES := cocos2dx_static

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END