Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard mess Javascript Interface functions when targeting SDK 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() {
            //....
        }
}

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

In the AndroidManifest I had the following

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

In the project I used proguard which declared:

-keepattributes JavascriptInterface

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

and everything was working fine.

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.

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?

like image 728
andreasv Avatar asked Jan 21 '14 13:01

andreasv


1 Answers

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

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*

Note: your MyJavaScriptInterface must be Public class

Ref#: Android Proguard Javascript Interface Fail

Br,

Frank

like image 128
Frank Nguyen Avatar answered Sep 27 '22 17:09

Frank Nguyen