混淆配置keep\keepclassmembers\keepcl

2018-01-05  本文已影响654人  SMSM

用了一个开源的RecyclerView库,用着倍爽,http://www.recyclerview.org/。爽在,不用自己new BaseViewHolder了,通过对Adapter指定泛型,内部解析泛型的类型,反射的方式搞定自定实例化指定类型的ViewHolder。详细代码见底部。
但是忘记了配 Proguard,导致release包,出现类型转换异常,原因是:没有配置Proguard导致,未直接显示new的类的构造函数被当做了无效函数,而被强制的删除(删除了构造函数),导致反射失败,反射失败后,开源库走了兜底方案,实例化了一个Base类,导致使用时强制向下转型到自定义类,出现类型异常。

------BaseDishCateVH的smali文件------
.class public Lcom/meituan/sankuai/erpboss/modules/dish/adapter/vh/a;
.super Lcom/meituan/sankuai/cep/component/recyclerviewadapter/d;
.source "BaseDishCateVH.java"

# instance fields
.field public n:Landroid/widget/TextView;

-------BaseDishCateVH.java-----------
public class BaseDishCateVH extends BaseViewHolder {
    public TextView tvCateName = (TextView)this.getView(2131559265);

    public BaseDishCateVH(View view) {
        super(view);
    }
}

-----usage.txt文件 被移除的无用类、方法、属性-----
com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH:
    20:22:public BaseDishCateVH(android.view.View)
com.meituan.sankuai.erpboss.modules.dish.bean.BatchSelectDishEvent
com.meituan.sankuai.erpboss.modules.dish.bean.DishItem
com.meituan.sankuai.erpboss.modules.dish.bean.EventChooseCategory:
    23:24:public void setType(int)
    31:32:public void setCate(com.meituan.sankuai.erpboss.modules.dish.bean.assort.DishCateV2TO)
com.meituan.sankuai.erpboss.modules.dish.bean.GroupItem
com.meituan.sankuai.erpboss.modules.dish.bean.IDishListSelectBean:
    public abstract boolean isSelected()
    public abstract void setSelected(boolean)
com.meituan.sankuai.erpboss.modules.dish.bean.MenuBean:
    public static final int TYPE_H5
    public static final int TYPE_NATIVE
    public int type
    25:29:public MenuBean(java.lang.String,java.lang.String,int)

如何解决呢1、配置混淆,2、new 的方式显性使用。方案2 不可行。
如何配置混淆呢?如下,告知了编译器,对所有继承关系下的类的构造函数不删除。

如下举例三种不同的混淆配置的作用。

配置项+类名,当前包下class
-keep class com.pitaya.commanager.{;}

配置项+类名,当前包下class+所有子包下的class
-keep class com.pitaya.commanager.*{;} #类内部的

类名不被混淆,所有的子类,不包含当前类
-keep public class * extends com.pitaya.commanager.ComLifecycle

方法,所有的子类,不包含当前类
-keepclassmembers class $ extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder {
<init>(...);
}

方法+类名,所有的子类,不包含当前类
-keepclasseswithmembers class * extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder{
<init>(...);
}

BaseDishCateVH
DishPrinterListAdapter$PrinterSelectListVH

日志顺序usage.txt - seeds.txt - mapping.txt 

# ------混淆方式1----只保证类名不被混淆,构造方法、普通方法、属性都会被混淆、或者移除
-keep public class * extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder
-keepclassmembers  class **$** extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder {
     <init>(...);
}


---------BaseDishCateVH
com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH:
    20:22:public BaseDishCateVH(android.view.View)

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH -> com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH:
    android.widget.TextView tvCateName -> a



# ------混淆方式2----只保证配置的方法或属性不被移除和混淆,类名会被混淆
-keepclassmembers class * extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder{
      <init>(...);
}


----------PrinterSelectListVH
无

com.meituan.sankuai.erpboss.modules.dish.adapter.DishPrinterListAdapter$PrinterSelectListVH: DishPrinterListAdapter$PrinterSelectListVH(android.view.View)

com.meituan.sankuai.erpboss.modules.dish.adapter.DishPrinterListAdapter$PrinterSelectListVH -> com.meituan.sankuai.erpboss.modules.dish.adapter.DishPrinterListAdapter$a:
    android.widget.TextView tvTextPrinter -> a
    android.widget.CheckBox cbIsCheck -> b
    40:43:void <init>(android.view.View) -> <init>


----------BaseDishCateVH
无

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH: BaseDishCateVH(android.view.View)

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH -> com.meituan.sankuai.erpboss.modules.dish.adapter.vh.a:
    android.widget.TextView tvCateName -> a
    20:22:void <init>(android.view.View) -> <init>



# ------混淆方式3----保证类名和配置项不会被移除和混淆
-keepclasseswithmembers class * extends com.meituan.sankuai.cep.component.recyclerviewadapter.BaseViewHolder{
      <init>(...);
}

----------BaseDishCateVH
无

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH
com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH: BaseDishCateVH(android.view.View)

com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH -> com.meituan.sankuai.erpboss.modules.dish.adapter.vh.BaseDishCateVH:
    android.widget.TextView tvCateName -> a
    20:22:void <init>(android.view.View) -> <init>

重新认识构造函数,smali中<init>开头的构造函数,和普通的方法没什么区别,没有new时也会被Proguard移除,列明错误的理解是:配置不混淆

混淆成功后,除生成了指定类型的混淆包外,还会在工程的根目录下或是根目录下得bin文件夹中生成proguard文件夹,里面包含dump.txt、mapping.txt、seeds.txt和usage.txt四个文件。

内部类的构造函数,会被改写,新增一个代表外部类实例的入参,如何获取到呢?很有用的!

    class BatchSelectDishVH extends BaseDishItemVH {
        CheckBox mCheckBox;

        public BatchSelectDishVH(View itemView) {
            super(itemView);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.batch_select);
        }
    }


com.meituan.sankuai.erpboss.modules.dish.adapter.batch.BatchSelectDishAdapterV2$BatchSelectDishVH: BatchSelectDishAdapterV2$BatchSelectDishVH(com.meituan.sankuai.erpboss.modules.dish.adapter.batch.BatchSelectDishAdapterV2,android.view.View)



开源库的相关代码
    protected K createBaseViewHolder(ViewGroup parent, int layoutResId) {
        return createBaseViewHolder(getItemView(layoutResId, parent));
    }

    /**
     * if you want to use subclass of BaseViewHolder in the adapter,
     * you must override the method to create new ViewHolder.
     *
     * @param view view
     * @return new ViewHolder
     */
    @SuppressWarnings("unchecked")
    protected K createBaseViewHolder(View view) {
        Class temp = getClass();
        Class z = null;
        while (z == null && null != temp) {
            z = getInstancedGenericKClass(temp);
            temp = temp.getSuperclass();
        }
        K k;
        // 泛型擦除会导致z为null
        if (z == null) {
            k = (K) new BaseViewHolder(view);
        } else {
            k = createGenericKInstance(z, view);
        }
        return k != null ? k : (K) new BaseViewHolder(view);
    }

    /**
     * try to create Generic K instance
     */
    @SuppressWarnings("unchecked")
    private K createGenericKInstance(Class z, View view) {
        try {
            Constructor constructor;
            // inner and unstatic class
            if (z.isMemberClass() && !Modifier.isStatic(z.getModifiers())) {
                constructor = z.getDeclaredConstructor(getClass(), View.class);
                constructor.setAccessible(true);
                return (K) constructor.newInstance(this, view);
            } else {
                constructor = z.getDeclaredConstructor(View.class);
                constructor.setAccessible(true);
                return (K) constructor.newInstance(view);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get generic parameter K
     */
    private Class getInstancedGenericKClass(Class z) {
        Type type = z.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            for (Type temp : types) {
                if (temp instanceof Class) {
                    Class tempClass = (Class) temp;
                    if (BaseViewHolder.class.isAssignableFrom(tempClass)) {
                        return tempClass;
                    }
                } else if (temp instanceof ParameterizedType) {
                    Type rawType = ((ParameterizedType) temp).getRawType();
                    if (rawType instanceof Class && BaseViewHolder.class.isAssignableFrom((Class<?>) rawType)) {
                        return (Class<?>) rawType;
                    }
                }
            }
        }
        return null;
    }
上一篇下一篇

猜你喜欢

热点阅读