greenrobot/EventBus · error · IllegalStateException

Unexpected exception

Error message

Unexpected exception

What it means

Thrown as IllegalStateException from invokeSubscriber(Subscription, Object) when Method.invoke fails with IllegalAccessException. This means reflection could not access the @Subscribe method: the method is not public (or the class is not accessible) at runtime even though it was discovered/registered. InvocationTargetException (the subscriber's own exception) is handled separately by handleSubscriberException; IllegalAccessException is purely an access problem and is treated as an unexpected, non-recoverable state.

Source

Thrown at EventBus/src/org/greenrobot/eventbus/EventBus.java:521

     * subscriber unregistered. This is particularly important for main thread delivery and registrations bound to the
     * live cycle of an Activity or Fragment.
     */
    void invokeSubscriber(PendingPost pendingPost) {
        Object event = pendingPost.event;
        Subscription subscription = pendingPost.subscription;
        PendingPost.releasePendingPost(pendingPost);
        if (subscription.active) {
            invokeSubscriber(subscription, event);
        }
    }

    void invokeSubscriber(Subscription subscription, Object event) {
        try {
            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
        } catch (InvocationTargetException e) {
            handleSubscriberException(subscription, event, e.getCause());
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Unexpected exception", e);
        }
    }

    private void handleSubscriberException(Subscription subscription, Object event, Throwable cause) {
        if (event instanceof SubscriberExceptionEvent) {
            if (logSubscriberExceptions) {
                // Don't send another SubscriberExceptionEvent to avoid infinite event recursion, just log
                logger.log(Level.SEVERE, "SubscriberExceptionEvent subscriber " + subscription.subscriber.getClass()
                        + " threw an exception", cause);
                SubscriberExceptionEvent exEvent = (SubscriberExceptionEvent) event;
                logger.log(Level.SEVERE, "Initial event " + exEvent.causingEvent + " caused exception in "
                        + exEvent.causingSubscriber, exEvent.throwable);
            }
        } else {
            if (throwSubscriberException) {
                throw new EventBusException("Invoking subscriber failed", cause);
            }
            if (logSubscriberExceptions) {

View on GitHub (pinned to 0194926b3b)

Solutions

  1. Add the standard EventBus ProGuard rules: -keepattributes *Annotation*,Signature and -keepclassmembers class * { @org.greenrobot.eventbus.Subscribe <methods>; }
  2. Check the stack trace for the method named in the reflection frame and verify it is public and its class is public at runtime
  3. Disable -allowaccessmodification or add explicit -keep for the affected subscriber classes, then rebuild the release variant
  4. If using a custom subscriber index, ensure the index classes are kept too

Example fix

# before (proguard-rules.pro has no EventBus rules)
# -> release build throws IllegalStateException: Unexpected exception

# after (proguard-rules.pro)
-keepattributes *Annotation*,Signature
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// At startup, verify annotated subscriber methods are reflectively accessible
for (Method m : subscriberClass.getDeclaredMethods()) {
    if (m.isAnnotationPresent(Subscribe.class)
            && !Modifier.isPublic(m.getModifiers())) {
        throw new IllegalStateException("Non-public @Subscribe method: " + m);
    }
}

Try / catch

try {
    EventBus.getDefault().post(event);
} catch (IllegalStateException e) {
    if ("Unexpected exception".equals(e.getMessage()) && e.getCause() instanceof IllegalAccessException) {
        // access problem: report ProGuard/visibility misconfiguration
        crashReporter.report(new IllegalStateException("Subscriber method not accessible (ProGuard?)", e));
        return;
    }
    throw e;
}

Prevention

When it happens

Trigger: A @Subscribe method that was public at compile time but got shrunk/obfuscated to package-private at runtime (aggressive ProGuard/R8 optimization like -allowaccessmodification combined with missing keep rules); a subscriber class loaded by a different classloader making its package inaccessible; runtime instrumentation altering modifiers.

Common situations: Release builds of Android apps with ProGuard/R8 stripping or renaming @Subscribe methods without the standard EventBus keep rules; modularized apps with non-exported packages; bytecode transformers (Jacoco offline-instrumentation edge cases, aspect agents) changing visibility.

Related errors


AI-assisted analysis of greenrobot/EventBus@0194926b3b (2026-08-14). Data as JSON: /api/errors/b2e04be7bd4e447e. Report an issue: GitHub.