项目中如果使用到第3方的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
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 //第1步
LOCAL_SRC_FILES := prebuilt/libuninstall.so //第2步
include $(PREBUILT_SHARED_LIBRARY) //第3步
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 //第4步
$(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)
注意事项
在主java文件中找到System.loadLibrary("cocos2dcpp");这句,然后在下面一样添上你要加载的3方库名称便可。貌似默许不指明的话,会到系统路径下去找so文件(没有root或system的权限,无权对这个文件夹操作),这几个so按道理是会装到data/appname/lib目录下的。
加载顺序
这里有个问题要特别注意,就是这些动态库的加载顺序,1定要放到libcocos2dcpp前加载,否则在载入libcocos2dcpp时,会由于没有之前这两个依赖的动态库而报错,报的就是对应的动态库木有载入。