0

In my app's webview some contents are derived from a class object. From WebView's document here:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.

What if I have a class and I would like to make all methods of this class accessible by my WebView? Do I need to add @JavascriptInterface to every single method? Is there another way to achieve this efficiently?

On the other hand, I am not familiar with ProGuard, I've read some posts here as well as some other stuffs (e.g. usage page for proguard) about ProGuard. I am confused about it and its usage. Do I have to configure ProGuard on this issue?

Currently I seem to be able to access methods with @JavascriptInterface without configuring them in ProGuard. However I saw many post regarding ProGuard and JavascriptInterface (like post 1 and post 2) are using the keep options with those classes, if I didn't get it wrong is this because those methods/classes are only used by the javascript in WebView, hence marking them "never used" and will be removed by ProGuard?

Thanks!

Community
  • 1
  • 1
peakingcube
  • 1,388
  • 15
  • 32

1 Answers1

0

What if I have a class and I would like to make all methods of this class accessible by my WebView? Do I need to add @JavascriptInterface to every single method?

Yes, you do. This is a security feature, so a rogue web page can't access methods it shouldn't be accessing.

Do I have to configure ProGuard on this issue?

Yes, you do. Reflection is used to get the Java method name from JavaScript. If ProGuard obfuscates the method names, the WebViewClient will be unable to locate the correct method on the Java class.

When Android Studio creates a project, a default ProGuard config file is created that should have some comments about adding your javascript interface and exactly how to do that.

kris larson
  • 30,387
  • 5
  • 62
  • 74