Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Proguard Javascript Interface Fail

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

like image 245
Denis Avatar asked Jun 07 '11 20:06

Denis


3 Answers

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.

like image 166
Eric Lafortune Avatar answered Oct 23 '22 18:10

Eric Lafortune


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

Use only this. It works for me.

like image 35
Sangoku Avatar answered Oct 23 '22 19:10

Sangoku


Those Who are laze to provide the entire package path.

-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}
like image 2
Nantha kumar Avatar answered Oct 23 '22 19:10

Nantha kumar