'Hilt: java.lang.ClassNotFoundException: Didn't find class "com.kotlin20test.Hilt_MyApp"
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>
'''
Solution 1:[1]
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
Solution 2:[2]
In your manifest rename the atributte:
android:name=".Hilt_MyApp"
to android:name=".MyApp"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ahmed Muhammed |
Solution 2 | Not an apple fan |