我刚刚使用了proguard,但是我试图通过反射实例化的类不起作用。
我有一个界面
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
-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(...);
}
答案 0 :(得分:59)
对于遇到此问题的其他人,您需要将以下内容添加到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
的类名第二个说保持构造函数名称(<init>
表示构造函数)没有混淆,其单个参数为Context
并扩展YourClassName
此外,对于在XML布局文件中使用 onClick属性的Android开发人员,您还需要在proguard.cnf文件中添加该函数的名称。
-keepclassmembers class * {
public void myClickHandler(android.view.View);
}
这表示在所有类中将名为myClickHandler
的所有方法保留为单个参数View
。您可以使用上面的extends关键字进一步限制这一点。
希望这会有所帮助。
答案 1 :(得分:18)
对于on click修复,您不必列出每个方法名称。你可以这样做:
-keepclassmembers class * {
public void *(android.view.View);
}
查找具有View作为参数的所有方法。
答案 2 :(得分:0)
因为编译过程将删除未使用的方法,所以即使您在编译过程中使用反射调用,构造函数方法也会被删除。
您可以在项目的use.txt中看到在编译过程中删除的方法。
因此,应将构造方法保留在proguard文件中。
-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{
public <init>(android.content.Context);
}