Android 中的 Proguard 和反射 [英] Proguard and reflection in Android
问题描述
我刚刚使用了 proguard,但是我试图通过反射实例化的类不起作用.
I have just used proguard, but classes I am trying to instantiate via reflection are not working.
我有一个界面
Algorithm
我通过这样的课程
AlgorithmFactory.SomeClassThatExtendsAlgorithmImpl.class
类是这样实例化的
public ArrayList<Algorithm> getAlgorithms(Context cnx) {
ArrayList<Algorithm> list = new ArrayList<Algorithm>();
for(Class<? extends Algorithm> alg: algorithms) {
try {
Constructor<? extends Algorithm> c = alg.getConstructor(Context.class);
list.add(c.newInstance(cnx));
} catch (IllegalArgumentException e) {
Log.e(TAG, "IllegalArgumentException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
} catch (InvocationTargetException e) {
Log.e(TAG, "InvocationTargetException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
} catch (InstantiationException e) {
Log.e(TAG, "InstantiationException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
} catch (IllegalAccessException e) {
Log.e(TAG, "IllegalAccessException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
} catch (SecurityException e) {
Log.e(TAG, "SecurityException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
} catch (NoSuchMethodException e) {
Log.e(TAG, "NoSuchMethodException", e);
throw new IllegalStateException("There was a problem creating the Algorithm class");
}
}
return list;
}
这是我的 proguard.cnf
Here is my proguard.cnf
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** i(...);
public static *** w(...);
public static *** e(...);
}
推荐答案
SOLVED
对于遇到此问题的其他人,您需要将以下内容添加到 proguard.cnf
SOLVED
For others that are having this problem you need to add the following to proguard.cnf
-keep public class * extends com.yoursite.android.yourappname.YourClassName
-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{
public <init>(android.content.Context);
}
第一个 keep 告诉 proguard 不要混淆扩展 YourClassName 的类名
The first keep tells proguard to not obfuscate class names that extend YourClassName
第二个说保持构造函数名称(<init>
表示构造函数)未混淆,它具有 Context
的单个参数并扩展 YourClassName
The second one says to keep the constructor name (<init>
means constructor) un-obfuscated that has a single argument of Context
and extends YourClassName
此外,对于在 XML 布局文件中使用 onClick 属性的 Android 开发者,您还需要在 proguard.cnf 文件中添加函数名称.
In addition, for android developers that are using the onClick attribute in you XML layouts file you will also need to add the name of the function in your proguard.cnf file.
-keepclassmembers class * {
public void myClickHandler(android.view.View);
}
这表示在所有类中使用单个参数 View
保留所有名为 myClickHandler
的方法.您可以使用上面的 extends 关键字进一步限制这一点.
This says keep all methods named myClickHandler
with a single argument View
in all classes. You could further constrain this by using the extends keyword like above.
希望这会有所帮助.
这篇关于Android 中的 Proguard 和反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!