首页 > 代码库 > Cocos2d-x中使用第三方so库

Cocos2d-x中使用第三方so库

项目中如果使用到第三方的SDK,大多数是以.so动态共享库的文件打包给我们使用,如何使用他们,见下面分析。

1、获得库文件

假如我们得到的库文件是libxxx.so(注:关于.so文件的命名方式,可百度)。在Jni目录下新建prebuilt文件夹,把获得的库文件
拷贝到该文件夹下。

2、使用库文件

库文件的使用需要修改Android.mk,在mk文件中加入以下代码:
include $(CLEAR_VARS)  
LOCAL_MODULE := xxx  
LOCAL_SRC_FILES := prebuilt/libxxx.so  
include $(PREBUILT_SHARED_LIBRARY)  
  
LOCAL_SHARED_LIBRARIES := xxx

Java中调用

static {
        System.loadLibrary("XXX");
        System.loadLibrary("cocos2dcpp");
} 

3、编译

编译后会在libs/armeabi目录下生产我们需要的libxxx.so文件,并编译到APK中。

4、万普例子

以下是万普平台Demo的mk文件,在这贴出来,让大家体会以下使用方法。
LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)
LOCAL_MODULE := uninstall  //第一步

LOCAL_SRC_FILES := prebuilt/libuninstall.so   //第二步
include $(PREBUILT_SHARED_LIBRARY)            //第三步

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/WapsAd.cpp

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

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES := cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static

# LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static
# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := uninstall     //第四步

$(call import-module,.)
$(call import-module,audio/android)
$(call import-module,Box2D)
$(call import-module,editor-support/cocostudio)
$(call import-module,extensions) 


# $(call import-module,Box2D)
# $(call import-module,editor-support/cocosbuilder)
# $(call import-module,editor-support/spine)
# $(call import-module,editor-support/cocostudio)
# $(call import-module,network)
# $(call import-module,extensions)





Cocos2d-x中使用第三方so库