How to create JsbBridgeWrapper for JsbBridge ? (Calling Java methods from Javascript)

Hello Everyone !
So i’ve implemented admob ads in my project but i want to call the java functions from js
i saw that cocos creator has an api “JsbBridgeWrapper” to use for handling events to native.
but the problem in the following link :
https://docs.cocos.com/creator/manual/en/advanced-topics/jsb-bridge-wrapper.html
the code for the JsbBridgeWrapper is not complete
and in the link : cocos-example-projects/native-script-bridge at v3.7 · cocos/cocos-example-projects · GitHub
when i try to copy the JsbBridgeTest class to my project build it shows that
“import com.cocos.lib.JsbBridgeWrapper” doesn’t exist
I hope Anyone can help me.
my game is already released but i want to add ads to it.
Thanks

…up up up up

Hey there,

Which version of CC are you using?

From the engine codes, I found some references already using it.
Example: here

import com.cocos.lib.JsbBridgeWrapper;
...
...
JsbBridgeWrapper.getInstance().addScriptEventListener()...

So, by right, import should be working.
Can you share your full Java codes?

1 Like

I’m using cocos creator v2.4.9
This is my AppActivity.java file :

/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.os.Bundle;

import android.content.Intent;
import android.content.res.Configuration;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;

import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;

import androidx.annotation.NonNull;

public class AppActivity extends Cocos2dxActivity {

    private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5354046379";
    private static final String TAG = "MainActivity";

    private boolean gameOver;
    private boolean gamePaused;

    private RewardedInterstitialAd rewardedInterstitialAd;
    private Button startButton;

    private boolean isLoadingAds;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Workaround in
        // https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            // Android launched another instance of the root activity into an existing task
            // so just quietly finish and go away, dropping the user back into the activity
            // at the top of the stack (ie: the last state of this task)
            // Don't need to finish it again since it's finished in super.onCreate .
            return;
        }
        // DO OTHER INITIALIZATION BELOW
        SDKWrapper.getInstance().init(this);
        Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion());
        MobileAds.initialize(
                this,
                new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {
                        loadRewardedInterstitialAd();
                    }
                });
        Toast.makeText(AppActivity.this, "Toast from Java ! ", Toast.LENGTH_SHORT).show();

//        JsbBridge.setCallback(new JsbBridge.ICallback() {
//            @Override
//            public void onScript(String arg0, String arg1) {
//                //TO DO
//                if(arg0.equals("open_ad")){
//                    //call openAd method.
//                }
//            }
//        });
//        startButton = findViewById(R.id.start_button);
//        startButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (rewardedInterstitialAd != null) {
//                    showRewardedVideo();
//                }
//            }
//        });

    }
    public void startAd(){
        Toast.makeText(AppActivity.this, "start Add Method Called ", Toast.LENGTH_SHORT).show();

        if (rewardedInterstitialAd != null) {
            showRewardedVideo();
        }
    }
    @Override
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
        SDKWrapper.getInstance().setGLSurfaceView(glSurfaceView, this);

        return glSurfaceView;
    }

    @Override
    protected void onResume() {
        super.onResume();
        SDKWrapper.getInstance().onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        SDKWrapper.getInstance().onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            return;
        }

        SDKWrapper.getInstance().onDestroy();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        SDKWrapper.getInstance().onNewIntent(intent);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SDKWrapper.getInstance().onRestart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKWrapper.getInstance().onStop();
    }

    @Override
    public void onBackPressed() {
        SDKWrapper.getInstance().onBackPressed();
        super.onBackPressed();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        SDKWrapper.getInstance().onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        SDKWrapper.getInstance().onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        SDKWrapper.getInstance().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        if (rewardedInterstitialAd != null && !isLoadingAds) {
            loadRewardedInterstitialAd();
        }
        SDKWrapper.getInstance().onStart();
        super.onStart();
    }
    private void loadRewardedInterstitialAd() {
        if (rewardedInterstitialAd == null) {
            isLoadingAds = true;

            AdRequest adRequest = new AdRequest.Builder().build();
            // Use the test ad unit ID to load an ad.
            RewardedInterstitialAd.load(
                    AppActivity.this,
                    AD_UNIT_ID,
                    adRequest,
                    new RewardedInterstitialAdLoadCallback() {
                        @Override
                        public void onAdLoaded(RewardedInterstitialAd ad) {
                            Log.d(TAG, "onAdLoaded");

                            rewardedInterstitialAd = ad;
                            isLoadingAds = false;
                            Toast.makeText(AppActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onAdFailedToLoad(LoadAdError loadAdError) {
                            Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage());

                            // Handle the error.
                            rewardedInterstitialAd = null;
                            isLoadingAds = false;
                            Toast.makeText(AppActivity.this, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }
    private void showRewardedVideo() {
        if (rewardedInterstitialAd == null) {
            Log.d(TAG, "The rewarded interstitial ad wasn't ready yet.");
            return;
        }

        rewardedInterstitialAd.setFullScreenContentCallback(
                new FullScreenContentCallback() {
                    /** Called when ad showed the full screen content. */
                    @Override
                    public void onAdShowedFullScreenContent() {
                        Log.d(TAG, "onAdShowedFullScreenContent");

                        Toast.makeText(AppActivity.this, "onAdShowedFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                    }

                    /** Called when the ad failed to show full screen content. */
                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                        Log.d(TAG, "onAdFailedToShowFullScreenContent: " + adError.getMessage());

                        Toast.makeText(
                                        AppActivity.this, "onAdFailedToShowFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                    }

                    /** Called when the ad dismissed the full screen content. */
                    @Override
                    public void onAdDismissedFullScreenContent() {
                        Log.d(TAG, "onAdDismissedFullScreenContent");

                        Toast.makeText(AppActivity.this, "onAdDismissedFullScreenContent", Toast.LENGTH_SHORT)
                                .show();
                        rewardedInterstitialAd = null;
                        loadRewardedInterstitialAd();
                    }
                });

        rewardedInterstitialAd.show(
                this,
                new OnUserEarnedRewardListener() {
                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                        Log.d(TAG, "onUserEarnedReward");

                        Toast.makeText(AppActivity.this, "onUserEarnedReward", Toast.LENGTH_SHORT).show();
                    }
                });
    }

}

when i try to add import com.cocos.lib.JsbBridgeWrapper; i get this error : Cannot resolve symbol 'cocos'
and if i change it to import org.cocos2dx.lib.JsbBridgeWrapper;
i get : Cannot resolve symbol ‘JsbBridgeWrapper’

i tried copying this file to my project engine library

/****************************************************************************
 Copyright (c) 2018-2021 Xiamen Yaji Software Co., Ltd.

 http://www.cocos.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated engine source code (the "Software"), a limited,
 worldwide, royalty-free, non-assignable, revocable and non-exclusive license
 to use Cocos Creator solely to develop games on your target platforms. You shall
 not use Cocos Creator software for developing other software or tools that's
 used for developing games. You are not granted to publish, distribute,
 sublicense, and/or sell copies of Cocos Creator.

 The software or tools in this License Agreement are licensed, not sold.
 Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
package org.cocos2dx.lib;

import java.util.ArrayList;
import java.util.HashMap;

public class JsbBridgeWrapper {
    //Interface for listener, should be implemented and dispatched
    public interface OnScriptEventListener {
        void onScriptEvent(String arg);
    }
    /**
     * Get the instance of JsbBridgetWrapper
     */
    public static JsbBridgeWrapper getInstance() {
        if (instance == null) {
            instance = new JsbBridgeWrapper();
        }
        return instance;
    }
    /**
     * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored
     */
    public void addScriptEventListener(String eventName, OnScriptEventListener listener) {
        if (eventMap.get(eventName) == null) {
            eventMap.put(eventName, new ArrayList<OnScriptEventListener>());
        }
        eventMap.get(eventName).add(listener);
    }
    /**
     * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist
     */
    public boolean removeScriptEventListener(String eventName, OnScriptEventListener listener) {
        ArrayList<OnScriptEventListener> arr = eventMap.get(eventName);
        if (arr == null) {
            return false;
        }
        arr.remove(listener);
        return true;
    }
    /**
     * Remove all listener for event specified.
     */
    public void removeAllListenersForEvent(String eventName) {
        this.eventMap.remove(eventName);
    }
    /**
     * Remove all event registered. Use it carefully!
     */
    public void removeAllListeners() {
        this.eventMap.clear();
    }
    /**
     * Dispatch the event with argument, the event should be registered in javascript, or other script language in future.
     */
    public void dispatchEventToScript(String eventName, String arg) {
        JsbBridge.sendToScript(eventName, arg);
    }
    /**
     * Dispatch the event which is registered in javascript, or other script language in future.
     */
    public void dispatchEventToScript(String eventName) {
        JsbBridge.sendToScript(eventName);
    }

    private JsbBridgeWrapper() {
        JsbBridge.setCallback(new JsbBridge.ICallback() {
            @Override
            public void onScript(String arg0, String arg1) {
                triggerEvents(arg0, arg1);
            }
        });
    }

    private final HashMap<String, ArrayList<OnScriptEventListener>> eventMap = new HashMap<>();
    private static JsbBridgeWrapper instance;

    private void triggerEvents(String eventName, String arg) {
        ArrayList<OnScriptEventListener> arr = eventMap.get(eventName);
        if (arr == null)
            return;
        for (OnScriptEventListener m : arr) {
            m.onScriptEvent(arg);
        }
    }
}

but i get Cannot resolve symbol ‘JsbBridge’ in the jsbbridgeWrapper file
I created This class `package org.cocos2dx.javascript;
//Script to add into com.cocos.game package, backup here
import org.cocos2dx.lib.JsbBridgeWrapper;

public class JsbBridgeTest {
public static void start(){
//Original method
JsbBridgeWrapper jbw = JsbBridgeWrapper.getInstance();
jbw.addScriptEventListener(“requestLabelContent”, arg ->{
System.out.print("@JAVA: here is the argument transport in" + arg);
jbw.dispatchEventToScript(“changeLabelContent”,“Charlotte”);
});
jbw.addScriptEventListener(“requestLabelColor”, arg ->{
System.out.print("@JAVA: here is the argument transport in" + arg);
jbw.dispatchEventToScript(“changeLabelColor”);
});
jbw.addScriptEventListener(“requestBtnColor”, arg ->{
System.out.print("@JAVA: here is the argument transport in" + arg);
jbw.dispatchEventToScript(“changeLightColor”);
});

    //Only use JavaEventHandler
    jbw.addScriptEventListener("removeJSCallback", arg->{
        jbw.removeAllListenersForEvent("requestBtnColor");
    });

}

}`
in the appActivity class i get Cannot resolve symbol ‘JsbBridge’

Can you try with 3.x.x?

You can use java reflection: JavaScript to Java Reflection · Cocos Creator

1 Like

it worked with jsb reflection … the problem is that i can’t call the ad method because it’s not static … i will post another topic about that