Dagger/MissingBinding issue, unable to inject in BaseApplication - android

I have implemented Hilt on my app. I am getting this error for the first time. Does anyone have an idea how to go about it?
weatherstackapp/utils/BaseApplication_HiltComponents.java:128: error:
[Dagger/MissingBinding]
com.malinikali.weatherstackapp.utils.BaseApplication cannot be provided
without an #Inject constructor or an #Provides-annotated method.
public abstract static class SingletonC implements
BaseApplication_GeneratedInjector,
^
Below is my BaseApplication class
#HiltAndroidApp
class BaseApplication : Application()
Below is my AppModule class
#Module
#InstallIn(SingletonComponent::class)
object AppModule {
private val client = OkHttpClient.Builder().apply {
addInterceptor(ApiInterceptor())
}.build()
#Singleton
#Provides
fun provideContext(application: BaseApplication): Context {
return application.applicationContext
}
#Provides
fun providesBaseUrl() = Constants.BASE_URL
#Provides
#Singleton
fun provideRetrofitInstance(BASE_URL:String):ApiService =
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(client)
.build()
.create(ApiService::class.java)
}

Hilt injects Application and #ApplicationContext Context by default. If this is the only binding that uses BaseApplication, you can simply change the parameter to one of these default bindings instead:
#Singleton
#Provides
fun provideContext(application: Application): Context {
return application.applicationContext
}
If you really need a BaseApplication binding, you can create a #Provides method based on one of the included bindings:
#Provides
fun provideBaseApplication(application: Application): BaseApplication {
return application as BaseApplication
}

Related

Hilt - app cannot be provided without an #Inject constructor or an #Provides-annotated method

This is my hilt module:
#Module
#InstallIn(SingletonComponent::class)
object AppModule {
#Provides
#Singleton
fun provideDatabase(app: App) =
Room.databaseBuilder(app, AppDatabase::class.java, "app_database").build()
#Provides
fun provideUserDao(database: AppDatabase) = database.userDao()
#Provides
#Singleton
fun provideApi(): ContactsService {
return Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(MoshiConverterFactory.create(
Moshi.Builder().build()))
.build()
.create(ContactsService::class.java)
}
#Provides
#Singleton
fun provideRepository(api:ContactsService,dao: ContactsDao): ContactRepository{
return ContactRepositoryImpl(api,dao)
}
}
This is the indicated App class:
#HiltAndroidApp
class App : Application() {
}
This is the error I get:
..\assignment\App_HiltComponents.java:128: error: [Dagger/MissingBinding] com.example.assignment.App cannot be provided without an #Inject constructor or an #Provides-annotated method.
I double check every class that I use injection and in each of them I used #Inject annotation. I read every stack question but none of them solved my problem.
Hilt is built on top of the DI library Dagger. Dagger works only with a specific type of class.
Hilt provides #ApplicationContext as Context type.
You should provide your App explicitly.
#Provides
fun provideApp(#ApplicationContext context: Context): App = context as App

Dagger/MissingBinding okhttp3.logging.HttpLoggingInterceptor cannot be provided without an #Inject constructor or an #Provides-annotated method

I am new with dagger-hilt. I can't solve this issue.
This is the error I'm facing :
\com\example\news\NewsApplication_HiltComponents.java:129: error: [Dagger/MissingBinding] okhttp3.logging.HttpLoggingInterceptor cannot be provided without an #Inject constructor or an #Provides-annotated method.
public abstract static class SingletonC implements NewsApplication_GeneratedInjector,
^
okhttp3.logging.HttpLoggingInterceptor is injected at
com.example.news.di.NetworkModule.provideOkHttpClient(interceptor)
okhttp3.OkHttpClient is injected at
com.example.news.di.NetworkModule.providesNewsService(�, okHttpClient)
com.example.news.api.NewsService is injected at
com.example.news.data.repository.NewsRepository(newsService)
com.example.news.data.repository.NewsRepository is injected at
com.example.news.presentation.viewmodel.NewsViewModel(newsRepository)
com.example.news.presentation.viewmodel.NewsViewModel is injected at
com.example.news.presentation.viewmodel.NewsViewModel_HiltModules.BindsModule.binds(arg0)
#dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.example.news.NewsApplication_HiltComponents.SingletonC ? com.example.news.NewsApplication_HiltComponents.ActivityRetainedC ? com.example.news.NewsApplication_HiltComponents.ViewModelC]
This is the Network module class:
#InstallIn(SingletonComponent::class)
#Module
object NetworkModule {
#Singleton
#Provides
fun providesRetrofit(): Retrofit.Builder {
return Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
}
#Singleton
#Provides
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
return OkHttpClient.Builder().addInterceptor(interceptor).build()
}
#Singleton
#Provides
fun providesNewsService(retrofitBuilder: Retrofit.Builder, okHttpClient: OkHttpClient): NewsService {
return retrofitBuilder.client(okHttpClient).build().create(NewsService::class.java)
}
}
Need some help on this error. I don't understand why this error is coming.
Issue is resolved:
I was not using interceptor in addInterceptor()
#Singleton
#Provides
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor) : OkHttpClient {
return OkHttpClient.Builder()**.addInterceptor(interceptor).**build()
}

com.google.android.exoplayer2.ExoPlayer cannot be provided without an #Provides-annotated method

I can't understand where is my error, I'm learning hilt and doing music app. In fragment I've coded #AndroidEntryPoint does is the trouble and supposed to be written a ActivityScopped? can some one olny where is my error suggest
i've viewed
error: [Dagger/MissingBinding] androidx.lifecycle.ViewModelProvider.Factory cannot be provided without an #Provides-annotated method
and other discuss but discuss same to me I haven't found
stack trace
D:\NCS\app\build\generated\source\kapt\debug\io\com\ncs\NCSApplication_HiltComponents.java:128: error: [Dagger/MissingBinding] com.google.android.exoplayer2.ExoPlayer cannot be provided without an #Provides-annotated method.
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
^
com.google.android.exoplayer2.ExoPlayer is injected at
io.com.ncs.exoplayer.MusicService.exoPlayer
io.com.ncs.exoplayer.MusicService is injected at
io.com.ncs.exoplayer.MusicService_GeneratedInjector.injectMusicService(io.com.ncs.exoplayer.MusicService) [io.com.ncs.NCSApplication_HiltComponents.SingletonC ? io.com.ncs.NCSApplication_HiltComponents.ServiceC]
application
#HiltAndroidApp
class NCSApplication: Application(){
}
AppModule
#Module
#InstallIn(SingletonComponent::class)
object AppModule {
#Singleton
#Provides
fun provideMusicServiceConnection(
#ApplicationContext context: Context
) = MusicServiceConnection(context)
#Singleton
#Provides
fun provideGlideInstance(#ApplicationContext context: Context) =
Glide.with(context).setDefaultRequestOptions(
RequestOptions()
.placeholder(R.drawable.ic_image)
.error(R.drawable.ic_image)
.diskCacheStrategy(DiskCacheStrategy.DATA)
)
}
Service Module
#Module
#InstallIn(ServiceComponent::class)
object ServiceModule {
#ServiceScoped
#Provides
fun provideMusicDatabase() = MusicDatabase()
#ServiceScoped
#Provides
fun provideAudioAttributes() = AudioAttributes.Builder()
.setContentType(C.CONTENT_TYPE_MUSIC)
.setUsage(C.USAGE_MEDIA)
.build()
#ServiceScoped
#Provides
fun provideExoPlayer(
#ApplicationContext context: Context,
audioAttributes: AudioAttributes
) = ExoPlayer.Builder(context).apply {
setAudioAttributes(audioAttributes,true)
setHandleAudioBecomingNoisy(true)
}
#ServiceScoped
#Provides fun provideSourceFactory(
#ApplicationContext context: Context
) = DefaultDataSourceFactory(context, Util.getUserAgent(context,"waltwe"))
}

Hilt repository injection

I've been trying to resolve my issue but I can't find anything...
MyAppApplication.kt :
#HiltAndroidApp
class MyAppApplication : Application()
MainActivity.kt
#AndroidEntryPoint
class MainActivity: AppCompatActivity()
MyViewModel.kt
#HiltViewModel
class MyViewModel #Inject constructor(private val myRepository: MyRepository,
private val appPreference: AppPreference) : ViewModel(){}
MyRepository.kt
interface MyRepository {
fun test()
}
class MyRepositoryImpl #Inject constructor(private val service:Service): MyRepository {
override fun test() { print("") }
}
HiltModule.kt
#Module
#InstallIn(SingletonComponent::class)
object NetworkModule {
private const val BASE_URL = "..."
#Singleton
#Provides
fun getGsonConverterFactory(): GsonConverterFactory = GsonConverterFactory.create(
GsonBuilder()
.setLenient()
.create()
)
#Singleton
#Provides
fun getRetrofit(gsonConverterFactory: GsonConverterFactory): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(gsonConverterFactory)
.build()
#Singleton
#Provides
fun provideService(retrofitClient: Retrofit) = retrofitClient.create(Service::class.java)
#Singleton
#Provides
fun getAppPreference(application: Application) :AppPreference = AppPreferenceImpl(application)
}
#Module
#InstallIn(ViewModelComponent::class)
object RepositoryModule {
#ViewModelScoped
#Provides
fun getMyRepository(myRepository: MyRepositoryImpl): MyRepository = myRepository
}
failed with 2 errors
[Dagger/MissingBinding] com.example.app.repository.MyRepositoryImpl cannot be provided without an #Inject constructor or an #Provides-annotated method.
com.example.myapp.repository.MyRepositoryImmpl is injected at....
java.lang.reflect.InvocationTargetException (no error message)
Do you have any idea of what I am missing?
Thanks you!
Complementary informations:
I have tried switching ViewModelScope to Singleton.
I have tried switching Object RepositoryModule to
#Module
#InstallIn(ViewModelComponent::class)
abstract class RepositoryModule {
#Binds
#ViewModelScoped
abstract fun bindMyRepository(myRepository: MyRepositoryImpl): MyRepository
}
I have tried having them all inside One HiltModule Object
I have tried
#Module
#InstallIn(SingletonComponent::class)
object RepositoryModule {
#Provides
fun getMyRepository(service: Service): MyRepository = MyRepositoryImpl(service)
}
But same issue mentioned.

Provide application context with dagger 2

Good day all,
I would like to provide application context for my AppModule class.
I would like to have a PrefsHelper be provided through out the application like I do with my ApiService class.
The code for my AppModule:
#Module
#Suppress("unused")
object AppModule {
#Provides
#Reusable
#JvmStatic
internal fun provideApiService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
/**
* Provides the Retrofit object.
* #return the Retrofit object
*/
#Provides
#Reusable
#JvmStatic
internal fun provideRetrofitInterface(): Retrofit {
val interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY
}
val client: OkHttpClient = OkHttpClient.Builder().apply { this.addInterceptor(interceptor) }.build()
return Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(GsonConverterFactory.create())
.build()
}
The way I have seen it done before (In Java) is create a constructor and pass in the application context in that way. Kotlin doesnt allow that with an object
How can I have the context be provided in this class allowing me to provide PrefsHelper?
You could also use the BindsInstance annotation in your AppComponent.
So your AppComponent would look something like this:
#Singleton
#Component(modules = YOUR_MODULES)
interface AppComponent {
//Whatever injections you have
#Component.Builder
interface Builder {
fun build(): AppComponent
#BindsInstance
fun application(Application application): Builder
}
}
Then you just add the new methods to your AppComponent creation in your Application class.
DaggerAppComponent.builder().application(this).build()
Change your AppModule to something like this:
#Module
class AppModule(private val application: Application) {
#Singleton
#Provides
internal fun provideApplication(): Application = application
#Singleton
#Provides
internal fun providePrefs(application: Application): YourPref {
return YourPref(application)
}
}

Categories

Resources