Skip to content

Samples of communication to Java from C++ via JNI. Using Cocos2d-x v3.2 and newest (non-deprecated) approaches.

Notifications You must be signed in to change notification settings

ElliotMebane/Cocos2d-x_CPP_to_Java_via_JNI_Samples

Repository files navigation

Cocos2d-x_CPP_to_Java_via_JNI_Samples

Samples of communication to Java from C++ via JNI. Using Cocos2d-x v3.2 and newer (non-deprecated) approaches.

Content Notes

This sample demonstrates the use of JNI for C++ to Java communication with Cocos2d-x v3.2. The foundation of these samples are based on these posts:

It was a challenge to get C++ to Java communication working with the latest version of Cocos2d-x because Cocos2d-x v.3.0 introduced significant changes and as a result a lot of the tutorials I found on this topic were outdated or incomplete. I prefer complete working samples over tutorials when I'm learning new code concepts. I hope this working sample helps make things a lot easier for you than they were for me as I tried to figure this stuff out! This sample has been compiled and tested but please notify me if you discover any problems.

JNI communication
Buttons that call methods in Java (and often launch native Java dialogs).

  • Calling a Java method from C++
  • Passing an int from C++ to Java
  • Passing an int from C++ to Java method and receiving a String return
  • Passing a String and an int from C++ to Java method and receiving a String return
  • Calling a Java method from C++ to detect Latitude/Longitude, receiving a Java Double Array return, converting it to a C++ vector.
  • Calling a Java method to launch a URL in the native device web browser. Implementation based on solution here:
    http://discuss.cocos2d-x.org/t/how-to-open-url/8440/5

Java Dialog
Resources that helped construct a solution to launch native Java dialogs:

Menu

Dev/Test environment

  • Cocos2d-x 3.2
  • Mac/OSX 10.9.4
  • Eclipse Luna 4.4.0
  • Google Nexus 4 phone
  • Android 4.4.2
  • (XCode6.0 Beta)*
    * Tested in XCode iPhone simulator to verify cross-platform compilation. Android-exclusive conditionals are verified. When publishing on iOS no dialog launcher menu nor dialogs are displayed.
I haven't made a sample of communication from Java to C++, but I may apply the info from Nguyen's second post and make a sample.
http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html

Elliot Mebane
http://www.roguish.com/blog

About

Samples of communication to Java from C++ via JNI. Using Cocos2d-x v3.2 and newest (non-deprecated) approaches.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published