0

I want to get RefWatcher object available throughout my activities and fragments so in my main application dagger module i did

@Provides
@AppScope
static RefWatcher provideRefWatcher(Application application) {
    return ((AppName) application).refWatcher;
}

and in my application class I declared

public class AppName extends DaggerApplication {

public RefWatcher refWatcher;
...
}

However when i inject into activity and use as

public class MainActivity extends DaggerAppCompatActivity {

@Inject
RefWatcher refWatcher;

@Override
public void onDestroy() {
    super.onDestroy();
    refWatcher.watch(this);
}

Iam getting error

Caused by: java.lang.NullPointerException: Cannot return null from a non-@Nullable @Provides method

What is the correct way to inject this leakcanary refwatcher?

Anoop Krishnan
  • 331
  • 3
  • 14

1 Answers1

0

Because refWathcer is null in this context. Try to get it like:

  @Override public void onCreate() {
    super.onCreate();
    refWatcher = LeakCanary.install(this);
  }

in your application class. But generally it's not a good idea using dagger in this case. After you installed LeakCanary you can use static LeakCanary.installedRefWatcher() to get refWatcher instance.

Kroha
  • 168
  • 2
  • 9