Javascript functionality for Proguard mess when using SDK targeting in Android Manifest above 17

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() { //.... } } 

which includes the JavascriptInterface , which I use to communicate on the JavaScript side on the Android side.

In AndroidManifest, I had the following

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

In the project, I used proguard, which stated:

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

and everything is working fine.

However, when I changed my AndroidManifest to android:targetSdkVersion=18 or 19 and tested on devices with 18 and above, proguard seems to somehow mess up the JavaScript methods that are no longer available.

If I go back to 16 or anything less than 17, everything will be fine. In addition, this only happens with proguard. If I don't use proguard, everything works fine even with android:targetSdkVersion=18 or 19. Can someone help make it work when targeting in an Android manifest of> 17?

+6
source share
3 answers

I am copying my answer from this thread for you: fooobar.com/questions/138901 / ...

And if you use Proguard, be sure 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 that's not all, add it.

 -keepattributes *Annotation* 

Note: your MyJavaScriptInterface must be a public class

Ref #: Javascript interface malfunction for Android Proguard

Br

Franc

+19
source

These 4 lines are usually enough - and there is no need to publish the interface.

 -keepattributes JavascriptInterface -keepclassmembers class * { @android.webkit.JavascriptInterface <methods>; } 
+6
source

In my case, just:

 -keepclassmembers class com.mypackage.MyJavaScriptInterface { public *; } -keepattributes *Annotation* 

was enough!

+1
source

All Articles