Call custom JS code from java with Cocos2dxJavascriptJavaBridge



I’m working with Cocos Creator. In order to call a JS function from java, I have tried this:

Java code:

Cocos2dxJavascriptJavaBridge.evalString(“onTwitterLogin(” + accessToken.getToken() + ", " + accessToken.getTokenSecret() + ", " + accessToken.getUserId() + “)”);

JS code:
function onTwitterLogin(token, tokenSecret, userId)
// …

but the logcat shows:

cocos js error:: ScriptingCore::evalString line:1 msg:ReferenceError: onTwitterLogin is not defined
cocos2d-x: ScriptingCore::callFunctionName error:__errorHandler wasn’t found!

I don’t know what is wrong in this code. Any help?

Thanks in advance


Please, can help me? I really need a solution. I have found a similar post written one year ago… I am not sure if this functionality is working properly in cocos2dx:



I have get the solution. I have to call first require(script) in the string to eval, then I call the methos as:

		Cocos2dxJavascriptJavaBridge.evalString("var LoginScene = require('LoginScene'); LoginScene.prototype.onTwitterLogin(\"" + accessToken.getToken() + "\", \"" + accessToken.getTokenSecret() + "\", \"" + accessToken.getUserId() + "\")");


Can you tell this ‘require(“LoginScene”);’ is your or LoginScene.js or something else? I am stuck here, any help will be highly appreciated. Thank You.


The require sentence uses the java script file


Thanks so much !


This helped me a lot… Thanks