简体   繁体   中英

How to provide application class using hilt

I am using a library that needs the Application class (not the application context) that I am using to work

its builder is something like Library.Builder.setApplication(app).setParameters(...).build()

is there a way to use the currently created Application in Hilt? @ApplicationContext obviously does not work

I'm not 100% sure, but maybe you can cast Application Context to Application class. I had similar problem when I needed Activity instance in provides method. I cast Activity Context from Hilt annotations and it works.

Simply inject the Application class:

class MyClass @Inject constructor(
    private val application: Application,
) 

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