我试图使用私有构造函数迭代一个类中的公共静态字段,但是如果Proguard为On,则getFields / getDeclaredField返回一个zeroSize数组,所以我从来没有看到来自循环的Log消息。 在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.**{*;}
如何保持文件能够使用反射方法?
答案 0 :(得分:2)
我不仅要保持课程,还需要保留所有成员
application/octet-stream