Android中的Proguard和反射

jax*_*jax 34 java reflection obfuscation android proguard

我刚刚使用了proguard,但我试图通过反射实例化的类不起作用.

我有一个界面

Algorithm
Run Code Online (Sandbox Code Playgroud)

我通过这样的课程

AlgorithmFactory.SomeClassThatExtendsAlgorithmImpl.class
Run Code Online (Sandbox Code Playgroud)

该类实例化如下

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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的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(...);
}
Run Code Online (Sandbox Code Playgroud)

jax*_*jax 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);
}
Run Code Online (Sandbox Code Playgroud)

第一个keep告诉proguard不要混淆扩展YourClassName的类名

第二个说保持构造函数名称(<init>意思是构造函数)没有混淆,它有一个参数Context和extendsYourClassName

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

-keepclassmembers class * {
 public void myClickHandler(android.view.View);
}
Run Code Online (Sandbox Code Playgroud)

这表示在所有类中保留所有myClickHandler使用单个参数命名的方法View.您可以通过使用上面的extends关键字进一步限制这一点.

希望这可以帮助.


Ami*_*far 18

对于on click fix,您不必列出每个方法名称.你可以做:

-keepclassmembers class * {
   public void *(android.view.View);
}
Run Code Online (Sandbox Code Playgroud)

查找具有View作为参数的所有方法.