未调用 WebView 的 JavascriptInterface 方法

JavascriptInterface Method for WebView not called

我有一个很奇怪的问题,我的申请是电子reader。为网络视图调用函数我使用 JavaScript class:

    public class MyJavaScriptInterface {
        Context mContext;

        /* Instantiate the interface and set the context */
        MyJavaScriptInterface(Context c) {
             mContext = c;
        }

        @JavascriptInterface
        public void functionToCall(String input) {
             System.out.println("this function was called: "+ input);
        }
  }

我有一个自定义的 Web 视图,我在其中添加了我的 JavaScript class。

  public class newBTWebView extends WebView implements {
       public newBTWebView(Context context) {
         super(context);
         init(context);

       }
       public void init(Context context) {
         System.out.println("newBTWebview init");
         this.context = context;
         this.getSettings().setJavaScriptEnabled(true);
         setInitialScale(100);
         addJavascriptInterface(new MyJavaScriptInterface(this.context), "HTMLOUT");
     }
 }

在我的主要 activity 中,我启动网络视图并使用 JavaScript:

调用该函数
   public class BookReader extends Activity implements WebViewDelegate{
        private static Context mContext;
        private static newBTWebView testWV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("onCreate");
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        testWV = (newBTWebView) findViewById(R.id.mywebview1);
        testWV.setDelegate(this);
        testWV.setWebChromeClient(new WebChromeClient());
        testWV.getSettings().setJavaScriptEnabled(true);
        testWV.getSettings().setPluginState(PluginState.ON);


        testWV.setWebViewClient(new WebViewClient() {

             @Override 
             public boolean shouldOverrideUrlLoading(WebView view, String url)
             {
                 System.out.println("shouldOverrideUrlLoading: " + url);
             }
             @Override
             public void onPageFinished(WebView view, String url) {
                 System.out.println("onPageFinished");
             }
         }
    }
    public void callMyFunction
    {
         System.out.println("callMyFunction");
        testWV.loadUrl("javascript:window.HTMLOUT.functionToCall('myinput');”);
    }
 }

functionToCall 运行良好,最近在我的最新更新中,因为我在设备上测试应用程序它仍在运行,但是当我在商店提交它时,functionToCall 不再被调用。我在未调用 functionToCall 的设备上测试了 apk 文件。它仅在我 运行 应用程序直接从 pc 到设备时有效,但在 functionToCall 之前提到的同一设备上使用 apk 文件不会被调用。

我已经在三星 S4、三星 Note 10.1 和 nexus note 上进行了测试。使用 android 4.4.2、4.0.4 和 5.0.1。

您是否启用了 proguard 或一些代码混淆?如果是,您应该添加 keep 属性

-keep public class com.MyJavaScriptInterface 
-keep public class * implements  com.MyJavaScriptInterface 
-keepclassmembers class * implements com.MyJavaScriptInterface {
<fields>;
<methods>;
}

我不知道你的 类 全称。所以请输入您的完全限定名称。