gpt4 book ai didi

java - 如果启用了 proguard,如何获取类字段?

转载 作者:行者123 更新时间:2023-12-02 04:08:11 25 4
gpt4 key购买 nike

我正在尝试使用私有(private)构造函数迭代类中的公共(public)静态字段,但如果 Proguard 处于打开状态,则 getFields/getDeclaredField 返回一个 ZeroSize 数组,因此我从未看到来自循环的日志消息。在深处我添加了

-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

完整的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.**{*;}

如何保留文件以便能够在其上使用反射方法?

最佳答案

我不仅需要保留类(class),还需要保留其所有成员

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

关于java - 如果启用了 proguard,如何获取类字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34041668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com