Home > OS >  Error: 'Unable to load class AndroidComponentsExtension' when building app with Hilt
Error: 'Unable to load class AndroidComponentsExtension' when building app with Hilt

Time:09-12

Trying to add Hilt to my project but sync fails. My app gradle contains these lines:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

the last one triggers the error "Unable to load com.android.build.api.extension.AndroidComponentsExtension". Full trace here: https://pastebin.com/dZFg7yyX

Kotlin version is: '1.6.10' Here the dependencies in the root gradle script:

dependencies {
    classpath 'com.android.tools.build:gradle:7.2.1'
    classpath 'io.realm:realm-gradle-plugin:7.0.8'
    classpath 'com.google.gms:google-services:4.3.8'
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.huawei.agconnect:agcp:1.7.0.300'
    classpath "com.google.dagger:hilt-android-gradle-plugin:2.35"
}

and in my app gradle I have:

implementation 'com.google.dagger:hilt-android:2.35'
kapt 'com.google.dagger:hilt-android-compiler:2.35'

Any hint?

CodePudding user response:

Updating your hilt seems to have resolved this issue.

This guide explains the basic concepts of Hilt and its generated containers.

Adding dependencies

First, add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:

buildscript {
    ...
    dependencies {
        ...
        classpath("com.google.dagger:hilt-android-gradle-plugin:2.38.1")
    }
}

Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file:

plugins {
    kotlin("kapt")
    id("dagger.hilt.android.plugin")
}

android {
    ...
}

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

// Allow references to generated code
kapt {
 correctErrorTypes = true
}

Hilt uses Java 8 features. To enable Java 8 in your project, add the following to the app/build.gradle file:

android {
    ...
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
  • Related