Proguard的乱七八糟的Javascript接口功能于Android清单高于17定位SDK时, [英] Proguard mess Javascript Interface functions when targeting SDK in Android Manifest above 17

查看:170
本文介绍了Proguard的乱七八糟的Javascript接口功能于Android清单高于17定位SDK时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Andr​​oid项目自定义web视图,如下所示:

I have a custom Webview in my android project as shown below:

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
    }

   public class JsObject {

        @JavascriptInterface
        public void show() {
            //...
        }

        @JavascriptInterface
        public void hide() {
            //....
        }
}

这包括 JavascriptInterface ,我用从JavaScript的一面传达给Android的一面。

that includes a JavascriptInterface which I use to communicate from the JavaScript side to the Android side.

在AndroidManifest我有以下

In the AndroidManifest I had the following

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

在我使用ProGuard的项目,宣布:

In the project I used proguard which declared:

-keepattributes JavascriptInterface

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

和一切工作正常。

然而,当我改变了我的Andr​​oidManifest为安卓targetSdkVersion = 18 19 和测试设备上有18以上,ProGuard的似乎莫名其妙地乱而不能访问了JavaScript的方法。

However when i changed my AndroidManifest to android:targetSdkVersion=18 or 19 and test on devices with 18 and above, proguard seems to somehow mess the JavaScript methods which are not accessible anymore.

如果我放回到16或任何小于17一切工作正常。此外这种情况只使用ProGuard。如果我不使用ProGuard的一切工作正常,即使安卓?targetSdkVersion = 18 或19。谁能帮助,使其在清单中的Andr​​oid> 17时,针对工作

If I put back to 16 or anything less than 17 everything works fine. Additionally this happens only with proguard. If I don't use proguard everything works fine even with android:targetSdkVersion=18 or 19. Can anyone help to make it work when targeting in the manifest Android > 17?

推荐答案

我复制我的回答从这个话题您:<一href="http://stackoverflow.com/a/19994873/1735499">http://stackoverflow.com/a/19994873/1735499

I copy my answer from this topic for you: http://stackoverflow.com/a/19994873/1735499

而且,如果你正在使用Proguard的,记得要加这个

And, if you are using Proguard, remember to add this

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

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

如果它仍然不能确定,添加此

If it's still not OK, add this

-keepattributes *Annotation*

请注意:你的MyJavaScriptInterface必须是公共类

Note: your MyJavaScriptInterface must be Public class

参考号码:<一href="http://stackoverflow.com/questions/6271485/android-proguard-javascript-interface-fail?rq=1">Android Proguard的Javascript的接口故障

BR,

弗兰克

这篇关于Proguard的乱七八糟的Javascript接口功能于Android清单高于17定位SDK时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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