简体   繁体   中英

Android JavascriptInterface not working with minifyEnabled

Thanks in advance.

I spend too much time reading stackoverflow about this. I have an hybrid app with a webview and inside a jquery web. I have my javascript interface.

Every think works fine when I run my app in debug mode, but when I run in release mode the javascript interface not working at all.

I try too many solutions like proguard rules:

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

I have my notation @JavascriptInterface as you see:

public static class WebAppInterface {
    Context mContext;

WebAppInterface(Context c) {
    mContext = c;
}

@JavascriptInterface
    public void estoyenchat() { enchat=true;
}
....

}

And in my gradle file:

minSdkVersion 14
targetSdkVersion 22

And the only solution I found to run the app with the javascript interface working in release mode is to set minifyEnabled to false.

Someone knows the reason?

Create an interface named JsInterface

public interface JsInterface {}

Implement it in your js interface classes.

public class WebAppInterface implements JsInterface {

Context mContext;

WebAppInterface(Context c) {
    mContext = c;
}

@JavascriptInterface 
public void estoyenchat() { enchat=true;
}

Add to proguard:

-keepclassmembers,allowobfuscation,allowoptimization interface * extends com.mysite.JsInterface{*;}

The correct way to keep any methods that have the @JavascriptInterface annotation would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM