简体   繁体   中英

How to provide Context with Hilt Android?

I am trying to migrate a project to Hilt but facing the below issue, not sure how to pass Context with Hilt. If I remove provideContext method then it complains with the below error:

error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method.

But my understanding is that in Hilt we don't need provideContext method and we could just use @ApplicationContext like below:

@Inject
public CardLayoutManager(@ApplicationContext Context context) {
    mContext = context;
}

Am I missing something?

You need to annotate the constructor properly:

class CardLayoutManager @Inject constructor(@ApplicationContext val context: Context) {
}

Remove the @ApplicationContext annotation

I have read all the documentation more than 5 times, tried the correct method and updated all the Hilt libraries but the problem prevailed.

Once I removed the annotation it all worked perfectly which is strange because I used the correct approach (using the @ApplicationContext annotation) in my other projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM