Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dagger 2 Dependency not being injected

i'm trying to use Dagger 2 into my apps but i'm having some problems regarding the entities repository and i haven't figured it out what i'm missing.

Here is my Application Component:

@Singleton
@Component(
        modules = {
                AndroidModule.class,
                RepositoryModule.class
        }
)

public interface ApplicationComponent {
    void inject(AndroidApplication app);
    IDependenceyRepository dependencyRepository();
}

My modules:

@Module
public class RepositoryModule {
    @Provides @Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
        return new DependencyRepository(context);
    }
}

@Module
public class AndroidModule {
    private final AndroidApplication app;

    public AndroidModule(AndroidApplication app) {
        this.app = app;
    }

    @Provides @Singleton Context provideApplicationContext() {
        return app;
    }
}

My AndroidApplication:

public class AndroidApplication extends Application {
    private ApplicationComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        setupGraph();
    }

    private void setupGraph() {
        component = DaggerApplicationComponent.builder()
                .androidModule(new AndroidModule(this))
                .repositoryModule(new RepositoryModule())
                .build();
        component.inject(this);
    }

    public ApplicationComponent component() {
        return component;
    }
}

And here is my activity:

public class MainActivity extends Activity {
    @Inject
    IDependenceyRepository repository;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.testRepository();
    }

    private void testRepository() {
        Dependency dep = new Dependency();
        dep.setDescription("XXX");
        dep.setName("AAAA");

        try {
            repository.save(dep);
            Log.d("", repository.queryAll().get(0).getName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

What happens is that i'm getting a null pointer exception into the repository. He is not being injected.

If i add this line though:

repository = ((AndroidApplication) getApplication()).component().dependencyRepository();

It works, but the point of the DI it's not to have to worry about this, or am im wrong about that?

I've tried some other example and tutorials but haven't managed to solve my problem.

Thanks in advance,

like image 215
gmemario Avatar asked Dec 19 '22 02:12

gmemario


1 Answers

david.mihola's comment is correct: in order to be able to have @Inject fields initialized, you need to have a method (typically void inject(MyClass)) in your component.

It's worth noting (not specifically for your question, but it could come up) that if you have a base class:

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((AndroidApplication) getApplication()).component().inject(this);
    }
} 

and in your component:

void inject(BaseActivity);

Injecting into a subclass that has it's own @Inject fields won't work:

public class ActualActivity extends BaseActivity {
    @Inject IDependenceyRepository repo;
}

This is because Dagger needs to know the concrete classes that will be injected at compile time, not runtime (like Dagger 1 could do).

like image 74
rdshapiro Avatar answered Jan 18 '23 23:01

rdshapiro