【解决问题的思路】its super classes have no public methods with the @Subscribe annotation

简介: 【解决问题的思路】its super classes have no public methods with the @Subscribe annotation

在打release包的时候遇到了这个问题,算是比较常见,记一下解决思路。


本文介绍了出现这个问题的原因,以及出现问题时我的建议和我的思路,着急的可以直接看解决方案。


异常

Subscriber class xxx and its super classes have no public methods with the @Subscribe annotation


遇到异常不要慌,其实已经提示的很明显了,指定了具体的class,且明确告诉你这个类没有@Subscribe注解。


原因

但其实知道了问题后,可能依然会有疑惑,比如我,因为我这个类是有@Subscribe注解的,那是为什么呢?


因为知道具体的class,也知道@Subscribe注解,所以很容易就能定位到是EventBus。


随后去EventBus官网寻找答案,果然issues第一页就有相同的问题,作者团队是这么回复的:


Check your R8 / ProGuard rules.

https://github.com/greenrobot/EventBus#r8-proguard


检查你的压缩/混淆规则。


这才反应过来,原来开启了R8压缩导致的,minifyEnabled true


 

buildTypes {
        release {
            minifyEnabled true
            ...
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }



那为什么开启了R8压缩就会导致上面的异常呢,是因为EventBus的注解使用了反射。


反射 (Reflection) 会导致 R8 在跟踪代码时无法识别到代码的入口点。第三方库也可能用到反射,并且由于第三方库实际上是您的应用的一部分,您 (作为应用开发者) 将负责这些库以及您自己的代码中使用的反射。第三方库可能附带了它们自己的规则,但是切记,有些库不一定是为 Android 编写的,抑或是未考虑缩减问题,因此它们可能需要其他配置。


解决方案

明白了前因后果之后,解决方案就应然而生了。


方案一

关闭R8压缩


minifyEnabled false


但是这样会使得你的应用增大不少,比如你使用了某个第三方库时,应用中只使用了其中很小一部分,但打包时所有库代码都会保留在应用中。


如果你不介意的话,这是最简单粗暴的解决方案。


作者:https://blog.csdn.net/yechaoa


方案二

添加相应的压缩/混淆规则,比如EventBus:


-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# And if you use AsyncExecutor:
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}


总结

其实问题并不复杂,关键是解决问题的过程,我想传达以下几点:


遇到问题先不要慌,也不要盲目搜索答案,这样会浪费时间,且可能混淆你的视线

先认真仔细的看一下日志,一般日志都会给你提示

结合一切可用信息,快速准确的定位问题

如果非要搜,建议先去官方文档找答案

我遇到这个异常是怎么解决的:


根据日志提示,我定位到问题是EventBus

查看使用教程,并没有发现什么问题

在EventBus的issues中看看是否有同样的问题,果然是有的,并知道了是R8压缩导致的

去google官网查看R8相关介绍,并知道了原因,且提供了解决方案

举一反三,也适用其他的问题。


如果对你有用或启发的话,点个赞呗 ^ _ ^


参考

目录
相关文章
|
1月前
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
37 5
|
IDE Java 开发工具
Lombok - Generating equals/hashCode implementation but without a call to superclass
Lombok - Generating equals/hashCode implementation but without a call to superclass
1049 0
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
85 0
this和super用法的区别与细节(java继承中this和super的比较)
this和super用法的区别与细节(java继承中this和super的比较)
【Junit 报错】Test class should have exactly one public zero-argument constructor和Test class can only have one constructor
错误1: 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.
6079 0
|
编译器
public <T> T method(T t)方法详解
public <T> T method(T t)方法详解
257 0
public <T> T method(T t)方法详解
Mockito框架学习之stub then return
Mockito框架学习之stub then return
103 0
Mockito框架学习之stub then return
Mockito框架学习 - how does expected annotation work
Mockito框架学习 - how does expected annotation work
Mockito框架学习 - how does expected annotation work
|
Java
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
665 0