Search code examples
androidandroid-webviewproguard

Android WebView JavaScript callbacks fail in APK without Proguard


I'm exposing @JavascriptInterface callbacks for a WebView which work just fine when debugging the app via Android Studio, however if the app is installed via APK the JavaScript callbacks fail with:

"Uncaught TypeError: NativeApp.onProgress is not a function"

I'm aware that improper Proguard rules can result in this problem, but in this case the project is not using Proguard and the problem occurs with debug and release APKS.

If I inspect the APKs, the methods are present.

public class MyServiceWithEmbeddedWebView {    
    ...

    public createWebview() {
    ...
    webView.addJavascriptInterface(this, "NativeApp");
    ...
    }

    @JavascriptInterface
    void onProgress(int loaded, int total) {
        ...
    }

    ...
}

Any ideas?


Solution

  • Changing the scope of the @JavascriptInterface methods to public solved the problem.

    So this works for an APK install:

    @JavascriptInterface
    public void onProgress(int loaded, int total) {
        // this is public
    }
    

    This does NOT work for an APK install, but works like a champ when deployed by the Android Studio debugger:

    @JavascriptInterface
    void onProgress(int loaded, int total) {
        // this is NOT public
    }
    

    How annoying!