Android Proguard Javascript Interface Fail

13,755

Solution 1

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.

Solution 2

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

Use only this. It works for me.

Solution 3

Those Who are laze to provide the entire package path.

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

Solution 4

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)

Share:
13,755
Denis
Author by

Denis

Updated on June 05, 2022

Comments

  • Denis
    Denis almost 2 years

    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

  • Denis
    Denis almost 13 years
    Hi Eric,I tried to modify Proguard configuration according to your suggestion, but the problem persists. Files generated by Proguard could help me to understand the problem?
  • Eric Lafortune
    Eric Lafortune almost 13 years
    Is the class MyJavaScriptInterface present in the processed code? Is the method showHTML present? You can check this with javap (on java class files) or dexdump (on Android dex files). The ProGuard documentation at Sourceforge contains an extensive example for Android, among other examples.
  • Denis
    Denis almost 13 years
    Hi Eric, I solved the problem. I made an error in Proguard configuration file. I wrote: -keepclassmembers class * implements com.mypackage.MyClass$MyJavaScriptInterface { <methods>; } instead -keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface { <methods>; } Thanks for suppots.
  • Nantha kumar
    Nantha kumar almost 8 years
    those who are lazy to type entire package. \n -keepclassmembers class **.*$JavaScriptInterface{ public *; }