[Thoughts to solve the problem] its super classes have no public methods with the @Subscribe annotation

Playing releasepack when it came to this issue, it is relatively common, remember this 解决思路.

This article introduces the reasons for this problem, as well as when 我的建议and when the problem occurs 我的思路, you can directly look at it if you are anxious 解决方案.

abnormal

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

Don't panic when you encounter an exception. In fact, the prompt is obvious. The specific is specified class, and it is clearly told that this class has no @Subscribeannotations.

the reason

But in fact, after knowing the problem, there may still be doubts, such as me, because my class is @Subscribeannotated, so why?

Knowing the specific class, but also know @Subscribenotes, so it is easy to locate to being EventBus.

Then go to EventBusthe official website to find the answer, and she issueshad the same problem on the first page, the author team of such a reply:

Check your R8 / ProGuard rules.
https://github.com/greenrobot/EventBus#r8-proguard

Check your compression/obfuscation rules.

Only then did I realize that it was R8caused by turning on compression,minifyEnabled true

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

Then why the R8above exception will be caused when compression is turned on , because EventBusthe annotation uses reflection.

Reflection will cause R8 to fail to recognize the entry point of the code when tracking the code. Third-party libraries may also use reflection, and since third-party libraries are actually part of your application, you (as the application developer) will be responsible for reflection used in these libraries and your own code. Third-party libraries may come with their own rules, but keep in mind that some libraries are not necessarily written for Android, or reduction issues are not considered, so they may require additional configuration.

solution

After understanding the cause and effect, the solution should be born.

Option One

Turn off R8compression

minifyEnabled false

But this will increase your application a lot. For example, when you use a third-party library, only a small part of it is used in the application, but all library code will remain in the application when packaging.

If you don't mind, this is the best 简单粗暴solution.

Author: https: //blog.csdn.net/yechaoa

Option II

Add corresponding 压缩/混淆rules, such as 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);
}

to sum up

In fact, the problem is not complicated. The key is the process of solving the problem. I want to convey the following points:

  1. Don’t panic when you encounter a problem, and don’t blindly search for the answer. This will waste time and may confuse your sight
  2. Look at it carefully 日志, the general log will give you a hint
  3. Combine all available information to locate problems quickly and accurately
  4. If you have to search, it is recommended to 官方文档find the answer first

How did I solve this exception:

  1. According to the log prompt, I located the problemEventBus
  2. Check the tutorial, and found no problems
  3. Check the EventBus issuesto see if there is the same problem, it is true, and I know the R8压缩cause
  4. Go to google官网check the R8relevant introduction, know the reason, and provide a solution

By analogy, other questions also apply.

If it is useful or enlightening to you, please give me a thumbs up ^ _ ^

reference

Guess you like

Origin blog.csdn.net/yechaoa/article/details/112696378