简体   繁体   English

如果打开了proguard,如何获取类字段?

[英]How to get class fields if proguard is on?

I am trying to iterate over public static fields in a class with private constructor, but getFields/getDeclaredField returns a zeroSize array if Proguard is On, so I never see Log messages from loop. 我正在尝试使用私有构造函数遍历类中的公共静态字段,但是如果Proguard为On,则getFields / getDeclaredField将返回一个ZeroSize数组,因此我从没有看到来自循环的日志消息。 In profuard I added 在profuard中,我添加了

-keep public class com.ello.bases.SyncKeysConstants

But it did not help Constants 但这并没有帮助常量

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

}

Method 方法

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

Log 日志记录

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

Full proguard 全员

# 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.**{*;}

How to keep file to be able use reflection methods on it?? 如何保持文件能够在其上使用反射方法?

I need to keep not only class but also all its members 我不仅要保留课程,还要保留其所有成员

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM