ProGuard即使在“keep class”标志之后也会混淆类.影响Android WebView行为



我正在使用ProGuard来混淆我的Android应用程序。

我还使用WebView来显示一个网页(一个HTML演练页面),其中包含一个按钮,将关闭WebView。Javascript中有一个函数回调closeWalkthrough()方法:

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

相关的Java类看起来像这样:

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();
    }
}

我在我的ProGuard文件中添加了以下标志,希望它不会混淆JavaScriptInterface类,因为,如果我正确理解它,Javascript方法MyClass.closeWalkthrough()正在寻找在我的JavaScriptInterface Java类中找到的closeWalkthrough()

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

然而,每当我查看mapping.txt文件时,我看到com.myclass.android.JavaScriptInterface被混淆了:

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

我甚至为创建WebView的文件添加了-keep public class标志,但它仍然不起作用。

有什么我可能做错了吗?

我还应该提到,当我不使用ProGuard时,按钮正常工作并关闭WebView

如果它有帮助,这是我完整的proguard-project.txt文件(我使用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.**

您可以指示ProGuard保留所有带注释的方法:

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

这应该是Android SDK默认配置的一部分。

不确定这是否是你的文章中的一个错别字,但是ProGuard配置文件引用了

com.myapp.android.JavaScriptInterface

而您的Java类指示不同的包

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

,当然,它们必须相同。

EDIT:除了您现有的配置,尝试添加以下选项:

-keepclassmembers class com.myclass.android.JavaScriptInterface { 
    public <methods>;
}

最新更新