android - addJavascriptInterface function doesn't work on API 21 -


i'm trying add javascript interface webview i've found. followed standard tutorial , still getting struggled task. when adding javascript interface, don't exceptions or errors, when explicitly calling bridge js, following error:

i/chromium: [info:console(1)] "uncaught referenceerror: jsnativebridge not defined", source:  (1) 

adding javascript interface:

new handler(context.getmainlooper()).post(new runnable() {     @override     public void run() {         webview webview = webviews[0];         if (constants.debug_mode) {             webview.setwebchromeclient(new webchromeclient());             webview.getsettings().setjavascriptenabled(true);         }         impressionsyncjsinterface impressionsyncjsinterface = new impressionsyncjsinterface(context);         webview.addjavascriptinterface(impressionsyncjsinterface, js_bridge_name);         didaddinterfacetowebview = true;     } }); 

my interface: public class impressionsyncjsinterface { private final context context;

    public impressionsyncjsinterface(context context) {         this.context = context;     }      @javascriptinterface     public void foo() {         log.e("test", "test");     } } 

the javascript execution:

final string javascriptinjectiontest = "javascript: " + js_bridge_name + ".foo();";  new handler(looper.getmainlooper()).post(new runnable() {     @override     public void run() {         webview.loadurl(javascriptinjectiontest);     } }); 

any appreciated.

figured out problem, i'll share insights:

the addjavascriptinterface function applies if called before loadurl / loaddata function.

in case - expected addjavascriptinterface inject js bridge, never reloaded webview content, never actively injected.

after reloading webview html content, bridge added expected.


Comments