Cocos2d-x's JNI use (docking android various SDK)

Source: Internet
Author: User

Game finished, to publish to the various platforms, iOS, Android is definitely not, then this article on Android platform docking agent Billing SDK, channels, five operators, advertising, sharing, data statistics and so on the JNI call, the SDK is really all kinds of trouble ah ...

First of all, so many SDKs, features are different, so there must be a unified SDK management class to manage, and then the game need to use what function when the call to the specified interface, interface implementation, different platforms in the call sdk~

Then, C + + calls the Java layer: (Take display ads for example)

====================================//c++//====================================//com/ican/redstar/---> The path of the called Class (the package name can be different from the absolute pathname)    //redstarsdkmanager---> class name//SHOWYCAD   ---> Interface name to invoke//() V    ---> Return value void  parameter void#define class_name "Com/ican/redstar/redstarsdkmanager" #if cc_target_platform = = Cc_platform_ Androidjnimethodinfo t;if (Jnihelper::getstaticmethodinfo (T,class_name, "Showycad", "() V")) {t.env-> Callstaticvoidmethod (T.classid, T.methodid); T.env->deletelocalref (T.CLASSID);} Else{cclog ("C + + call Java Showycad faild!");} #endif//========================================//java//========================================public Static void Showycad () {log.d ("Ycad", "Call from Cocos2d-x ..."); Try{yccpmanager.getinstance (). SHOWCP (GetContext (). Getapplicationcontext ());} catch (Exception e) {log.d ("Ycad", "show CP thow uncaught Exception");}}

Yes, very simple ~, of course, if you want to pass a parameter, or the function has a return value, it will be a little more complicated, and finally to discuss, first look at the Java layer out of C + +

==============================//java//==============================//directly declare a native method, such as user login return//jni  callbackprivate static native void Loginendcallback (int loginsuccess, String usename);//============================= =//c++//==============================//Login Callback//com_ican_redstar_redstarsdkmanager---> Path, class name, function name are underlined// Jniexport void Jnicall java_com_ican_redstar_redstarsdkmanager_loginendcallback (jnienv *env, Jobject thiz, Jint Loginsuccess, jstring usename) {Jboolean jb_copy = false;string nameStr = string (Env->getstringutfchars (UseName, &jb_copy)); int sec = loginsuccess; Cclog ("%s jni called True  [login =%d, UserName =%s]", __function__, sec, Namestr.c_str ()); Redstarsdkmanager::getinstance ()->logincallback (sec, nameStr);}

Great, Java tune C + + is also simple.

Finally, let's take a look at parameter passing, and return with the function:

Learn from other friends of the blog, Lunch break ~

Http://www.blogjava.net/china-qd/archive/2006/04/29/44002.html (Explanation of parameter passing)

http://blog.csdn.net/luxiaoyu_sdc/article/details/15874505 (Detailed Cocos2d::jnihelper)

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.