没有Proguard的Android WebView JavaScript回调在APK中失败 [英] Android WebView JavaScript callbacks fail in APK without Proguard

查看:145
本文介绍了没有Proguard的Android WebView JavaScript回调在APK中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公开了一个WebView的@JavascriptInterface回调,当通过Android Studio调试应用时,该回调工作得很好,但是,如果通过APK安装该应用,则JavaScript回调将失败,

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:

未捕获的TypeError:NativeApp.onProgress不是函数"

我知道不正确的Proguard规则可能会导致此问题,但是在这种情况下,该项目未使用Proguard,并且问题发生在调试和发布APKS上.

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.

如果我检查APK,就会发现方法.

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) {
        ...
    }

    ...
}

有什么想法吗?

推荐答案

@JavascriptInterface方法的范围更改为public解决了该问题.

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

因此这适用于APK安装:

So this works for an APK install:

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

这不适用于APK安装,但在由Android Studio调试器部署后,其工作原理就像冠军:

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
}

多么烦人!

这篇关于没有Proguard的Android WebView JavaScript回调在APK中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆