11.cocos2dx2.2 some of the problems and solutions encountered by using JNI technology to invoke some of the methods inside the jar package.

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif;" > Step One: Import the JniHelper.h header file. </span>
#if (Cc_target_platform = = cc_platform_android) #include "JniHelper.h" #endif;
at this time will encounter JniHelper.h header file can not be found problems, need to add additional include folder to solve this problem. The detailed procedure is as follows: Right-click the project---->c/c++----> General----> Attach folder----> Join JniHelper.h path, eg:e:\ Android development workspace \ Cocos2d-x-2.2\cocos2dx\platform\android\jni.
At this point, you may also encounter problems found jni.h and jni_md.h, the solution is to find jni.h under%java_home%/include/,%java_home%/include/win32/found under Jni_md.h, copy to " Visual Studio Folder/vc/include/"To resolve this issue.

Step Two: Write code that calls the Java-side 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>
Be sure to add the platform inference statement. Otherwise, the following bug will appear. (Please refer to the relevant documentation for details on how to call)
<span style= "FONT-SIZE:18PX;" > Errors    2 error    LNK2019: unresolved external symbol "__declspec (dllimport) public:static bool __cdecl  cocos2d::jnihelper:: Getstaticmethodinfo (struct cocos2d::jnimethodinfo_ &,char const *,char Const *,CHAR const *) "(__imp_? [Email protected]@[email protected]@[email protected]@[email protected]), the symbol in the function "Public:void __thiscall HelloWorld:: Fun (void) "([email protected]@ @QAEXXZ) is referenced    e:\ android development Workspace \cocos2d-x-2.2\projects\jni2\proj.win32\ Helloworldscene.obj    hellocpp</span>

Step three: porting it to the Android platform, you will be prompted to log.h:no such file or directory in the CCPlatformDefine.h files, that is log.h not found.
How to resolve:1. Change the android.mk file configuration, add the following statements such as Local_ldlibs + =-l$ (sysroot)/usr/lib-llog2. Change the CCPlatformDefine.h filestare out the # include "Log.h" and add # include "ALog.h". 3. Create a new ALog.h file, write the following, and place it in the same folder as the 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 four: After the third step, and then compile the execution, but also to mention the demo sample such as the following error.
<span style= "FONT-SIZE:18PX;" > "e:\\ Thunderbolt download \\android-ndk-r9-windows-x86\\android-ndk-r9\\ndk-build.cmd" Android ndk:warning:e:\ development workspace \ Cocos2d-x-2.2\/cocos2dx/android.mk:cocos2dx_static:local_ldlibs is all 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: * * [o BJ/LOCAL/ARMEABI/OBJS/COCOS2DCPP_SHARED/__/__/CLASSES/HELLOWORLDSCENE.O] Error 1</span>

The JniHelper.h file cannot be found, and the solution is as follows.
<span style= "FONT-SIZE:18PX;" >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:\ Android Development Workspace \cocos2d-x-2.2\cocos2dx\platform\android\jnilocal_whole_static_librar IES + = 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) </span> 

is in local_c_includes: = $ (Local_path)/. /.. /classes\ append the path to the JniHelper.h file followed by the compiler to compile the local file. Some basic knowledge of the relevant android.mk, see http://www.himigame.com/lua-game/1388.htmlIf you are still prompted to find the header fileThe path to the first step in the CPP file of the project is changed to the following#if (Cc_target_platform = = cc_platform_android)
#include "Platform/android/jni/jnihelper.h"
#endif
Step Five: Write a Java method for C + + calls, assuming that it is a method inside a third-party jar, it is recommended to use handler to trigger it to execute in the UI thread, otherwise it may be wrong.
<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 {/** * Saves the current activity instance, static variable */private statics Context mactivity = null;protected void OnCreate (Bun Dle 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 bufferglsurfaceview.seteglconfigchooser (5, 6, 5, 0, +, 8); return Glsurfaceview;} /** * Display ad */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>


Articles:1.http://www.firedragonpzy.com.cn/index.php/archives/3140



11.cocos2dx2.2 some of the problems and solutions encountered by using JNI technology to invoke some of the methods inside the jar package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.