Search code examples
javaandroidproguardevent-busgreenrobot-eventbus

Android EventBus app crashes in release mode due to no @Subcribe methods


The app works in debug, but not in release

Process: com.rubenwardy.monzolytics, PID: 14943
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rubenwardy.monzolytics/com.rubenwardy.monzolytics.MainActivity}: org.greenrobot.eventbus.EventBusException: Subscriber class com.rubenwardy.monzolytics.MainActivity and its super classes have no public methods with the @Subscribe annotation
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
   at android.app.ActivityThread.access$800(ActivityThread.java:145)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5319)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.rubenwardy.monzolytics.MainActivity and its super classes have no public methods with the @Subscribe annotation
   at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
   at org.greenrobot.eventbus.EventBus.register(EventBus.java:136)
   at com.rubenwardy.monzolytics.MainActivity.onStart(MainActivity.java:88)
   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
   at android.app.Activity.performStart(Activity.java:5992)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404) 
   at android.app.ActivityThread.access$800(ActivityThread.java:145) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5319) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 

Here's my gradle file: https://gist.github.com/rubenwardy/b467d1efd79c671f9a932d98768ff656
Here is my proguard file:

-keep class com.androidplot.** { *; }
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

-keepclassmembers class com.rubenwardy.** { *; }

The error still occurs if I replace proguard with this:

-dontoptimize
-dontshrink
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose

I definitely have @Subscribe functions in MainActivity:

@Subscribe
protected void onPeriodChange(final Events.PeriodChangeRequestedEvent e) {
    Log.e("MAct", "Period " + e.from.toString() + " to " + e.to.toString());
    filter = new TransactionFilter() {
        @Override
        public boolean isAllowed(Transaction transaction) {
            return transaction.created.getTime() > e.from.getTime() &&
                    transaction.created.getTime() < e.to.getTime();
        }
    };

    filterTransactions();
}

I have, ofc, googled this - but can't find any results. Please ask if you need more information.


Solution

  • Turns out the @Subscribe methods need to be public.

    @Subscribe
    public void onPeriodChange(final Events.PeriodChangeRequestedEvent e) {
        Log.e("MAct", "Period " + e.from.toString() + " to " + e.to.toString());
        filter = new TransactionFilter() {
            @Override
            public boolean isAllowed(Transaction transaction) {
                return transaction.created.getTime() > e.from.getTime() &&
                        transaction.created.getTime() < e.to.getTime();
            }
        };
    
        filterTransactions();
    }