Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build failed after adding Hilt components in Android project

I was trying to explore Hilt with simplest possible example, but my application isn't building. I added all the dependencies but when I try to build, it shows an error indicating not finding the hilt gradle plugin. Here are my codes:

build.gradle(project)

    ext.kotlin_version = "1.5.20"
    ext.hilt_version = '2.35'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(app)

plugins{
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.bonny.tutorial.hilttest"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        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'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    //hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Application Class (included in the Manifest too)

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class TextApplication : Application() {
}

Dependency class (MyTexts.kt)

class MyTexts @Inject constructor(){
    val text = "Hello World"
}

MainActivity.kt

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var tv: TextView
    @Inject lateinit var myTexts: MyTexts
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv = findViewById(R.id.myText)
        tv.text = myTexts.text
    }
}

and the error looks like this:

public final class MainActivity extends androidx.appcompat.app.AppCompatActivity {
             ^
  Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
  See https://dagger.dev/hilt/gradle-setup.html
  [Hilt] Processing did not complete. See error above for details.

Please suggest.

like image 500
B.Ahmad Avatar asked Jun 26 '21 12:06

B.Ahmad


People also ask

What are the hilt components in Android?

The Hilt components are the ones responsible for injecting their bindings into your Android classes. Each component is responsible for injecting a different type of Android class. This is shown in the table below: Component. Injector for. ApplicationComponent. Application. ActivityRetainedComponent.

What is a hilt dependency injection library?

Hilt is a new dependency injection library built on top of Dagger that simplifies its use in Android apps. This guide showcases the core functionality with a few code snippets to help you get started with using Hilt in your project. To set up Hilt in your app, follow the Gradle Build Setup guide first.

What is the hilt component hierarchy?

Hilt comes with a built-in set of components (and corresponding scope annotations) that are automatically integrated into the various lifecycles of an Android application. The diagram below shows the standard Hilt component hierarchy.

How do I set up hilt in Android?

To set up Hilt in your app, follow the Gradle Build Setup guide first. After installing all the dependencies and plugins, annotate your Application class with @HiltAndroidApp to use Hilt. You don’t need to do anything else or otherwise directly invoke it.


1 Answers

Kotlin 1.5.20 just came out (24 jun 2021) so there seems to be some compatibility issue. You can bring down to ext.kotlin_version = "1.5.10" in your build.gradle(project)

Also, Update hilt to latest 2.37

project build

classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"

app build:

//Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"
like image 106
Mayur Gajra Avatar answered Oct 20 '22 20:10

Mayur Gajra