Home > OS >  Unable to build when Added new Activity in project having Hilt - Android
Unable to build when Added new Activity in project having Hilt - Android

Time:08-20

In my project, using dagger-Hilt

just by adding a new activity, it shows an error

:app:kaptDebugKotlin
    [Hilt] 
    java.lang.reflect.InvocationTargetException (no error message)

but before adding a new activity, the whole project works fine

Activity :->


@AndroidEntryPoint
class SplashScreen : AppCompatActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)
    }
}

my build.gradle

 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
 classpath("com.google.dagger:hilt-android-gradle-plugin:2.38.1")

CodePudding user response:

I have reproduced your issue. The real problem is with versions,your kotlin version is not compatible with hilt plugin version and hilt library version. Here are the few changes you need to do.

In project build.gradle, update hilt plugin version to 2.42

    classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")

In app build.gradle, update to latest versions like below.

implementation("com.google.dagger:hilt-android:2.42")
kapt("com.google.dagger:hilt-android-compiler:2.42")
kapt "androidx.hilt:hilt-compiler:1.0.0"

Hilt lifecycle module not required in latest hilt version. You should remove the below line in build.gradle and remove all androidx.hilt.lifecycle.ViewModelInject imports in your project.Other wise it will throw error

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
  • Related