Hilt: java.lang.ClassNotFoundException: Didn't find class "com.kotlin20test.Hilt_MyApp"
Asked Answered
U

8

11

I have an error concerning Hilt, I"ve been trying to inject a retrofit interface I created using Hilt,

Here is the error:


 java.lang.ClassNotFoundException: Didn't find class "com.kotlin20test.Hilt_MyApp" on path: DexPathList[[zip file "/data/app/com.example.kotlin20test-hKFhgE2D6vBE-1ZkVY-UOA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.kotlin20test-hKFhgE2D6vBE-1ZkVY-UOA==/lib/x86, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 16 more

and Here are my files:

MyApp


@HiltAndroidApp
class MyApp : Application()

build.gradle


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

android {
    //View Binding
    buildFeatures {
        viewBinding true
    }
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.example.kotlin20test"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        multiDexEnabled true
        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'
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        exclude 'META-INF/gradle/incremental.annotation.processors'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //Room
    def room_version = "2.2.6"
    def lifecycle_version = "2.3.1"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
    // Annotation processor
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"


    apply plugin: 'kotlin-kapt'
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"



    implementation 'androidx.room:room-ktx:2.3.0'
    annotationProcessor "androidx.room:room-compiler:2.3.0"

    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3'

    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"


    //Activity Result API
    implementation "androidx.activity:activity-ktx:1.3.0-alpha07"
    implementation 'androidx.fragment:fragment-ktx:1.3.3'


    //Retrofit and Gson
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'



    //Dagger Hilt
    implementation 'com.google.dagger:hilt-android:2.35.1'
    implementation 'com.google.dagger:hilt-compiler:2.35.1'


    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'androidx.multidex:multidex:2.0.0'


}



buildscript {
    ext.kotlin_version = "1.3.50"
    repositories {
        google()
        jcenter()
    }
    ext.hilt_version = '2.35'
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.35.1'


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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.kotlintest">

    <application
        tools:remove="android:appComponentFactory"
        android:name=".Hilt_MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.KotlinTest">
        <activity android:name=".MainActivity2"
            ></activity>
        <activity android:name=".MainActivity"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".ExampleBroadcast"/>
    </application>

</manifest>

I tried hilt in another application using Kotlin but still the same error is produced, I've also read a couple of questions on the subject her on Stack Overflow, and tries some Answers, including MultiDexApplication.

Edit: Sorry I wrote the wrong AndroidManifest, the previous app one was of an app that had the same error(I'm trying two apps), and I changed the second one's Application class to Hilt_MyApp class before that error showed up in hopes of trying to solve the error.

Here is the real manifest related to this app:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.kotlin20test">

    <application

        android:name="com.kotlin20test.MyApp"
        tools:remove="android:appComponentFactory"
        tools:targetApi="p"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Kotlin20Test">
        <activity android:name="com.kotlin20test.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

'''
Urmia answered 22/5, 2021 at 14:9 Comment(3)
is should be android:name=".MyApp"Nicoline
thank you for letting me notice I gave the wrong AndroidManifest, I was quite confused and frustrated because I've spent hours trying to solve that error.Urmia
@Nicoline This is not necessary. I tried and tested. Make sure you have installed all the required dependencies. Especially the kapt dependency because people almost ignore this one.Brunhild
U
15

I solved it, it turns out that I did not provide the kapt,

id 'kotlin-kapt'

and used used implementation instead of kapt so instead of:

implementation 'com.google.dagger:hilt-compiler:2.35.1'

I provided:

kapt 'com.google.dagger:hilt-compiler:2.35.1'

hope this helps someone someone

Urmia answered 23/5, 2021 at 10:15 Comment(2)
even after having the kapt, I'm still facing the issue. Were you able to resolve it?Instantly
In my case I upgrade to hilt 2.51.1Hoffman
F
4

i had the same problem today, and I saw that it in your gradle too

packagingOptions {
   exclude 'META-INF/gradle/incremental.annotation.processors'
} 

this is how you should do the hilt implementation to avoid using it and fix the problem

//Hilt
implementation "com.google.dagger:hilt-android:2.42"
kapt "com.google.dagger:hilt-android-testing:2.42"
kapt "com.google.dagger:hilt-compiler:2.42"

Renamed the application name in manifest.xml to android:name=".MyApp"

Ferrocene answered 5/6, 2022 at 11:30 Comment(1)
I edited the dependencies with the latest versions.. Actually this solution worked for me!Baywood
S
1

I ended up migrating to KSP after wasting hours with this issue. Please follow this guide for migrating to KSP: https://developer.android.com/build/migrate-to-ksp

and use this guide for Hilt versions that you should use: https://dagger.dev/dev-guide/ksp.html

Sneeze answered 25/12, 2023 at 7:23 Comment(1)
Can you confirm that migrating to KSP helped fix the issue? And that you are able to use Hilt with KSP successfully (since it's in alpha)?Plumate
P
1

I tried every single thing I could find in Stackoverflow answers...

Build/Clean/Invalid & Restart, Uninstalling app, reinstalling, Removing hilt and it's annotations, and readding them, but none of those worked. Even tried renaming the class and creating it under another name.

What DID work was, moving the affected class.

So my WalletFragment with the annotation AndroidEntryPoint was not working and giving that error. Moving it to another package fixed it.

Prostate answered 30/1 at 2:22 Comment(0)
B
0

In your manifest rename the atributte:

android:name=".Hilt_MyApp" to android:name=".MyApp"

Bluebeard answered 27/4, 2022 at 0:20 Comment(1)
This is not necessary. I tried and tested. Make sure you have installed all the required dependencies. Especially the kapt dependency because people almost ignore this one.Brunhild
E
0

I have seen some issue's in your build.gradle

You have put

apply plugin: 'kotlin-kapt'

in wrong place . I mean in dependencies instead of at top in plugins.

And you are also adding two differnt classpath like

classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.35.1'
Eolian answered 9/8, 2023 at 10:53 Comment(0)
H
0

you should use kapt instead of implementation for the hilt-compiler

kapt "com.google.dagger:hilt-compiler:$hilt_version"
Hahnert answered 11/11, 2023 at 23:15 Comment(0)
H
0

Assalamu alaykum, these configurations solved my problems.

build.gradle.kts(Project)

plugins {
   alias(libs.plugins.android.application) apply false
   alias(libs.plugins.jetbrains.kotlin.android) apply false
   id("com.google.dagger.hilt.android") version "2.44" apply false
}

build.gradle.kts(Module)

plugins {
   alias(libs.plugins.android.application)
   alias(libs.plugins.jetbrains.kotlin.android)
   kotlin("kapt")
   id("com.google.dagger.hilt.android")
}

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
    jvmTarget = "17"
}
composeOptions {
    kotlinCompilerExtensionVersion = "1.4.3"
}
dependencies {
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-compiler:2.44")
}

kapt {
    correctErrorTypes = true
}

libs.versions.toml

[versions]
agp = "8.5.2"
kotlin = "1.8.10"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Heddie answered 13/8 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.