1

I am trying to integrate an android library (.aar) into cocos2D Android release.

Though I can import the android library, I am not able to call the class function in the library.

This is how I call the Android function from the cocos2D javascript handler.

jsb.reflection.callStaticMethod('org/cocos2dx/javascript/myDialog', 'showAlertDialog', '(Ljava/lang/String;Ljava/lang/String;)V', 'MyAlert', 'This is my first Alert from cocos2d' )

Android library class

package org.cocos2dx.javascript;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

public class myDialog extends Activity {

    private static myDialog cam = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cam = this;
    }

    public static void showAlertDialog(final String title,final String message) {
        Log.i(title, message);
        //we must use runOnUiThread here
        cam.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog alertDialog = new AlertDialog.Builder(cam).create();
                alertDialog.setTitle(title);
                alertDialog.setMessage(message);
                alertDialog.show();

            }
        });
    }
}

below is the console warning I get

2020-07-01 16:47:46.514 26242-26339/org.cocos2d.demo E/JavaScriptJavaBridge: [ERROR] (C:/start_project/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptJavaBridge.cpp, 598): call valid: 0, call.getArgumentsCount()= 2

2020-07-01 16:47:46.514 26242-26339/org.cocos2d.demo E/JavaScriptJavaBridge: [ERROR] Failed to invoke JavaScriptJavaBridge_callStaticMethod, location: C:/start_project/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptJavaBridge.cpp:604

I have been working on this for 3 days now. But could not find any solution. Any kind of help will be fairly appreciated

The documentation for the same Calling java function from cocos2d The documentation is fairly simple and has been followed likewise with no success.

Kuldeep Bhimte
  • 961
  • 1
  • 10
  • 25
  • Doesn't seem to be – Kuldeep Bhimte Jul 01 '20 at 09:25
  • @Rookie are there any other particular configuration I have to follow. – Kuldeep Bhimte Jul 01 '20 at 09:29
  • 1
    They way you are calling the method is how it is called. But now I think you have a different main activity than myDialog. If this is true, this is wrong way to call the method. You have to keep the methods you are calling from cocos into that main activity which was initially provided. – Rookie Jul 01 '20 at 10:02
  • Yes, the myDialog is not the main activity. Its android library(.aar) build from another android project. I imported it as a dependency in my cocoa android project – Kuldeep Bhimte Jul 01 '20 at 10:54

0 Answers0