Eventbus: Illegal onEvent method - ProGuard

Created on 3 Dec 2014  Â·  11Comments  Â·  Source: greenrobot/EventBus

I am receiving this error:

Illegal onEvent method, check for typos

The error appears to be caused by a method name changing in my compile process by ProGuard. Here is a sample of the log I created to test:

methodName=onEvent
modifierString=
methodName=onEvent$3884d48a
modifierString=$3884d48a
skipMethodVerificationForClasses={}

The solution is to check for the $ character at the start of the modifierString.

This diff solves the issue:

@@ -91,7 +91,7 @@ class SubscriberMethodFinder {
                             } else if (modifierString.equals("Async")) {
                                 threadMode = ThreadMode.Async;
                             } else {
-                                if (skipMethodVerificationForClasses.containsKey(clazz)) {
+                                if (skipMethodVerificationForClasses.containsKey(clazz) || modifierString.startsWith("$")) {
                                     continue;
                                 } else {
                                     throw new EventBusException("Illegal onEvent method, check for typos: " + method);

Most helpful comment

I had an similar issue and I contacted the creator of ProGuard/DexGuard and he sent me a 'workaround' for this.

Just add:
-keepclassmembers,includedescriptorclasses class ** { public void onEvent*(**); }

To your config file to prevent ProGuard/DexGuard's optimization step to add a suffix to the method name.

All 11 comments

Interesting. Can you detail how you got ProGuard to have such names? What was the original method name of the offending method?

Honestly, no. I just checked my ProGuard mapping file. The method signature
does not even exist in the project.
On Wed Dec 03 2014 at 4:12:21 PM greenrobot [email protected]
wrote:

Interesting. Can you detail how you got ProGuard to have such names?

—
Reply to this email directly or view it on GitHub
https://github.com/greenrobot/EventBus/issues/133#issuecomment-65423117.

Funny, which ProGuard version are you using?

Version 4.7

On Wed Dec 03 2014 at 4:18:40 PM greenrobot [email protected]
wrote:

Funny, which ProGuard version are you using?

—
Reply to this email directly or view it on GitHub
https://github.com/greenrobot/EventBus/issues/133#issuecomment-65424527.

I have a issue with pro guard, optimizing this class, both versions 5.1 and 4.7. If you turn on proguards "-optimizations code/removal/advanced: flag (Which you need to make -assumenosideeffects work so you can remove logging from your code) proguard removes the addition of the subscriberMethod in SubscriberMethodFinder.

IE: it turns this

subscriberMethods.add(new SubscriberMethod(method, threadMode, eventType));

into this

new SubscriberMethod(localMethod, localThreadMode, localClass);

So the eventBus never finds any onEvent methods. This would seem to be a problem in proguard.

I have the same problem, now I solved it by this:
change onEvent(SOMETHING EXTENDS OBJECT) to onEvent(android.os.Message )

the version of proguard is 5.1;

here is the error stack:
12-19 07:16:24.832 4126-4126/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
de.greenrobot.event.EventBusException: Illegal onEvent method, check for typos: public void com.MYPACKAGE.c.a.onEvent$4b6f2907(com.b.a.b.c)
at de.greenrobot.event.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:95)
at de.greenrobot.event.EventBus.register(EventBus.java:163)
at de.greenrobot.event.EventBus.register(EventBus.java:133)
at com.MYPACKAGE.c.a.onResume(BaseFragment.java:88)

and in the mapping file:
65:72:void onEvent$4b6f2907(com.google.gson.internal.ConstructorConstructor$1) -> onEvent$4b6f2907

We met the same issue, and we searched the mapping.txt and found one line like this:

448:462:void onEvent$6f60fb76(android.support.v7.widget.RecyclerView$SmoothScroller$Action) -> onEvent$6f60fb76

RecyclerView introduce a method named onEvent$6f60fb76 which breaks SubscriberMethodFinder.findSubscriberMethods. @DavidEdwards 's way is a good way to fix it.

I had an similar issue and I contacted the creator of ProGuard/DexGuard and he sent me a 'workaround' for this.

Just add:
-keepclassmembers,includedescriptorclasses class ** { public void onEvent*(**); }

To your config file to prevent ProGuard/DexGuard's optimization step to add a suffix to the method name.

@DavidEdwards no, you cannot fix this issus by this. you just hide the bug, the method onEvent$() won't execute in logic if fix by this! If I wrote an onEvent() but it won't execute after proguard, this not what I want.
and final my solution is:

modifierString.equals("MainThread") --> modifierString.startWith("MainThread")
...
modifierString.equals("BackgroundThread") --> modifierString.startWith("BackgroundThread")
...
modifierString.equals("Async") --> modifierString.startWith("Async")

@sdeff Your answer should be the right solution.

Stumbled upon this recently. This should be in the wiki of the project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cckroets picture cckroets  Â·  16Comments

gsteigert picture gsteigert  Â·  5Comments

fmweigl picture fmweigl  Â·  13Comments

liaohuyu picture liaohuyu  Â·  4Comments

veeti picture veeti  Â·  3Comments