Android中的Proguard和reflection

我刚刚使用了proguard,但是我尝试通过reflection来实例化的类不工作。

我有一个接口

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

Solutions Collecting From Web of "Android中的Proguard和reflection"

解决了

对于其他人有这个问题,你需要添加以下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>意味着构造函数)un-obfuscated有一个Context参数,并扩展YourClassName

此外,对于在您的XML布局文件中使用onClick属性的 android开发人员,您还需要在您的proguard.cnf文件中添加该函数的名称。

 -keepclassmembers class * { public void myClickHandler(android.view.View); } 

这说在所有类中保持所有名为myClickHandler方法myClickHandler带有一个参数View 。 你可以使用上面的extends关键字进一步限制这个。

希望这可以帮助。

对于点击修复,您不必列出每个方法的名称。 你可以做:

 -keepclassmembers class * { public void *(android.view.View); } 

find所有具有View作为参数的方法。