Question

I'm using ProGuard to obfuscate my Android app.

I'm also using WebView to show a webpage (an HTML walkthrough page) that contains a button that will close the WebView. There is a function in the Javascript that calls back a closeWalkthrough() method:

function closeFunction()
{
    MyClass.closeWalkthrough();
}

The releated Java class looks like this:

package com.myclass.android;

import android.app.Activity;
import android.content.Context;
import android.webkit.JavascriptInterface;

public class JavaScriptInterface {

    Context _context;

    JavaScriptInterface(Context context) {
        _context = context;
    }

    @JavascriptInterface
    public void closeWalkthrough() {
        ((Activity) _context).finish();
    }
}

I've added the following flags in my ProGuard file in hopes that it won't obfuscate the JavaScriptInterface class because, if I understand it correctly, the Javascript method MyClass.closeWalkthrough() is looking for the closeWalkthrough() found in my JavaScriptInterface Java class.

...
-keepattributes JavascriptInterface
-keep public class com.myclass.android.JavaScriptInterface { *; }
...

However, whenever I look at my mapping.txt file, I see that com.myclass.android.JavaScriptInterface gets obfuscated:

...
com.myclass.android.JavaScriptInterface -> axf:
    android.content.Context _context -> a
...

I even added a -keep public class flag for the file that creates the WebView but it still doesn't work.

Any pointers on what I might be doing wrong?

I should also mention that when I don't use ProGuard, the button works properly and closes the WebView.

In case it helps, here's my complete proguard-project.txt file (I'm using IntelliJ):

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

#-----------------------------------------------------------
#                   CUSTOM DEFINED FLAGS
#-----------------------------------------------------------

# Note that in order for Log to be hidden, you must have optimization enabled.
# Source: https://groups.google.com/d/msg/adt-dev/60wPZrk8qMU/-9KLgBZnIS4J
-assumenosideeffects class android.util.Log {
    public static int d(...);
    public static int w(...);
    public static int v(...);
}

#-repackageclasses ''
#-allowaccessmodification

-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }
-keep class org.codehaus.** { *; }
-keep class com.facebook.** { *; }

-keepattributes JavascriptInterface
-keep public class com.myclass.android.JavaScriptInterface { *; }
-keep public class com.myclass.android.WalkThroughActivity { *; }

-dontwarn android.support.**
-dontwarn com.facebook.**
-dontwarn com.google.**
-dontwarn com.google.android.gms.**
-dontwarn com.google.code.**
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn com.google.gdata.**, com.google.common.**
-dontwarn com.ibm.icu.text.**
-dontwarn com.sun.**
-dontwarn demo.**
-dontwarn java.awt.**
-dontwarn java.awt.**,javax.security.**,java.beans.**
-dontwarn java.beans.**
-dontwarn java.lang.management.**
-dontwarn javax.**
-dontwarn javax.swing.**
-dontwarn oauth.signpost.**
-dontwarn org.apache.**
-dontwarn org.apache.commons.codec.binary.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.apache.log4j.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn org.joda.time.**
-dontwarn org.mortbay.**
-dontwarn org.slf4j.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn sun.misc.Unsafe
-dontwarn twitter4j.**
-dontwarn org.codehaus.jackson.**
Was it helpful?

Solution

You can instruct ProGuard to keep all annotated methods:

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

This should probably be part of the default configuration in the Android SDK.

OTHER TIPS

Not sure if it is just a typo in your post or not, but the ProGuard configuration file refers to

com.myapp.android.JavaScriptInterface

whereas your Java class indicates a different package

package com.myclass.android; // myapp != myclass

and, of course, they must be the same.

EDIT: in addition to your existing configuration, try adding the following option:

-keepclassmembers class com.myclass.android.JavaScriptInterface { 
    public <methods>;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top