【问题标题】:How to get class fields if proguard is on?如果 proguard 开启,如何获取类字段?
【发布时间】:2016-03-06 15:08:35
【问题描述】:

我正在尝试使用私有构造函数迭代类中的公共静态字段,但如果 Proguard 为 On,getFields/getDeclaredField 返回一个 zeroSize 数组,因此我永远不会看到来自循环的日志消息。 在profuard我添加了

-keep public class com.ello.bases.SyncKeysConstants

但这并没有帮助 常量

public final class SyncKeysConstants {
  private SyncKeysConstants() {
  }
  public static final String HOME = "home";

}

方法

public class ConnectionBroadcastReceiver extends BroadcastReceiver {
  public ConnectionBroadcastReceiver() {
  }

  @Override public void onReceive(Context context, Intent intent) {
    boolean connected = true;
    Log.e("updateConnection", " current - " + connected);
    Field[] fields = SyncKeysConstants.class.getFields();
    Log.e("updateConnection", "fields length - " + fields.length);

    for (Field f : fields) {
      Log.e("updateConnection", "field name - " + f.getName());
      try {
        String value = (String) f.get(null);
        Log.e("updateConnection", "field name - " + f.getName() + ", data - " + value);
        if (connected) {
          SyncStatus.setNoConnection(context, value, false);
        }
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
  }
}

日志

E/updateConnection:  current - true
E/updateConnection: fields length - 0

完整的保护

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# 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

# Reduce the size of the output some more.

-repackageclasses ''
-allowaccessmodification

# Switch off some optimizations that trip older versions of the Dalvik VM.
-dontobfuscate
-optimizations !code/allocation/variable/!field

# keep annotations for otto, retrofit, butterknife and others
-keepattributes *Annotation*

#keep signatures
-keepattributes Signature
# Preserve all fundamental application classes.

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class com.ello.bases.SyncKeysConstants
# Preserve all View implementations, their special context constructors, and
# their setters.

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public 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);
}

# Preserve all classes that have special context constructors, and the
# constructors themselves.

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

# Preserve all classes that have special context constructors, and the
# constructors themselves.

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve all possible onClick handlers.

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

# Preserve the special fields of all Parcelable implementations.

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.

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

# Preserve annotated Javascript interface methods.

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

# The Android Compatibility library references some classes that may not be
# present in all versions of the API, but we know that's ok.
-dontwarn android.support.**

# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}
-dontwarn org.apache.**


# Preserve the special static methods that are required in all enumeration
# classes.

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your application doesn't use serialization.
# If your code contains serializable classes that have to be backward 
# compatible, please refer to the manual.

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
# Removes loggin calls

#-assumenosideeffects class android.util.Log {
#    public static boolean isLoggable(java.lang.String, int);
#    public static int v(...);
#    public static int i(...);
#    public static int w(...);
#    public static int d(...);
#    public static int e(...);
#}
######################################################################################################

# facebook
-keep class com.facebook.** { *; }

#
#play services
#

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

#
#otto
#

-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

#
# butterknife
#

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}
#
# retrofit
#

-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

-dontwarn rx.**
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}
-keep class com.ello.GsonModels.** { *; }
-keep class com.ello.data.** { *; }

#
#twitter
-dontwarn twitter4j.**
-keep class twitter4j.** { *; }
#
# comscore
#
-keep class com.comscore.** { *; }

-dontwarn com.comscore.**

-keep class com.google.android.maps.** { *; }
-keep interface com.google.android.maps.** { *; }

#
#okio
#

-dontwarn okio.**

#
# Simple XML
#

-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; }
-dontwarn org.simpleframework.xml.stream.**
#
# aptitude
#
-dontwarn com.amplitude.**
-keep class com.amplitude.**{*;}

如何保存文件以便能够对其使用反射方法??

【问题讨论】:

  • @dotvav 提供的帖子中还有另一个问题,OP 要求分离静态字段,而他在获取字段时没有问题。我的问题 - 我没有收到文件
  • @Yarh 您的代码运行良好:ideone.com/RDMiWh
  • 也许您不相关的check(context); 操作因异常而失败,您永远无法调用getFields()?或者您的类不匹配并且您在运行时评估的SyncKeysConstants 不是您在此处发布的源代码的SyncKeysConstants
  • @assylias:这里没有帮助,因为HOME 是编译时常量,因此,如果类在运行时不同,您不会注意到。

标签: java android reflection


【解决方案1】:

我不仅要保留班级,还要保留班级的所有成员

-keepclassmembers class com.ello.bases.SyncKeysConstants {
    public *;    
}

【讨论】:

  • 我认为,当你使用像SyncKeysConstants.class.getFields() 这样的类文字时,你不需要保留类,而只需要保留成员。
  • @Holger 你又是对的:) 只需要保持常量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 2014-07-05
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多