Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 1 to 2 migration - Members injection methods may only return the injected type or void

Tags:

dagger-2

Im trying to migrate our current system from dagger 1 to 2 and I been stuck for half a day on this. I don't think I'm understanding this well.

Here is my module:

public class BaseModule {

private final Context context;
private final SharedPreferences rawSharedPreferences;

public BaseModule(
        Context context,
        @Named("RawPreferences") SharedPreferences rawSharedPreferences
        ) {
    this.context = context;
    this.rawSharedPreferences = rawSharedPreferences;
}

@Provides
@Singleton
public Context provideContext() {
    return context;
}

@Provides
@Singleton
public DevicePlatform provideDevicePlatform(AndroidDevicePlatform devicePlatform) {
    return devicePlatform;
}

@Provides
@Named("RawPreferences")
@Singleton
public SharedPreferences provideRawSharedPreferences() {
    return rawSharedPreferences;
}

@Provides
@Named("RawPreferencesStore")
@Singleton
public SharedPreferencesStore provideRawSharedPreferencesStore(
        @Named("RawPreferences") SharedPreferences sharedPreferences) {
    return new AndroidSharedPreferencesStore(sharedPreferences);
}

And my component:

@Singleton
@Component(
    modules = {BaseModule.class}
)
public interface BaseComponent {
    void inject (DefaultClientController defaultClientController);
    void inject (StatisticsProvider statisticsProvider);


    Context provideContext();

    AndroidDevicePlatform provideDevicePlatform(AndroidDevicePlatform devicePlatform);

    SharedPreferences provideRawSharedPreferences();

    SharedPreferencesStore provideRawSharedPreferencesStore(
            @Named("RawPreferences") SharedPreferences sharedPreferences);
}

I keep getting this error in provideRawSharedPreferencesStore when I run it:

Error:(168, 28) error: Members injection methods may only return the injected type or void.

I dont understand why. Can someone please help me out. Thanks!

like image 901
Juan Acevedo Avatar asked May 10 '16 21:05

Juan Acevedo


1 Answers

A component can contain 3 types of methods:

  1. inject something into some object, which is the error you see. Those methods usually return void, but you can just return the same object, if you try to have something like a builder.

    MyInjectedObject inject(MyInjectedObject object); // or
    void inject(MyInjectedObject object);
    
  2. Subcomponents, for which you would include the needed modules as parameters (if they require initialization)

    MySubcomponent plus(MyModuleA module);
    
  3. and basically just "getters" or correctly called provision methods to expose objects, to manually get them from the component, and to your subcomponents

    MyExposedThing getMything();
    

Which one of those is this?

// the line you get your error:
SharedPreferencesStore provideRawSharedPreferencesStore(
        @Named("RawPreferences") SharedPreferences sharedPreferences);

You are already providing the SharedPreferencesStore from your module. There you also declare its dependency on RawPreferences: SharedPreferences. You do not have to do this again in your component.

It seems you just try to make the SharedPreferencesStore accessible, as described in 3.. If you just depend on it within the same scope / component, you could just remove the whole component. If you need the getter, you should just remove the parameter. Your Module knows how to create it.

SharedPreferencesStore provideRawSharedPreferencesStore(); // should work.
like image 73
David Medenjak Avatar answered Nov 18 '22 04:11

David Medenjak