otto eventbus for android behaves differently in release build
Asked Answered
A

1

6

i have a singleton service class that pulls data from a server on a set schedule. as soon as the client has received the data, i trigger bus.post(new NewServerResponseEvent()); (http://square.github.io/otto/)

then in my fragments i do this:

@Override
public void onResume() {
    super.onResume();
    eventBus.register(this);
}

@Override
public void onPause() {
    super.onPause();
    eventBus.unregister(this);
}

@Subscribe
public void handleNewServerData(NewServerResponseEvent e) {
    refreshView();
}

everything works very smoothly as long as i just run it while developing on my testing device. as soon as i build a release version and put that into the play store, that handleNewServerData() function is never called.

i can't make sense out of this. what differnce does it make to run that whole thing as a release build? is there maybe stuff happening in another thread that cant post to my subscriber?

can someone point me into the right direction?

thanks in advance

Anthropogenesis answered 24/11, 2014 at 13:31 Comment(0)
T
16

Chances are that your release build is run through ProGuard and it deduces that since the subscriber methods are not directly called, they can be safely removed as unused code. Otto invokes the methods via reflection and ProGuard cannot see that.

Add the following to your proguard config file to keep methods annotated with @Subscribe or @Produce:

-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}
Twomey answered 24/11, 2014 at 13:42 Comment(1)
omg, they even mention that on their website... i completely skipped over that several times it seems. thanks! ;)Anthropogenesis

© 2022 - 2024 — McMap. All rights reserved.