Search code examples
androidandroid-fragmentsgreenrobot-eventbus

UI Fragment <-> EventBus <-> Service


I have an interaction between UI Fragment and background Service. I use EventBus. Of course, if Activity is stopped/killed, then there are no subscribers.

Here's the code so you understand:

public class LocationService extends Service {
    //...
    EventBus.getDefault().post(new MessageEventLocationClient(data));
}


public class ClientFragment extends Fragment {
    //...
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEventLocationClient event) {
        // update UI
        textViewLastSentData.setText(event.trackData.lastLatLon());
    }
}

And all OK.

However, here is the report sent to me from Google Play Developer Console:

Devices with errors 14:
Google Nexus 7 (flo) - Android 5.0
Google Nexus 9 (flounder) - Android 5.0
Google Pixel (sailfish) - Android 7.1
Motorola XT1096 (victara) - Android 4.4
...


Exceptions:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.tim4dev.imokhere/com.tim4dev.imokhere.MainActivity}: org.a.a.e:
Subscriber class com.tim4dev.imokhere.c and its super 
classes have no public methods with the @Subscribe annotation
...

What does it mean?

Is this really a problem?

Then what to do?

A similar problem is described here.

UPD. RTM. Thanks to all.


Solution

  • as pointed out, you need to instruct proguard not to remove method annotated with @Subscribe. Proguard will remove them if they are unused and since EventBus will look for them with reflection they will very likely be unused. you can add some directives to your proguard configuration file, from here:

    ## New rules for EventBus 3.0.x ##
    # http://greenrobot.org/eventbus/documentation/proguard/
    
    -keepattributes *Annotation*
    -keepclassmembers class ** {
        @org.greenrobot.eventbus.Subscribe <methods>;
    }
    -keep enum org.greenrobot.eventbus.ThreadMode { *; }
    
    # Only required if you use AsyncExecutor
    -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
        <init>(java.lang.Throwable);
    }