<Span style = "font-family: Arial, Helvetica, sans-serif;"> Step 1: import the jnihelper. h header file. </Span>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "JniHelper.h"#endif;
In this case, the jnihelper. h header file cannot be found. You need to add an included directory to solve the problem. The procedure is as follows: Right-click the project ----> C/C ++ ----> General ----> Add the include directory ----> Add jnihelper. h path, eg: e: \ Android development workspace \ cocos2d-x-2.2 \ cocos2dx \ platform \ Android \ JNI.
JNI cannot be found. H and jni_md.h. The solution is to find JNI under % java_home %/include. h. Find jni_md.h under % java_home %/include/Win32/and copy it to "Visual Studio directory/VC/include/" to solve the problem.
Step 2: Compile the code to call the Java method.
<span style="font-size:18px;">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}</span>The Platform judgment statement must be added. Otherwise, the following bugs may occur. (For details about how to call it, see the relevant documentation)
<Span style = "font-size: 18px;"> Error 2 error lnk2019: external symbol that cannot be parsed "_ declspec (dllimport) Public: static bool _ cdecl cocos2d :: jnihelper: getstaticmethodinfo (struct cocos2d: jnimethodinfo _ &, char const *) "(_ imp _? [Email protected] @ [email protected] @ [email protected] @ [email protected]). This symbol is used in the function "public: void _ thiscall helloworld: Fun (void) "([email protected] [email protected] @ qaexxz) referenced in E: \ Android development workspace \ cocos2d-x-2.2 \ projects \ jn2\ proj. win32 \ helloworldscene. OBJ hellocpp </span>
Step 3: port it to the Android platform. The system will prompt log. h: no such file or directory in the ccplatformdefine. h file, that is, log. h cannot be found.
Solution: 1. modify android. MK file configuration, add the following statement local_ldlibs + =-L $(sysroot)/usr/lib-llog2. modify ccplatformdefine. # include "log. H ", add # include" alog. H ". 3. Create a. h file, write the following content, and place it in the same directory as ccplatformdefine. h.
<span style="font-size:18px;">#pragma once #include<android/log.h> #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) </span>
Step 4: After completing step 3, compile and run the program again. The following error message is displayed.
<Span style = "font-size: 18px;"> "E: \ thunder Download \ android-ndk-r9-windows-x86 \ android-ndk-r9 \ ndk-build.cmd" android ndk: Warning: e: \ Android development workspace \ 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 </span>
The jnihelper. h file cannot be found. The solution is as follows.
<Span style = "font-size: 18px;"> local_path :=$ (call my-DIR) include $ (clear_vars) local_module: = cocos2dcpp_sharedlocal_module_filename: = colibcos2dcpplocal_src_files: = hellocpp/main. CPP .. /.. /classes/appdelegate. CPP .. /.. /classes/helloworldscene. cpplocal_c_includes: = $ (local_path )/.. /.. /classes E: \ Android development workspace \ cocos2d-x-2.2 \ cocos2dx \ platform \ Android \ jnilocal_whole_static_libraries + = 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) </span>
Add the path of jnihelper. h file after local_c_includes :=$ (local_path)/.../../Classes \ To let the compiler compile local files. For some basic knowledge about Android. mk, see http://www.himigame.com/lua-game/1388.html.
Step 5: compile a Java method called by C ++. If it is a method in a third-party jar, we recommend that you use handler to trigger the method so that it is running in the UI thread, otherwise, an error may occur.
<Span style = "font-size: 18px;"> 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 {/*** Save the current activity instance, static variable */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 () {javasglsurfaceview = new cocos2dxglsurfaceview (this); // hellocpp shocould create stencel bufferglsurfaceview. seteglconfigchooser (5, 6, 5, 0, 16, 8); Return glsurfaceview;}/*** display advertisement */public static void showad () {mhandler. sendemptymessage (0); log. I ("hellocpp", "showad");} public static handler mhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {// todo auto-generated method stubsuper. handlemessage (MSG); Switch (MSG. what) {Case 0: La. showad1 (mactivity); break ;}}; static {system. loadlibrary ("cocos2dcpp") ;}</span>