开发者

Android Proguard Javascript Interface Fail

开发者 https://www.devze.com 2023-03-10 22:08 出处:网络
I use in my project a piece of code as described here http://lexandera.com/2009/01/extracting-html-from-a-webview/

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/a开发者_Python百科ndroid/webkit/JavascriptInterface.html


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.


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

Use only this. It works for me.


Those Who are laze to provide the entire package path.

-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消