ProGuard,并将其反映在Android中将其、ProGuard、Android

2023-09-12 04:08:01 作者:关谷神秘@

我刚使用ProGuard的,但类我试图通过反射来实例不工作。

我有一个接口

 算法
 

我通过类这样的

  AlgorithmFactory.SomeClassThatExtendsAlgorithmImpl.class
 

类实例化这样

 公开的ArrayList<算法> getAlgorithms(上下文CNX){
ArrayList的<算法>名单=新的ArrayList<算法>();

对于(类和LT ;?扩展算法> ALG:算法){

    尝试 {
        构造函数和LT ;?扩展算法> C = alg.getConstructor(Context.class);
        list.add(c.newInstance(CNX));
    }赶上(抛出:IllegalArgumentException E){
        Log.e(TAG,抛出:IllegalArgumentException,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }赶上(的InvocationTargetException E){
        Log.e(TAG的InvocationTargetException,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }赶上(InstantiationException E){
        Log.e(TAG,InstantiationException,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }赶上(IllegalAccessException E){
        Log.e(TAG,IllegalAccessException,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }赶上(SecurityException异常E){
        Log.e(TAG,SecurityException异常,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }赶上(NoSuchMethodException E){
        Log.e(TAG,NoSuchMethodException,E);
        抛出新IllegalStateException异常(有创造算法类的问题);
    }
}
返回列表;
}
 
Android优化 proguard之缩减体积

下面是我的proguard.cnf

  -optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dont preverify
-verbose
-optimizations!code /简/算术,!现场/ * ,!类/合并/ *

-keep公共类*扩展android.app.Activity
-keep公共类*扩展android.app.Application
-keep公共类*扩展android.app.Service
-keep公共类*扩展android.content.BroadcastReceiver
-keep公共类*扩展android.content.ContentProvider
-keep公共类com.android.vending.licensing.ILicensingService


-keepclasseswithmembernames类* {
    天然的LT;方法&gt ;;
}

-keepclasseswithmembernames类* {
    公共和LT; INIT>(android.content.Context,android.util.AttributeSet);
}

-keepclasseswithmembernames类* {
    公共和LT; INIT>(android.content.Context,android.util.AttributeSet,INT);
}

-keepclassmembers枚举* {
    公共静态** []值();
    公共静态**的valueOf(java.lang.String中);
}

-keep类*实现android.os.Parcelable {
  公共静态最终android.os.Parcelable $造物主*;
}

-assumenosideeffects类android.util.Log {
    公共静态*** D(...);
    公共静态*** V(...);
    公共静态***我(...);
    公共静态*** W(...);
    公共静态*** E(...);
}
 

解决方案

解决

对于别人认为是有这个问题,你需要添加以下内容proguard.cnf

  -keep公共类*扩展com.yoursite.android.yourappname.YourClassName

-keepclassmembers类*扩展com.yoursite.android.yourappname.YourClassName {
 公共和LT; INIT>(android.content.Context);
}
 

第一保持告诉ProGuard的不模糊的类名,扩展YourClassName

第二个说要保持构造名(< INIT> 表示构造)未混淆,有一个上下文的参数并延伸 YourClassName

此外,对于在你的XML布局文件中使用的 onclick属性Android开发你还需要添加在你的proguard.cnf文件中的函数的名称。

  -keepclassmembers类* {
 公共无效myClickHandler(android.view.View);
}
 

本说,把所有名为 myClickHandler 在所有类的一个参数查看的方法。您可以通过使用extends关键字像上面进一步约束这一点。

希望这会有所帮助。

I have just used proguard, but classes I am trying to instantiate via reflection are not working.

I have an interface

Algorithm

I pass classes like this

AlgorithmFactory.SomeClassThatExtendsAlgorithmImpl.class

The class is instantiated like this

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;
}

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

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

The first keep tells proguard to not obfuscate class names that extend 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

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

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.

hope this helps.