3

I have an issue where one of my subscribed methods does not get called upon a post of the correct event type unless that subscribed method is used (called) elsewhere.

Here is some relevant information about the code:

  • A method of one of my classes is annotated with @Subscribe.
  • By stepping through the code with the debugger, I find that under my specific circumstance, the class has no methods annotated with @Subscribe.
  • Unless I call the method directly at some point in time (doesn't matter when, or even if it actually gets called at runtime) elsewhere, my post does not work.
  • The IDE (Android Studio) notifies me that the "method is never used"

I can certainly call the method in a block of code that I am confident will never fire, but this is obviously terrible practice, and defeats the purpose of this post/subscribe paradigm.

Or I can make the method static, but I'd rather not because I use member variables inside of it.

Any solutions to why this is occuring even though Otto's example uses a similar pattern

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
TeePaps
  • 471
  • 3
  • 17

1 Answers1

7

Turns out it was a ProGuard issue. Fixed it by adding the following lines:

-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}
TeePaps
  • 471
  • 3
  • 17
  • Which file did you add this to? – superuserdo Jan 22 '16 at 04:23
  • This link should help you: http://developer.android.com/tools/help/proguard.html Pretty much, if you don't have a custom proguard file already, you can specify the location of one in your build.gradle file and add these lines to it – TeePaps Jan 23 '16 at 12:52