android java and javascript bridge, inspired by wechat webview jsbridge
Switch branches/tags
Nothing to show
Clone or download
lzyzsd Merge pull request #118 from hyy12345678/master
fix example android 5.0+ openFileChooser not invoked problem
Latest commit aca02b4 Oct 29, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
example modify code Apr 16, 2018
gradle/wrapper modify code Apr 16, 2018
library Merge pull request #118 from hyy12345678/master Oct 29, 2018
.gitignore init Feb 26, 2015
JsBridge.gif add demo gif Mar 1, 2015 change com.github.lzyzsd.BridgeWebView to com.github.lzyzsd.jsbridge.… Mar 17, 2016
build.gradle modify code Apr 16, 2018 merge from upstream uptodate Jan 10, 2018
gradlew init Feb 26, 2015
gradlew.bat init Feb 26, 2015
settings.gradle init Feb 26, 2015


inspired and modified from this and wechat jsBridge file, with some bugs fix and feature enhancement.

This project make a bridge between Java and JavaScript.

It provides safe and convenient way to call Java code from js and call js code from java.


JsBridge Demo


I strongly recommend

repositories {
    // ...
    maven { url "" }

dependencies {
    compile 'com.github.lzyzsd:jsbridge:1.0.4'

Use it in Java

add com.github.lzyzsd.jsbridge.BridgeWebView to your layout, it is inherited from WebView.

Register a Java handler function so that js can call

    webView.registerHandler("submitFromWeb", new BridgeHandler() {
        public void handler(String data, CallBackFunction function) {
            Log.i(TAG, "handler = submitFromWeb, data from web = " + data);
            function.onCallBack("submitFromWeb exe, response data from Java");

js can call this Java handler method "submitFromWeb" through:

        , {'param': str1}
        , function(responseData) {
            document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData

You can set a default handler in Java, so that js can send message to Java without assigned handlerName

    webView.setDefaultHandler(new DefaultHandler());
        , function(responseData) {
            document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData

Register a JavaScript handler function so that Java can call

    WebViewJavascriptBridge.registerHandler("functionInJs", function(data, responseCallback) {
        document.getElementById("show").innerHTML = ("data from Java: = " + data);
        var responseData = "Javascript Says Right back aka!";

Java can call this js handler function "functionInJs" through:

    webView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() {
        public void onCallBack(String data) {


You can also define a default handler use init method, so that Java can send message to js without assigned handlerName

for example:

    bridge.init(function(message, responseCallback) {
        console.log('JS got a message', message);
        var data = {
            'Javascript Responds': 'Wee!'
        console.log('JS responding with', data);

will print 'JS got a message hello' and 'JS responding with' in webview console.


This lib will inject a WebViewJavascriptBridge Object to window object. So in your js, before use WebViewJavascriptBridge, you must detect if WebViewJavascriptBridge exist. If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBridgeReady event, as the blow code shows:

    if (window.WebViewJavascriptBridge) {
        //do your work here
    } else {
            , function() {
                //do your work here


This project is licensed under the terms of the MIT license.