简体   繁体   中英

JavascriptInterface not working on Android 5

I am using a WebView like in https://github.com/evgenyneu/js-evaluator-for-android to execute javascript in a WebView and return the result to java with a @JavascriptInterface annotated method.

I do this in a library project that is obfuscated with dexguard.

This is working fine on android 7, 6 and 5. I am also using the library in an android app, it works except when using Proguard, it stops working on Android 5, but still works on android 6.

I target SDK 24, the minimum is 8 but the code is only used on 21 and up. The @JavascriptInterface annotation and all the classes are exempt from obfuscation, but still, this does not work on Android 5 and 6. It does work on 7.

Any idea what I am doing wrong?

After even more testing, I can conclude that it starts working on release when I make my release buildtype debuggable:

release {
        minifyEnabled true
        debuggable true
}

The problem was that I was doing this in a service, and some webview implementations check if you are running on the main looper.

A solution for me was executing it on the main looper:

this.handler = new Handler(Looper.getMainLooper());
this.handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            // create a webview
            final WebView webView = new WebView(context);

            // etc
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM