ProGuard的,即使混淆类`保持class`标志。影响的Andr​​oid的WebView行为 [英] ProGuard obfuscating classes even after `keep class` flag. Affecting Android WebView behavior

查看:190
本文介绍了ProGuard的,即使混淆类`保持class`标志。影响的Andr​​oid的WebView行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ProGuard的混淆我的Andr​​oid应用程序。

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

 函数closeFunction()
{
    MyClass.closeWalkthrough();
}
 

在相关型号的Java类看起来是这样的:

 包com.myclass.android;

进口android.app.Activity;
进口android.content.Context;
进口android.webkit.JavascriptInterface;

公共类JavaScriptInterface {

    语境_context;

    JavaScriptInterface(上下文的背景下){
        _context =背景;
    }

    @JavascriptInterface
    公共无效closeWalkthrough(){
        ((活动)_context).finish();
    }
}
 

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

  ...
-keepattributes JavascriptInterface
-keep公共类com.myclass.android.JavaScriptInterface {*; }
...
 

不过,每当我看我的 mapping.txt 文件,我看到 com.myclass.android.JavaScriptInterface 被混淆的:

  ...
com.myclass.android.JavaScriptInterface  - > AXF:
    android.content.Context _context  - >一个
...
 

我还添加了一个 -keep公共类标志用于创建的WebView文件,但它仍然没有按'科技工作。

这是什么,我可能是做错了任何指针?

我还要提到的是,当我不使用ProGuard的,按钮正常工作并关闭的WebView

在情况下,它可以帮助,这是我的完整的 ProGuard的-project.txt 文件(我用的IntelliJ):

 #这是ProGuard的配置文件。
#http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

#优化是默认关闭的。地塞米松不喜欢code运行
#通过ProGuard的优化和preverify步骤(并执行一些
#这些优化自身)的。
-dontoptimize
-dont preverify
#请注意,如果要启用优化,你不能只是
#在你自己的项目配置文件的优化参数;
#相反,你需要将指向
#ProGuard的,Android的optimize.txt的文件,而不是这一个从
#project.properties文件。

-keepattributes *注释*
-keep公共类com.google.vending.licensing.ILicensingService
-keep公共类com.android.vending.licensing.ILicensingService

#对于本地方法,请参阅http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames类* {
    天然的LT;方法&gt ;;
}

#守制定者的意见,使动画仍然可以正常工作。
#看到http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers公共类*扩展android.view.View {
   无效套*(***);
   *** 得到*();
}

#我们要保持活性的方法,可以在XML中使用属性的onClick
-keepclassmembers类*扩展android.app.Activity {
   公共无效*(android.view.View);
}

#对于枚举类,请参见http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers枚举* {
    公共静态** []值();
    公共静态**的valueOf(java.lang.String中);
}

-keep类*实现android.os.Parcelable {
  公共静态最终android.os.Parcelable $造物主*;
}

-keepclassmembers类** R $ * {
    公共静态<领域取代;
}

#支持库包含对新版本的平台。
#不发出警告的情况下这个程序是针对连接的旧
#平台版本。我们了解他们,他们是安全的。
-dontwarn android.support。**

#------------------------------------------------- ----------
#CUSTOM定义的标志
#------------------------------------------------- ----------

#请注意,为了使日志被隐藏,您必须启用优化。
#来源:https://groups.google.com/d/msg/adt-dev/60wPZrk8qMU/-9KLgBZnIS4J
-assumenosideeffects类android.util.Log {
    公共静态INT D(...);
    公共静态INT W(...);
    公共静态INT V(...);
}

#-repackageclasses''
#-allowaccessmodification

-keep类的javax ** {*。 }
-keep类组织** {*。 }
-keep类twitter4j ** {*。 }
-keep类组织codehaus ** {*。; }
-keep类com.facebook ** {*。 }

-keepattributes JavascriptInterface
-keep公共类com.myclass.android.JavaScriptInterface {*; }
-keep公共类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演示。**
-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有机codehaus.jackson。**
 

解决方案

您可以指示ProGuard的将所有注释方法:

  -keepclassmembers类* {
    @ android.webkit.JavascriptInterface<方法&GT ;;
}
 

这也许应该是在Android SDK中的默认配置的一部分。

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.**

解决方案

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.

这篇关于ProGuard的,即使混淆类`保持class`标志。影响的Andr​​oid的WebView行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆