I want to learn clean architecture and I'm using hilt for di. Once I had java.lang.RuntimeException:

 Unable to instantiate application com.example.cleanarchitecture.MainActivity package com.example.cleanarchitecture: java.lang.ClassCastException: com.example.cleanarchitecture.MainActivity cannot be cast to android.app.Application

error I've changed SingletonComponent to ActivityComponent now I have the error that I mention at title in my application.

My module is like that;

@Module
@InstallIn(ActivityComponent::class)
class ProfileDetailModule {

    @Singleton
    @Provides
    fun providesProfileDetailRepository(dataSource: ProfileDataSource): ProfileRepository {

        return ProfileRepositoryImpl(dataSource)
    }

    @Singleton
    @Provides
    fun providesProfileDataSource(profileApiService: ProfileApiService): ProfileDataSource {

        return ProfileDataSource(profileApiService)
    }

    @Singleton
    @Provides
    fun providesProfileDetailApiService(retrofit: Retrofit): ProfileApiService =
        retrofit.create(ProfileApiService::class.java)

    @Module
    @InstallIn(ActivityComponent::class)
    object AppModule {


        @Provides
            fun provideApplicationContext(@ActivityContext activity: Activity): Activity {
            return activity
        }
    }




}

and my repository is ;

interface ProfileRepository {

    suspend fun getUser(url:String): ProfileResponse
}

and this is my domain class;


class GetUserProfile @Inject constructor(
    private val profileRepository: ProfileRepository
): MutableLiveData<ProfileResponse>(){

    companion object {
        private const val URL =
            "https://jsonplaceholder.typicode.com/users/1"
    }

    data class Params(
        val id: Int,
        val name: String,
        val username: String,
        val email: String,
        val phone: String,
    )


     fun execute(
        viewModel: ProfileDetailViewModel,
        input: Params?
    ): MutableLiveData<ProfileResponse> {
        return MutableLiveData<ProfileResponse>().apply {
            input?.let {
                viewModel.viewModelScope.launch {
                    value =
                        profileRepository.getUser(URL+it.id+it.name+it.username+it.email+it.phone)
                }
            }
        }
    }
}

Want to learn clean architecture so I have many question marks in my head.

Looked that question and cant helped me ; question

1

There are 1 best solutions below

2
Mafaz Ahsan On

Can you check by removing AppModule at the last of your file. For the app, AppModule has to be singleton as it's one for the entire app container.