Search code examples
androidandroid-studiokotlindependency-injectiondagger-hilt

Execution failed for task ':app:kaptDebugKotlin' - Error Occurs while using Hilt


Added hilt dependencies:

Build.gradle(project)

def hilt_version = "2.38.1"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"

Build.gradle(app)

plugins {
 id 'dagger.hilt.android.plugin'
 ....
 }

....

dependencies {
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"
 .......
 }

I also have a global application class:

MyApplication.kt

@HiltAndroidApp
class MyApplication : Application()

In my manifest:

Manifest.xml

<application
    android:allowBackup="true"
    android:name=".global.MyApplication"/>

Now, I create a module

NetworkModule.kt

@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {

}

I get error when I run my code:

**Execution failed for task ':app:kaptDebugKotlin'.

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)**

When I remove these: @Module() and @InstallIn()

The error goes away...

what could be the problem? The error shown to me is not informative at all.


Solution

  • So, it appears there is an issue integrating Hilt while targeting version 31 (Android 12).

    When I had:

    compileSdkVersion 31
    buildToolsVersion "31.0.0"
    
    defaultConfig {
       minSdkVersion 21
       targetSdkVersion 31
    }
    

    The error appears...

    but when I changed to:

    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    
    defaultConfig {
       minSdkVersion 21
       targetSdkVersion 30
    }
    

    It starts working, without that error..

    Something wrong with integrating Hilt while targeting android 12 (likely)