A Simpler Way to Call Java Methods with JavaScript (Experimental)

Note: After v3.6.0, jsb module is about to be deprecated, APIs will be moved to the native module of namespace cc.

Background

Prior to v3.4.0, the reflection mechanism in Using JavaScript to Call Java static methods, not only needed to strictly declare package names and function signatures, but also needed to strictly check the number of parameters to ensure proper operation, which was a complicated step.

Additionally provided in v3.4.0 is another experimental method for simplifying calls from the scripting layer to the native layer. This is a kind of channel, or a bridge, named JsbBridge before introducing other scripting systems, meaning that it serves as a bridge to communicate between script and native APP via the JSB binding.

Note: both ways are working fine, developers can choose to use them according to their actual needs. To use the previous way, please review the Using JavaScript to Call Java documentation.

JavaScript Interface Introduction

The only two interfaces at the scripting level are sendToNative and onNative, which are transfer and receive native layer parameters, respectively. The following points need to be noted when using them:

  • This feature is still in the experimental stage, only string transfers are supported. To transfer objects containing multiple parameters, please consider converting them to the JSON format for transfer and parsing them at different levels.
  • onNative will only record one function at a time, and will override the original onNative method when the property is set again.
  • The sendToScript method is a one-way communication and does not care about the return of the lower level, nor does it tell JavaScript whether the operation succeeded or failed. The developer needs to handle the operation itself.
  1. // JavaScript
  2. export namespace bridge{
  3. /**
  4. * Send to native with at least one argument.
  5. */
  6. export function sendToNative(arg0: string, arg1?: string): void;
  7. /**
  8. * Save your own callback controller with a JavaScript function,
  9. * Use 'jsb.bridge.onNative = (arg0: String, arg1: String | null)=>{...}'
  10. * @param args : received from native
  11. */
  12. export function onNative(arg0: string, arg1?: string | null): void;
  13. }

Java Interface Introduction

The corresponding JAVA interfaces are also dominated by two, including sendToScript and onScript:

  • sendToScript corresponds to sendToNative and represents the parameters to be transferred to JavaScript.
  • onScript corresponds to onNative, which indicates the response behavior after receiving a script message. Wrap the behavior by creating an interface called ICallback and use setCallback to enable the interface function.
  1. // JAVA
  2. public class JsbBridge {
  3. public interface ICallback{
  4. /**
  5. * Applies this callback to the given argument.
  6. *
  7. * @param arg0 as input
  8. * @param arg1 as input
  9. */
  10. void onScript(String arg0, String arg1);
  11. }
  12. /* Add a callback which you would like to apply
  13. * @param f ICallback, the method which will be actually applied. multiple calls will override
  14. * multiple calls will override */
  15. public static void setCallback(ICallback f);
  16. /*
  17. * Java dispatch Js event, use native c++ code
  18. * @param arg0 input values
  19. */
  20. public static void sendToScript(String arg0, String arg1);
  21. public static void sendToScript(String arg0);
  22. }

Basic Usage

Using JavaScript to Trigger Java Methods

Assuming the ad interface is set in the native layer, then when the player clicks the button to open the ad, it is logical to trigger JAVA to open the ad.

The code example of the interface to open the ad is as follows:

  1. public void openAd(String adUrl){
  2. // Code to open ad
  3. }

Register the event that opens the ad first:

  1. JsbBridge.setCallback(new JsbBridge.ICallback() {
  2. @Override
  3. public void onScript(String usrName, String url) {
  4. // Check usr
  5. // Open Ad
  6. openAd(url);
  7. }
  8. });

Perform the open action on the button’s click event in JavaScript:

  1. public static onclick(){
  2. // 'usrName' and 'defaultAdUrl' are both string
  3. native.bridge.sendToNative(usrName, defaultAdUrl);
  4. }

This will send the required information to the Java layer through the native.Bridge channel.

Using JAVA to Trigger JavaScript Methods

Assuming that the animation playback operation is recorded in JavaScript. To play this animation in the Java layer, register an event to play the animation.

First, define a function to play the animation:

  1. public void playAnimation(animationName: string, isLoop: boolean){
  2. // Code to play Animation
  3. }

Second, document the method in onNative:

  1. import { native } from 'cc'
  2. native.bridge.onNative = (animationName: string, isLoop: String | null):void=>{
  3. if(isLoop && isLoop == "true") {
  4. this.playAnimation(animationName, true);
  5. return;
  6. }
  7. this.playAnimation(animationName, false);
  8. return;
  9. }

Still using the Android project as an example, the Java code example is as follows:

  1. JsbBridge.sendToScript("SkeletonAnim001", "true");

This will call the JavaScript playback operation.

Sample project: simple multi-event calls

Creator provides the native-script-bridge example, which developers can download for reference use as needed.