我正在使用 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 中默认配置的一部分。
关于java - ProGuard 混淆类甚至在 `keep class` 标志之后。影响 Android WebView 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618588/