简体   繁体   中英

Javascript interface not working in release build

I have a Webview with some Javascript interface

public class WebAppInterface {
    @JavascriptInterface
    void buttonClick() {
        listener.onButtonClicked();
    }
}

This is how it's added to the view

webView.addJavascriptInterface(new WebAppInterface(), "Android");

In debug build the listener is working.

In release build, made by Jenkins, it doesn't work.

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "terminal_${variant.versionName}.apk"
                }
            }
        }
    }

It shouldn't be related to Proguard, because minify is disabled.

I tried different Proguard settings anyway, it didn't helped.

How to make it work in release build?

Making the interface method public resolved the issue

public void buttonClick() {
        listener.onButtonClicked();
    }

If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). https://developer.android.com/guide/webapps/webview#BindingJavaScript

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