Dagger: How to use Dagger2 in library project?

Created on 13 Oct 2016  路  4Comments  路  Source: google/dagger

Our projects (phone, tablet) need share presenters and components in library project, but Application class in application project, so the DaggerApplicationComponent cannot generate. have any solution? Thanks

Most helpful comment

I have implemented dagger 2 in my library project and when i am using this library in my main project then i am facing cannot resolve symbol DaggerApplicationComponent issue.

I tried the above solution but i didn't know that how can i use another component in main project. So, can you please explain in brief. cc @zeshaoaaa @trevjonez @JakeWharton

All 4 comments

If you have items in the library that depend on details of the component you can define a type of base interface but not annotate it. Then in the application code create another component and annotate that.

so you have in the library:

/** some javadoc explaining how to extend this and what modules to include */
public interface AppComponentBase { ... }

and in the application you have:

@Component(modules = {...})
public interface AppComponent extends AppComponentBase {...} 

By doing this you defer the generation of the component until the application is being built and dagger has the full graph available as a result.

I have implemented dagger 2 in my library project and when i am using this library in my main project then i am facing cannot resolve symbol DaggerApplicationComponent issue.

I tried the above solution but i didn't know that how can i use another component in main project. So, can you please explain in brief. cc @zeshaoaaa @trevjonez @JakeWharton

Experiencing something similar to @GarimaMathur07's issue...

I have implemented dagger 2 in my library project and when i am using this library in my main project then i am facing cannot resolve symbol DaggerApplicationComponent issue.

I've created a trivial android app to demo this issue. Interestingly enough, Gradle is perfectly capable of processing the annotations and adding the generated files to the class path. It seems in my case that Android Studio fails to index those generated files, resulting in the "Cannot resolve symbol" error.

I've submitted a ticket to Android Studio which can be tracked here.

What @trevjonez mentioned is correct. You can have a PhoneApplication and TabletApplication, each with their own component that extends AppComponentBase.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mskx42 picture mskx42  路  3Comments

matpag picture matpag  路  3Comments

Axrorxoja picture Axrorxoja  路  3Comments

SAGARSURI picture SAGARSURI  路  3Comments

SteinerOk picture SteinerOk  路  3Comments