JailedBird / ArouterGradlePlugin

Arouter auto register plugin for AGP7.4+ & AGP8
Apache License 2.0
46 stars 7 forks source link

作者提醒混淆:高版本AGP8开启混淆可能导致AutoWired注解解析报错,解法如下 #9

Closed JailedBird closed 7 months ago

JailedBird commented 7 months ago

README 关于新增混淆规则的说明,如果出现这个问题即可采用如下方案:

# 避免继承自TypeWrapper的匿名内部类(获取泛型T对应的Type)被混淆 导致Type获取失败
# Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
# at com.alibaba.android.arouter.facade.model.TypeWrapper.<init>(TypeWrapper.java:19)
# 这种情况下 就是匿名内部类被混淆导致 getClass().getGenericSuperclass() 从ParameterizedType变为Class从而导致Type类型转换异常
-keep class ** extends com.alibaba.android.arouter.facade.model.TypeWrapper { *; }
JailedBird commented 7 months ago

分支 bugfix/agp8.2.2_runtime_error 实验发现AGP 8.2.2启用混淆minify后,貌似 会把 TypeWrapper的匿名内部类从泛型ParameterizedType混淆为Class,导致类型转换报错;

public class TypeWrapper<T> {
    protected final Type type;

    protected TypeWrapper() {
        Type superClass = getClass().getGenericSuperclass();

        type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
    }

    public Type getType() {
        return type;
    }
}

导致对象注入时候,类型解析抛出异常;

    if(serializationService != null) {
      val res = substitute.intent?.extras?.getString("list")
      if(!res.isNullOrEmpty()) {
        val typeWrapper = object : TypeWrapper<MutableList<String>>(){}
        serializationService?.parseObject<MutableList<String>>(res, typeWrapper.type)?.let{
                substitute.list = it
            }
      }
    }

报错类似: Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType at com.alibaba.android.arouter.facade.model.TypeWrapper.(TypeWrapper.java:19)

添加上面的混淆应该就OK!