Samples of communication to Java from C++ via JNI. Using Cocos2d-x v3.2 and newer (non-deprecated) approaches.
Content Notes
- I omitted the cocos2d folder from this source because it's 200+Mb. Add it from your owm copy of Cocos2d-x.
- I've included a compiled APK file so you can load it on your device and preview it without having to compile the app.
https://github.com/ElliotMebane/Cocos2d-x_CPP_to_Java_via_JNI_Samples/blob/master/proj.android/bin/JNIDemos.apk - The Java file is here:
https://github.com/ElliotMebane/Cocos2d-x_CPP_to_Java_via_JNI_Samples/blob/master/proj.android/src/com/roguish/MyAwesomeJavaClass.java
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:
- Post by Stefan Nguyen, JNI basics: http://stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html
- Post by Simon Pan, Open URL: http://discuss.cocos2d-x.org/t/how-to-open-url/8440/5
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:
- http://stackoverflow.com/questions/13377300/how-to-show-dialog-from-a-static-method
- http://stackoverflow.com/questions/12051973/how-to-display-custom-dialog-in-cocos2dxactivity
- http://stackoverflow.com/questions/4336470/how-do-i-close-an-android-alertdialog
- (Formatting Java method signatures. i.e., when to use semicolons) http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html#appendixa
Menu
- I used simple MenuItem text-label buttons with callbacks as described in the 3.0 release notes:
https://github.com/cocos2d/cocos2d-x/blob/cocos2d-x-3.0/docs/RELEASE_NOTES.md. - Similar samples are documented here:
https://github.com/cocos2d/cocos2d-x/blob/v3/tests/cpp-tests/Classes/MenuTest/MenuTest.cpp. - I avoided methods that have been deprecated. I did not use the newest GUI options like Button, which would have looked better. They're documented here if you want them:
http://www.cocos2d-x.org/wiki/Cocos2D-X_New_GUI.
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.
http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html
Elliot Mebane
http://www.roguish.com/blog