简体   繁体   中英

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

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 *;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM