即使在"保持类"标志之后,ProGuard也会对类进行模糊处理.影响Android WebView行为

Fil*_*ero 1 java android proguard android-webview

我正在使用ProGuard来混淆我的Android应用.

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

function closeFunction()
{
    MyClass.closeWalkthrough();
}
Run Code Online (Sandbox Code Playgroud)

相关的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

...
-keepattributes JavascriptInterface
-keep public class com.myclass.android.JavaScriptInterface { *; }
...
Run Code Online (Sandbox Code Playgroud)

但是,每当我查看我的mapping.txt文件时,我都会看到它com.myclass.android.JavaScriptInterface被混淆:

...
com.myclass.android.JavaScriptInterface -> axf:
    android.content.Context _context -> a
...
Run Code Online (Sandbox Code Playgroud)

我甚至-keep public class为创建WebView它的文件添加了一个标志,但它仍然无效.

关于我可能做错的任何指示?

我还要提一下,当我不使用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.**
Run Code Online (Sandbox Code Playgroud)

Eri*_*une 7

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

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}
Run Code Online (Sandbox Code Playgroud)

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