Cruces
Cruces

Reputation: 3119

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

Upvotes: 1

Views: 3104

Answers (2)

molundb
molundb

Reputation: 629

Simply inject the Application class:

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

Upvotes: -1

Matija Sokol
Matija Sokol

Reputation: 167

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.

Upvotes: 6

Related Questions