COCOS2DX java,c++ calling functions to each other

Source: Internet
Author: User

C + + Call Java example
1 #if(Cc_target_platform = = cc_platform_android)2#include"Platform/android/jni/jnihelper.h"3 4 intKuniulogin (Lua_state *Lua)5 {6 Jnimethodinfo MethodInfo;7Auto IsOk = Jnihelper::getstaticmethodinfo (MethodInfo,"org/cocos2dx/cpp/appactivity","Sdklogin","() V");8     if(isOk)9     {TenAuto Activityobj = methodinfo.env->Callstaticobjectmethod (Methodinfo.classid, methodinfo.methodid); One     } A     return 0; - } - #endif

Premise hypothesis:

There is such a Java file.

Org/cocos2dx/cpp/appactivity.java

This class has a static common function of Sdklogin.

Class Appactivity {

public static void Sdklogin () {}

};

We need to call the above Java function Sdklogin through C + +.

JniHelper.h is the COCOS2DX engine that comes with the Java-based header file, which does not work on the Windows platform.

If you need a syntax hint for VS, you can write it out of conditional compilation,

This will be an error, because it contains the jni.h file, this file in the Java Virtual Machine installation directory can be found,

Put it in the VS include directory.

" org/cocos2dx/cpp/appactivity " " Sdklogin " " () V ");

The first parameter is a parameter of type Jnimethodinfo.

The second parameter specifies the path to the Java file,

The third argument is the function name,

The fourth parameter is a parameter signature. (Google has a more detailed explanation of the signature).

Returns true to indicate that the GET function succeeded.

So

Auto Activityobj = Methodinfo.env->callstaticobjectmethod (Methodinfo.classid, Methodinfo.methodid);

This line is called the ...

Java calls the C + + example.
1 extern "C" {2 #if(Cc_target_platform = = cc_platform_android)3     voidJava_org_cocos2dx_cpp_jnitesthelper_kuniulogin (JNIENV *env, Jobject thiz, jstring jstr)4     {5Auto Stropenid = env->Getstringutfchars (Jstr, NULL);6Env->Releasestringutfchars (Jstr, Stropenid);7     }8 #endif9}

Notice the name of the function.

Java prefix.

Org_cocos2dx_cpp the package name.

The Jnitesthelper class name.

The name of the Kuniulogin function.

Parameters:

The first two parameters are required, the specific use of Google has:

The following parameters fill in the required, note type.

When you get parameters inside a function, you get them through the above method.

Java just build a class, named Jnitesthelper,

Define the function Kuniulogin, which must be defined according to the NDK convention.

public static native void Kuniulogin (String openId);

The native prefix must be added, and this function does not need to be implemented in Java and automatically maps C + + functions.

When using the direct Jnitesthelper.kuniulogin (""); You can do it.

COCOS2DX java,c++ calling functions to each other

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.