Developers Manual > Cocos2d-x > How To > User Contributed Tutorials > User Tutorial-Call Java functions in Cocos2d-x

Call Java functions in Cocos2d-x

Contributed By: javi_moralesf

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 Imgur
  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/ Imgur

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 Imgur Imgur
  4. Root Directory -> Browse “PROJECT_LOCATION\JniTest\proj.android” -> Finish Imgur
  5. Project -> Properties -> Android -> Project build target :
  6. This tutorial was made on 4.0 (API 14)
  7. Apply -> Ok. Imgur

Import "libcocos2dx" :

  1. Go to eclipse
  2. File -> Import -> Android -> Exiting Android code into Workplace -> Next
    Imgur
    Imgur

  3. Root Directory -> Browse “PROJECT_LOCATION\JniTest\cocos2d\cocos” -> Finish
    Imgur

Build project

Compile c++ files

  1. Open terminal on “PROJECT_LOCATION\JniTest\proj.android”
  2. Run “Python build_native.py”. Imgur Imgur Imgur
  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) Imgur Imgur 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);
    }

Imgur

C++

  1. First we need to include JniHelper and jni headers

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

    Imgur

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
        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);
        }
    

    Imgur

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:

Imgur

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.