Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JailedBird opened this issue Apr 11, 2024 · 1 comment

Comments

@JailedBird
Copy link
Owner

JailedBird commented Apr 11, 2024

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
Copy link
Owner Author

JailedBird commented Apr 11, 2024

分支 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!

@JailedBird JailedBird pinned this issue Apr 11, 2024
@JailedBird JailedBird changed the title 混淆提醒:高版本AGP8开启混淆可能导致导致AutoWired注解解析报错 作者提醒混淆:高版本AGP8开启混淆可能导致AutoWired注解解析报错 Apr 11, 2024
@JailedBird JailedBird changed the title 作者提醒混淆:高版本AGP8开启混淆可能导致AutoWired注解解析报错 作者提醒混淆:高版本AGP8开启混淆可能导致AutoWired注解解析报错,解法如下 Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant