How to execute JavaScript on Android?

AndroidJSCore is a great one. And here is another little library I wrote for evaluating JavaScript:

https://github.com/evgenyneu/js-evaluator-for-android

jsEvaluator.evaluate("function hello(){ return 'Hello world!'; } hello();", new JsCallback() {
  @Override
  public void onResult(final String result) {
    // get result here (optional)
  }
});

It creates a WebView behind the scenes. Works on Android version 3 and newer.


You can use Webview which inherits View class. Make an XML tag and use findViewById() function to use in the activity. But to use the JavaScript, you can make a HTML file containing the JavaScript code. The example blelow might help.

Webview browser=(Webview) findViewById(R.main.browser); //if you gave the id as browser
browser.getSettings().setJavaScriptEnabled(true); //Yes you have to do it
browser.loadUrl("file:///android_asset/JsPage.html"); //If you put the HTML file in asset folder of android

Remember that the JS will run on WebView, not in native environment, thus you might experience a lag or slow FPS in emulator. However when using on an actual phone, the code may run fast, depending on how fast is your phone.