Android WebView JavaScript callbacks fail in APK without Proguard

允我心安 提交于 2019-12-25 05:12:31

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/41727497/android-webview-javascript-callbacks-fail-in-apk-without-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!