[Cocos3.0 Tutorial] Call Java functions on Cocos2d-x

[Cocos3.0 Tutorial] Call Java functions on Cocos2d-x v3.0 Final

Get files

  1. Download http://cdn.cocos2d-x.org/cocos2d-x-3.0.zip
  2. Unzip it
  3. copy cocos2d-x3.0 to your workspace

Create project

Prepare environment

  1. Open a terminal on cocos2d-x folder
  2. Run “Python setup.py” to make sure all environment variables are properly defined
  3. Now we will create our project with command “cocos new ProjectName -p project.package -l language -p PROJECT_DIRECTORY”
    cocos new JniTest -p com.test.jnitest -l cpp -d /Users/javi_moralesf/Documents/workspace/tutorials/

Import projects on Eclipse

Import “inappTest\proj.android” :

  1. Open eclipse
  2. Eclipse → Create a new Workplace
  3. File → New → Project → Android → Android Project from Exiting Code → Next

  4. Root Directory → Browse “PROJECT_LOCATION\JniTest\proj.android” → Finish
  5. Project → Properties → Android → Project build target :
  6. This tutorial was made on 4.0 (API 14)
  7. Apply → Ok.

Import “libcocos2dx” :

  1. Go to eclipse

  2. File → Import → Android → Exiting Android code into Workplace → Next


  3. Root Directory → Browse “PROJECT_LOCATION\JniTest\cocos2d\cocos” → Finish

Build project

Compile c++ files

  1. Open terminal on “PROJECT_LOCATION\JniTest\proj.android”
  2. Run “Python build_native.py”.


  3. If you have some error, check your environment variables again.

Build java project

  1. Go to eclipse
  2. Connect an Android device (adb enabled).
    Run → Run → Android Application → Choose a running Android Device → OK
    Auto Monitor logcat → yes, Monitor logcat and display … → Ok (will use this later for device hash)


    Make sure “Hello World” showing and working correctly.

Edit code

Java

Now we will add a function on AppActivity to be called from C++ code.

public static Activity _activiy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    //Save activity instance
	    _activiy = this;
    }

    public static void alertJNI() {
	    String tag = "JniTest";
	    String message = "I've been called from C++";
        Log.d(tag, "Showing alert dialog: " + message);
    }

C++

  1. First we need to include JniHelper and jni headers

    #include "platform/android/jni/JniHelper.h"
    #include <jni.h>

  1. And finally we will call our java function from C++ code

     void HelloWorld::menuCloseCallback(Ref* pSender)
     {
         cocos2d::JniMethodInfo methodInfo;
     
         if (! cocos2d::JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "alertJNI", "()V")) {
             return;
         }
     
         methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
         methodInfo.env->DeleteLocalRef(methodInfo.classID);
     }
    

Then we do again “Python build_native.py” and build android application:

Now when we press the off button, the java function alertJNI is called:

@javi_moralesf

thanks, great job:)

The pictures somehow do not show up. I can’t see the detail. Would you mind check this problem…

Thanks a ton, this is really helpful :slight_smile:

@javi_moralesf

Can you update your pictures url? I cannot see them :frowning: