Search code examples
androidkotlindagger-hilt

ClassNotFoundException Error When Integrating Hilt in Android Application


I am trying to integrate Hilt for dependency injection. I have added all the dependencies, but I am getting the following error in the log:

java.lang.RuntimeException: Unable to instantiate application com.example.stockmarket.App package com.example.stockmarket: java.lang.ClassNotFoundException: Didn't find class "com.example.stockmarket.App"

enter image description here

libs.versions.toml

[versions]
agp = "8.4.1"
kotlin = "2.0.0"
hilt = "2.44"
hilt-compile = "1.2.0"
hilt_lifecycle_viewmodel = "1.0.0-alpha03"
hilt_navigation_compose = "1.2.0"

[libraries]
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" }
hilt-lifecycle-viewmodel = { group = "androidx.hilt", name = "hilt-lifecycle-viewmodel", version.ref = "hilt_lifecycle_viewmodel" }
hilt-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hilt-compile" }
hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "hilt_navigation_compose" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinAndroidKsp = { id = "com.google.devtools.ksp", version.ref ="ksp" }
hiltAndroid = { id = "com.google.dagger.hilt.android", version.ref ="hilt" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

build.gradle (App)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.kotlinAndroidKsp)
    alias(libs.plugins.hiltAndroid)
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.kotlin.serialization)
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.room)
    implementation(libs.androidx.room.ktx)
    implementation(libs.retrofit)
    implementation(libs.retrofit.converster)
    implementation(libs.okhttp)
    implementation(libs.okhttp.logging)
    implementation(libs.opencsv)
    implementation(libs.lifecycle.viewmodel.compose)
    implementation(libs.material.icons.extended)
    implementation(libs.accompanist.swiperefresh)
    implementation(libs.hilt.android)
    implementation(libs.navigation.compose)
    implementation(libs.kotlinx.serialization.json)
    ksp(libs.hilt.android.compiler)
    ksp(libs.hilt.compiler)
    implementation(libs.hilt.lifecycle.viewmodel)
    implementation(libs.hilt.navigation.compose)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

build.gradle (Project)

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    alias(libs.plugins.hiltAndroid) apply false
    alias(libs.plugins.kotlinAndroidKsp) apply false
    alias(libs.plugins.compose.compiler) apply false

}

Application class

@HiltAndroidApp
class App: Application()

Manifest file

    <application
        tools:replace="theme,label,allowBackup,icon"
        android:name=".App"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:requestLegacyExternalStorage="true"
        android:supportsRtl="true"
        android:theme="@style/Theme.StockMarket"
        android:usesCleartextTraffic="true"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.StockMarket">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Tried with latest hilt version, not helping


Solution

  • You use the heavily outdated hilt version 2.44. Upgrade it to the current version (as of the time of writing that is 2.51.1).

    Now you can weed out the artifacts you do not need anymore. In the version catalog remove these:

    • hilt-lifecycle-viewmodel
    • hilt-compiler

    In your module level gradle file remove the following:

    • ksp(libs.hilt.compiler)
    • implementation(libs.hilt.lifecycle.viewmodel)

    With this updated Hilt setup the error shouldn't occur anymore.