步骤一:导入JniHelper.h头文件。
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "JniHelper.h"#endif;此时会遇到JniHelper.h头文件无法找到的问题,须要加入附加包括文件夹就可以解决这个问题。详细操作过程例如以下:右击项目---->c/c++---->常规---->附加包括文件夹---->加入JniHelper.h所在的路径就可以,eg:E:\安卓开发工作空间\cocos2d-x-2.2\cocos2dx\platform\android\jni。 此时可能还会遇到找不到jni.h和jni_md.h的问题,解决方式为在%JAVA_HOME%/include/下找到 jni.h,在%JAVA_HOME%/include/win32/下找到jni_md.h,拷贝到“Visual Studio文件夹/VC/include/”下,就可以解决这个问题。
步骤二:编写调用java端方法的代码。
void HelloWorld::fun(){#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo methodInfo; bool isHave = JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/hellocpp/HelloCpp","showAd","()V"); if(isHave) { methodInfo.env->CallStaticVoidMethod(methodInfo.classID,methodInfo.methodID); }#endif}
一定要加上平台推断语句。否则会出现下面bug。(详细怎样调用,请參见相关文档) 错误 2 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static bool __cdecl cocos2d::JniHelper::getStaticMethodInfo(struct cocos2d::JniMethodInfo_ &,char const *,char const *,char const *)" (__imp_? getStaticMethodInfo@JniHelper@cocos2d@@SA_NAAUJniMethodInfo_@2@PBD11@Z),该符号在函数 "public: void __thiscall HelloWorld::fun (void)" (?fun@HelloWorld@@QAEXXZ) 中被引用 E:\安卓开发工作空间\cocos2d-x-2.2\projects\jni2\proj.win32\HelloWorldScene.obj HelloCpp
解决方式:
1.改动Android.mk文件配置,加入例如以下语句 參考文章:
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
2.改动CCPlatformDefine.h文件
凝视掉#include "log.h",加入#include "ALog.h"。
3.新建ALog.h文件,写入下面内容,并将其放置于与CCPlatformDefine.h所在的同一文件夹下。
#pragma once #include #define LOG_TAG "debug log" #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args) #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args) #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)
步骤四:搞完第三步之后,再编译执行,还会提演示样例如以下错误。
"E:\\迅雷下载\\android-ndk-r9-windows-x86\\android-ndk-r9\\ndk-build.cmd" Android NDK: WARNING:E:\安卓开发工作空间\cocos2d-x-2.2\/cocos2dx/Android.mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries "Compile++ thumb : cocos2dcpp_shared <= HelloWorldScene.cppjni/../../Classes/HelloWorldScene.cpp:3:23: fatal error: JniHelper.h: No such file or directorycompilation terminated.make: *** [obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o] Error 1
即找不到JniHelper.h这个文件,解决方式例如以下。 LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := cocos2dcpp_sharedLOCAL_MODULE_FILENAME := libcocos2dcppLOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cppLOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes\ E:\安卓开发工作空间\cocos2d-x-2.2\cocos2dx\platform\android\jniLOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_staticLOCAL_WHOLE_STATIC_LIBRARIES += box2d_staticLOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_staticLOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -lloginclude $(BUILD_SHARED_LIBRARY)$(call import-module,cocos2dx)$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)$(call import-module,CocosDenshion/android)$(call import-module,extensions)$(call import-module,external/Box2D)$(call import-module,external/chipmunk)
就是在LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes\后面追加JniHelper.h文件所在的路径,让编译器编译本地的文件。相关的Android.mk里面的一些基础知识,參见 假设仍然提示找不到头文件
则在项目的cpp文件里第一步的路径改成以下的
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "platform/android/jni/JniHelper.h" #endif
步骤五:编写供c++调用的java方法,假设是第三方jar里面的方法的话,建议用Handler去触发,让其处于在UI线程中执行,否则可能出错。
package org.cocos2dx.hellocpp;import org.cocos2dx.lib.Cocos2dxActivity;import org.cocos2dx.lib.Cocos2dxGLSurfaceView;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import com.manage.LA;public class HelloCpp extends Cocos2dxActivity { /** * 保存当前Activity实例, 静态变量 */ private static Context mActivity = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = this; LA.initSDK(mActivity, "1005020"); Log.i("HelloCpp", "onCreate"); } public Cocos2dxGLSurfaceView onCreateView() { Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this); // HelloCpp should create stencil buffer glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); return glSurfaceView; } /** * 显示广告 */ public static void showAd() { mHandler.sendEmptyMessage(0); Log.i("HelloCpp", "showAd"); } public static Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case 0: LA.showAD1(mActivity); break; } } }; static { System.loadLibrary("cocos2dcpp"); }}
1.