RakNet + Android + Cocos2d-x

Hi there,
a lot of pepole here recommend RakNet library as a great solution for networking things. So I tried to learn something about it, downloaded and now I would like to add RakNet to my cocos2d-x Android helloworld project. Unfortunately, I found few information about it. I compiled .so files of RakNet and now I am unable to link it and use in C++ code. I guess the solution will be somewhere around Android.mk file, but it is all that I know.
Thank you for for advices.

Marek

1 Like

You have to add RakNet as an external lib for cocos2d-x.
Copy the Source folder of RakNet to <your_cocos2d-x_folder>/external/RaKNet

Create the file Android.mk with the following content in the RakNet folder from above:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := RakNet

LOCAL_MODULE_FILENAME := raknet

MY_PREFIX := $(LOCAL_PATH)/Source/
MY_SOURCES := $(wildcard $(MY_PREFIX)*.cpp)
LOCAL_SRC_FILES += $(MY_SOURCES:$(MY_PREFIX)%=Source/%)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/..

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/Source
                                 
include $(BUILD_STATIC_LIBRARY)

Add the RakNet lib and import definition to your main Android.mk file at the appropriate locations(there are already some static libs and import modules defined in your Android.mk).

LOCAL_WHOLE_STATIC_LIBRARIES += RakNet

$(call import-module,RakNet)
1 Like

Thank you very much! You saved a lot of my pain (and probably my final university project too) :smiley:
Here I include main Android.mk with linked RakNet. It took me some time to make it the right way that you wrote :smile:

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

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

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END


LOCAL_STATIC_LIBRARIES := cocos2dx_static

# _COCOS_LIB_ANDROID_BEGIN

LOCAL_WHOLE_STATIC_LIBRARIES += RakNet

# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)
$(call import-module,RakNet)

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
1 Like