【问题标题】:Android JavascriptInterface not working with minifyEnabledAndroid JavascriptInterface 不适用于 minifyEnabled
【发布时间】:2016-06-15 09:47:33
【问题描述】:

提前致谢。

我花了太多时间阅读有关此的 stackoverflow。 我有一个带有 webview 和 jquery web 的混合应用程序。我有我的 javascript 界面。

当我在调试模式下运行我的应用程序时,每个想法都可以正常工作,但是当我在发布模式下运行时,javascript 界面根本不起作用。

我尝试了太多解决方案,例如 proguard 规则:

-keep public class com.mypackage.MyClass$MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface { 
   <methods>; 
}
-keepattributes JavascriptInterface

如你所见,我有我的符号@JavascriptInterface:

public static class WebAppInterface {
    Context mContext;

WebAppInterface(Context c) {
    mContext = c;
}

@JavascriptInterface
    public void estoyenchat() { enchat=true;
}
....

}

在我的 gradle 文件中:

minSdkVersion 14
targetSdkVersion 22

我发现在发布模式下运行应用程序的唯一解决方案是将 minifyEnabled 设置为 false。

有人知道原因吗?

【问题讨论】:

    标签: javascript android interface proguard


    【解决方案1】:

    保留任何具有@JavascriptInterface 注释的方法的正确方法是:

    -dontnote android.webkit.JavascriptInterface
    -keepclassmembers class * {
        @android.webkit.JavascriptInterface <methods>;
    }
    

    【讨论】:

      【解决方案2】:

      创建一个名为 JsInterface 的接口

      public interface JsInterface {}
      

      在你的 js 接口类中实现它。

      public class WebAppInterface implements JsInterface {
      
      Context mContext;
      
      WebAppInterface(Context c) {
          mContext = c;
      }
      
      @JavascriptInterface 
      public void estoyenchat() { enchat=true;
      }
      

      添加到proguard:

      -keepclassmembers,allowobfuscation,allowoptimization interface * extends com.mysite.JsInterface{*;}
      

      【讨论】:

      • 谢谢我也试过了......在我的proguard文件中我也有这个:-dontwarn -keepattributes Annotation -keepattributes JavascriptInterface
      猜你喜欢
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2016-12-06
      • 2012-01-27
      • 2018-11-13
      • 2017-11-02
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多