Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

otto eventbus for android behaves differently in release build

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

like image 665
stephanlindauer Avatar asked Nov 24 '14 13:11

stephanlindauer


1 Answers

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 *;
}
like image 158
laalto Avatar answered Oct 15 '22 19:10

laalto