5

I checked everything from scratch, but I couldn't find the error. I couldn't find the error, what could it be?

I am getting the following compilation error;

HomeViewModel.java:6: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class HomeViewModel extends androidx.lifecycle.ViewModel {
 ^

Manifes File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.movielistandroid">
 <uses-permission android:name="android.permission.INTERNET"/>
 <application
 android:name="com.example.movielistandroid.Application"
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundIcon="@mipmap/ic_launcher_round"
 android:supportsRtl="true"
 android:theme="@style/Theme.MovieListAndroid">
 <activity android:name=".ui.MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <meta-data
 android:name="preloaded_fonts"
 android:resource="@array/preloaded_fonts" />
 </application>
</manifest>

Application.kt

package com.example.movielistandroid
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class Application : Application() {}

AppModule.kt

package com.example.movielistandroid.di
import com.example.movielistandroid.BuildConfig
import com.example.movielistandroid.data.remote.MovieService
import com.example.movielistandroid.data.repositories.MoviesRepository
import com.example.movielistandroid.usecase.repositories.MoviesRepositoryImpl
import com.example.movielistandroid.utils.Constants
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
 @Singleton
 @Provides
 fun provideOkHttpClient(): ...
 @Singleton
 @Provides
 fun provideRetrofit(okHttpClient: OkHttpClient): ...
 @Provides
 @Singleton
 fun provideApiService(retrofit: Retrofit): ...
 @Provides
 @Singleton
 fun provideMoviesRepository(movieService: MovieService): ...
}

MoviesRepository.kt

package com.example.movielistandroid.data.repositories
import MoviesResponseModel
import com.example.movielistandroid.data.remote.MovieService
interface MoviesRepository {
 ...
}

MoviesRepositoryImpl.kt

package com.example.movielistandroid.usecase.repositories
import com.example.movielistandroid.data.remote.MovieService
import com.example.movielistandroid.data.remote.MovieService.Companion.MOVIE_API_KEY
import com.example.movielistandroid.data.repositories.MoviesRepository
import javax.inject.Inject
import javax.inject.Singleton
class MoviesRepositoryImpl
@Inject constructor(
 private val movieService: MovieService
) : MoviesRepository {
 override suspend fun getCurrentPlayingMovies() = movieService.getCurrentPlayingMovies(apiKey = MOVIE_API_KEY)
 override suspend fun getUpComingMovies() = movieService.getUpComingMovies(apiKey = MOVIE_API_KEY);
}

HomeViewModel.kt

package com.example.movielistandroid.ui.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.example.movielistandroid.data.repositories.MoviesRepository
import com.example.movielistandroid.utils.Resource
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject
class HomeViewModel
@Inject constructor(
 private val moviesRepository: MoviesRepository
) : ViewModel() {
 fun getUpComingMovies() = liveData(Dispatchers.IO) {
 ...
 }
 fun getCurrentPlayingMovies() = liveData(Dispatchers.IO) {
 ...
 }
}

HomeViewModel.java (generated)

package com.example.movielistandroid.ui.home;
import java.lang.System;
@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\"\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u00002\u00020\u0001B\u000f\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004J\u0012\u0010\u0005\u001a\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00020\b0\u00070\u0006J\u0012\u0010\t\u001a\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00020\b0\u00070\u0006R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004\u00a2\u0006\u0002\n\u0000\u00a8\u0006\n"}, d2 = {"Lcom/example/movielistandroid/ui/home/HomeViewModel;", "Landroidx/lifecycle/ViewModel;", "moviesRepository", "Lcom/example/movielistandroid/data/repositories/MoviesRepository;", "(Lcom/example/movielistandroid/data/repositories/MoviesRepository;)V", "getCurrentPlayingMovies", "Landroidx/lifecycle/LiveData;", "Lcom/example/movielistandroid/utils/Resource;", "LMoviesResponseModel;", "getUpComingMovies", "app_debug"})
public final class HomeViewModel extends androidx.lifecycle.ViewModel {
 private final com.example.movielistandroid.data.repositories.MoviesRepository moviesRepository = null;
 
 @javax.inject.Inject()
 public HomeViewModel(@org.jetbrains.annotations.NotNull()
 com.example.movielistandroid.data.repositories.MoviesRepository moviesRepository) {
 super();
 }
 
 @org.jetbrains.annotations.NotNull()
 public final androidx.lifecycle.LiveData<com.example.movielistandroid.utils.Resource<MoviesResponseModel>> getUpComingMovies() {
 return null;
 }
 
 @org.jetbrains.annotations.NotNull()
 public final androidx.lifecycle.LiveData<com.example.movielistandroid.utils.Resource<MoviesResponseModel>> getCurrentPlayingMovies() {
 return null;
 }
}

build.gradle (app)

plugins {
 id 'com.android.application'
 id 'kotlin-android'
 id 'kotlin-android-extensions'
 id 'androidx.navigation.safeargs.kotlin'
 id 'kotlin-kapt'
 id 'dagger.hilt.android.plugin'
}
android {
 compileSdkVersion 30
 buildToolsVersion "30.0.3"
 defaultConfig {
 applicationId "com.example.movielistandroid"
 minSdkVersion 19
 targetSdkVersion 30
 versionCode 1
 versionName "1.0"
 multiDexEnabled true
 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
 }
 }
 compileOptions {
 sourceCompatibility JavaVersion.VERSION_1_8
 targetCompatibility JavaVersion.VERSION_1_8
 }
 kotlinOptions {
 jvmTarget = '1.8'
 }
 buildFeatures {
 viewBinding true
 }
}
dependencies {
 def nav_version = "2.3.5"
 def retrofit_version = "2.9.0"
 def glide_version = "4.12.0"
 implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
 implementation 'androidx.core:core-ktx:1.6.0'
 implementation 'androidx.appcompat:appcompat:1.3.1'
 implementation 'com.google.android.material:material:1.4.0'
 implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
 implementation 'androidx.legacy:legacy-support-v4:1.0.0'
 implementation 'androidx.recyclerview:recyclerview:1.2.1'
 implementation 'androidx.multidex:multidex:2.0.1'
 implementation 'androidx.fragment:fragment-ktx:1.3.6'
 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
 implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
 implementation 'androidx.lifecycle:lifecycle-common:2.3.1'
 implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
 implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
 implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
 implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
 implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
 implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
 implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
 implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
 implementation 'com.google.code.gson:gson:2.8.6'
 implementation 'com.squareup.okhttp3:logging-interceptor:4.5.0'
 implementation "com.github.bumptech.glide:glide:$glide_version"
 annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
 implementation "com.google.dagger:hilt-android:2.38.1"
 kapt "com.google.dagger:hilt-compiler:2.38.1"
 testImplementation 'junit:junit:4.13.2'
 androidTestImplementation 'androidx.test.ext:junit:1.1.3'
 androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
 ext.kotlin_version = "1.5.21"
 repositories {
 google()
 mavenCentral()
 }
 dependencies {
 def nav_version = "2.3.5"
 classpath "com.android.tools.build:gradle:4.2.2"
 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
 classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
 }
}
allprojects {
 repositories {
 google()
 mavenCentral()
 }
}
task clean(type: Delete) {
 delete rootProject.buildDir
}
asked Aug 15, 2021 at 13:18
3
  • 1
    You forgot to add @HiltViewModel to your ViewModel class dagger.dev/hilt/view-model.html Commented Aug 15, 2021 at 14:07
  • Yes, just add this Annotation @HiltViewModel on top of ViewModel class Commented Aug 15, 2021 at 14:55
  • I added '@HiltViewModel' annotation but I have still this error. @IR42 Commented Aug 15, 2021 at 18:08

2 Answers 2

1

You need to add @HiltViewModel in your vm,if you using hilt vm factory to inject vm in your activity/fragment.

Also, you can just bind your MoviesRepositoryImpl with MoviesRepository Instead of writing a provider for it, you are already using constructor injection for MoviesRepositoryImpl

How about -

@Module
@InstallIn(SingletonComponent::class)
interface MoviesRepositoryImplModule {
 @Binds
 fun bindMoviesRepositoryImpl(movieRepoImp: MoviesRepositoryImpl): MoviesRepository
}

and remove

 @Provides
 @Singleton
 fun provideMoviesRepository(movieService: MovieService): MoviesRepository = MoviesRepositoryImpl(movieService)

from AppModule

answered Aug 15, 2021 at 23:48
Sign up to request clarification or add additional context in comments.

6 Comments

I did what you wrote but the problem persists. Same error. :(
Can you add, code snippet, how are you injecting VM in activity/fragment? And also can you try running app with option --stracktrace in compiler option for more detailed error logs.
I found the problem with --stacktrace compiler option. My problem is not about the dagger hilt. I was getting this problem because the model class I was creating didn't have a "package" prefix.
@SalihCan thanks for your info!! I literally go and check one-by-one about the package thing and it resolved!. Dagger-hilt should be improved their error prompting skill omg!
Thanks @SalihCan for sharing. I faced the same error
|
0

My problem was different from OP, but I had the same error message and it was caused because I forgot to use kapt for Hilt compile dependencies.

changed from:

implementation 'com.google.dagger:hilt-android-compiler:2.42'

to:

kapt 'com.google.dagger:hilt-android-compiler:2.42'
answered Jun 11, 2023 at 0:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.