我已经在我的Android项目自定义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>;
}
和一切工作正常。
然而,当我改变了我的AndroidManifest为安卓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。谁能帮助,使其在清单中的Android> 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?
推荐答案
我复制我的回答从这个话题您: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
参考号码:Android Proguard的Javascript的接口故障
BR,
弗兰克
相关推荐
最新文章