The Cocos2d-x3.1 calls the static function in the Java class through JniHelper

Source: Internet
Author: User

The Cocos2d-x3.1 calls the static function in the Java class through JniHelper
1. Create a Cocos2d-x Project (1) cd/Users/own user name/Desktop/cocos2d-x-3.1/tools/cocos2d-console/bin (2) cocos new JniDemo-p com. packet.com. jni-l cpp-d's own project Path 2. android Import ADT (1) FILE-> Import-> Android-> Existing Android Code Into Workspace (2) next-> Browse, find proj in the created project directory. android: Finish. 3. Import the new project to XCode (1). Click proj. XXXX in ios_mac. xcodeproj completes the import. (2) Add the following code under the HelloWorld. cpp header file:

# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) # include
 
  
# Include ".. /.. /cocos2d/cocos/platform/android/jni/JniHelper. h "// The path here should be changed according to the actual situation of your project # include # if 1 # define LOG_TAG" JniHelper "# define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG ,__ VA_ARGS _) # else # define LOGD (...) # endif
 
(3) Add the following code to the following function:
Void HelloWorld: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) JniMethodInfo info; // getStaticMethodInfo determines whether a static function defined by java exists, return bool ret = JniHelper: getStaticMethodInfo (info, "org/cocos2dx/cpp/TestJni", "func1", "() V"); if (ret) {log ("call void func1 () succeed"); // input the class ID and method ID. Be careful when the method name is incorrect. The first letter is an upper-case info. env-> CallStaticVoidMethod (info. classID, info. methodID);} ret = JniHelper: getStaticMethodInfo (info, "org/cocos2dx/cpp/TestJni", "func2", "() I"); if (ret) {log ("call int func2 () succeed"); jint iret = info. env-> CallStaticIntMethod (info. classID, info. methodID); log ("func2 return = % d", iret);} ret = JniHelper: getStaticMethodInfo (info, "org/cocos2dx/cpp/TestJni", "func3 ", "(I) Ljava/lang/String;"); if (ret) {log ("Call int func3 (int) succeed"); jobject jobj = info. env-> CallStaticObjectMethod (info. classID, info. methodID, 1438);} ret = JniHelper: getStaticMethodInfo (info, "org/cocos2dx/cpp/TestJni", "func4", "(Ljava/lang/String ;) ljava/lang/String; "); if (ret) {log (" call string func4 (string) succeed "); jobject para = info. env-> NewStringUTF ("haha"); jstring jstr = (jstring) info. env-> CallStaticObjectMethod (info. classID, info. methodID, para); std: string text = JniHelper: jstring2string (jstr); log ("% s", text. c_str ();} ret = JniHelper: getStaticMethodInfo (info, "org/cocos2dx/cpp/TestJni", "func5", "(II) I "); if (ret) {log ("call int func5 (int a, int B) succeed"); jint iret = info. env-> CallStaticIntMethod (info. classID, info. methodID, 1, 2); log ("return value is % d", iret);} # endif}
In this way, click the close button in HelloWorld to print the above information on the console. Note the four parameters in getStaticMethodInfo. The first parameter is not required. The second parameter is the path of the third function. The specific position is proj. android/src directory path, Cocos2d-x3.1 PATH value: org/cocos2dx/cpp. Remember this step and you will get it later. 4. Create a New java class in ADT (1) Click the project name in ADT, expand the project directory, find src, and click expand src directory to see an AppActivity under the directory. java Class. To demonstrate our functions, right-click New-> Class and enter the Class name, for example, TestJni. A TestJni is added to the src directory. java class. Note that an error should be reported at this time. Next step. (2) The error in step (1) is generally caused by the absence of src under the Import engine. Perform the following operations: Right-click the project name and choose Properties> Java Build Path> Source> Link Source> Browse. At this point, the import Engine Directory such as: cocos2d-x-3.1/cocos/platform/android/java/src, and then casually write a name in the Folder name. Click Finish, OK. The reference of the imported directory is added to the project directory. The error in step (1) disappears. (3) Add the following code to the class created in step (1:
package org.cocos2dx.cpp;import android.R.integer;import android.util.Log;public class TestJni {public static void func1(){Log.e("hello", "world");}public static int func2(){return 12345;}public static String func3(int i){String str = "get int value:" + i;Log.e("yu", str);return str;}public static String func4(String str){Log.e("func4", "str");return str;}public static int func5(int a,int b){int c = a + b;Log.e("func5", "a+b");return c;}}
(4) So far, We can compile it. By the way, in the Xcode project, what is the path of the 2nd parameter in getStaticMethodInfo in the HelloWorld. cpp file. If you keep following me, the path is org/cocos2dx/cpp/TestJni. The third parameter is the static function in TextJni. java. The specific meaning of other parameters is Baidu. Here we mainly talk about the path. 5. Is there another problem with the compilation? If it is the first compilation, it may be that the NDK_ROOT environment is not configured, for solutions, refer to blog posts-> Preferences-> C/C ++-> Build-> Environment, and knock your NDK location and NDK_ROOT in sequence. 6, so far, In the Cocos2d-x project through Jni to call the static function in java should be no problem, if there is a problem, Baidu just fine. Below is my program.


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.