Android Proguard Javascript Interface Fail

2019-01-06 17:55发布

I use in my project a piece of code as described here

http://lexandera.com/2009/01/extracting-html-from-a-webview/

I create the .apk file, install it on my device and it correctly works. If I try to use the obfuscation with proguard the project fails, the method showHTML(String html) of MyJavaScriptInterface is not reached.

My proguard configuration regarding that

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

according to this this answer Android proguard Javascript Interface problem.

SOLVED.

As Eric suggested, I changed the Proguard configuration file like this:

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

Now my project works perfectly.

For API 17+ you also need to preserve the @JavascriptInterface annotations:

-keepattributes JavascriptInterface

http://developer.android.com/reference/android/webkit/JavascriptInterface.html

4条回答
\"骚年 ilove
2楼-- · 2019-01-06 18:07

If MyJavaScriptInterface is an inner class of MyClass, ProGuard expects a fully qualified name com.mypackage.MyClass$MyJavaScriptInterface. The naming convention with $ is used in the compiled class files on which ProGuard operates. Note that ProGuard mentions class names in the configuration that it can't find in the input jar, suggesting that these names may have been misspelled.

查看更多
Juvenile、少年°
3楼-- · 2019-01-06 18:21

As suggested by edit in question, out of those suggestions, only using

-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface { public *; }

with Important -

For API 17+ to preserve @JavascriptInterface annotations:

-keepattributes JavascriptInterface

(Which was stopping my app to work on Marshmallow)

查看更多
迷人小祖宗
4楼-- · 2019-01-06 18:22
    -keepclassmembers class com.mypackage.MyClass$JavaScriptInterface {    
public *;
     }

Use only this. It works for me.

查看更多
趁早两清
5楼-- · 2019-01-06 18:31

Those Who are laze to provide the entire package path.

-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}
查看更多
登录 后发表回答