MyAppHiltApp: java.lang.ClassNotFoundException: 在路径上找不到类 “HiltApp”

huangapple go评论55阅读模式
英文:

MyAppHiltApp: java.lang.ClassNotFoundException: Didn't find class "HiltApp" on path: DexPathList

问题

I have a problem how to fix java.lang.RuntimeException: Unable to instantiate application com.example.bettehomes.MyAppHiltApp: java.lang.ClassNotFoundException: Didn't find class "com.example.bettehomes.BetteHomesHiltApp" on path: DexPathList. My package name is "com.example.bettehomes". I tried clean and rebuild.

AndroidManifest.xml:

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

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.BetteHomes"
        android:name=".BetteHomesHiltApp">
        <activity
            android:name=".BetteHomesActivity"
            android:exported="true"
            android:theme="@style/Theme.BetteHomes.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_launcher" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/dark_orange" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />
        <meta-data
            android:name="firebase_performance_logcat_enabled"
            android:value="true" />
    </application>

The logcat shows:

FATAL EXCEPTION: main Process: com.example.bettehomes, PID: 27056
java.lang.RuntimeException: Unable to instantiate application
com.example.bettehomes.BetteHomesHiltApp:
java.lang.ClassNotFoundException: Didn't find class
"com.example.bettehomes.BetteHomesHiltApp" on path: DexPathList[[zip
file
"/data/app/com.example.bettehomes-KuesyOEkKsRDfOrSY8aeXA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bettehomes-KuesyOEkKsRDfOrSY8aeXA==/lib/arm64, /system/lib64, /system/product/lib64, /system/vendor/lib64]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:1090)
at
android.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
at android.app.ActivityThread.access$1100(ActivityThread.java:208)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:205)
at android.app.ActivityThread.main(ActivityThread.java:6991)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:884)

英文:

I have a problem how to fix java.lang.RuntimeException: Unable to instantiate application com.example.bettehomes.MyAppHiltApp: java.lang.ClassNotFoundException: Didn&#39;t find class &quot;com.example.bettehomes.BetteHomesHiltApp&quot; on path: DexPathList. My package name is "com.example.bettehomes". I tried clean and rebuild.

AndroidManifest.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.bettehomes&quot;&gt;

    &lt;application
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:theme=&quot;@style/Theme.BetteHomes&quot;
        android:name=&quot;.BetteHomesHiltApp&quot;
        &gt;
        &lt;activity
            android:name=&quot;.BetteHomesActivity&quot;
            android:exported=&quot;true&quot;
            android:theme=&quot;@style/Theme.BetteHomes.NoActionBar&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;meta-data
            android:name=&quot;com.google.firebase.messaging.default_notification_icon&quot;
            android:resource=&quot;@mipmap/ic_launcher&quot; /&gt;
        &lt;meta-data
            android:name=&quot;com.google.firebase.messaging.default_notification_color&quot;
            android:resource=&quot;@color/dark_orange&quot; /&gt;
        &lt;meta-data
            android:name=&quot;com.google.firebase.messaging.default_notification_channel_id&quot;
            android:value=&quot;@string/default_notification_channel_id&quot; /&gt;
        &lt;meta-data
            android:name=&quot;firebase_performance_logcat_enabled&quot;
            android:value=&quot;true&quot; /&gt;
    &lt;/application&gt;

The logcat shows:

> FATAL EXCEPTION: main Process: com.example.bettehomes, PID: 27056
> java.lang.RuntimeException: Unable to instantiate application
> com.example.bettehomes.BetteHomesHiltApp:
> java.lang.ClassNotFoundException: Didn't find class
> "com.example.bettehomes.BetteHomesHiltApp" on path: DexPathList[[zip
> file
> "/data/app/com.example.bettehomes-KuesyOEkKsRDfOrSY8aeXA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bettehomes-KuesyOEkKsRDfOrSY8aeXA==/lib/arm64, /system/lib64, /system/product/lib64, /system/vendor/lib64]]
> at android.app.LoadedApk.makeApplication(LoadedApk.java:1090)
> at
> android.app.ActivityThread.handleBindApplication(ActivityThread.java:6140)
> at android.app.ActivityThread.access$1100(ActivityThread.java:208)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:205)
> at android.app.ActivityThread.main(ActivityThread.java:6991)
> at java.lang.reflect.Method.invoke(Native Method)
> at
> com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:884)

答案1

得分: 1

以下是翻译好的部分:

成功的方法是:
在 Hilt 应用中解决 ClassNotFound 问题的方法是:(当然要在应用的 build.gradle 中添加 kotlin-kapt 插件 ID 和 com.google.dagger.hilt.android 的 ID,使用 JAVA 18。)

implementation "com.google.dagger:hilt-android:${hilt_version}"(2.46)
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
kapt 'com.google.dagger:hilt-android-compiler:${hilt_version}'

对于 Android Gradle 插件 7.4.2、Kotlin 1.8.20、Compose 1.4.6

英文:

What eventually worked was:
What worked for ClassNotFound in hilt app was: (of course added kotlin-kapt plugin id and id com.google.dagger.hilt.android in app's build.gradle) using JAVA 18.

`implementation &quot;com.google.dagger:hilt-android:${hilt_version}&quot; (2.46)
implementation &#39;androidx.hilt:hilt-navigation-compose:1.0.0&#39;
kapt &#39;androidx.hilt:hilt-compiler:1.0.0&#39;
kapt &#39;com.google.dagger:hilt-android-compiler:${hilt_version}&#39;`

For android gradle plugin`7.4.2` kotlin `1.8.20` compose `1.4.6`

答案2

得分: 0

以下是代码部分的中文翻译:

在您的项目中添加kapt,将annotationProcessor替换为以下代码:

项目级别的build.gradle文件:

plugins {
    ...
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

应用级别的build.gradle文件:

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

android {
    ...
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.6' // 如果使用Compose,请安装此版本
    }
    ...
}

dependencies {
    ...
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
}

kapt {
    correctErrorTypes true
}

此解决方案适用于您正在使用的AGP 8.0.0Kotlin 1.8.20Gradle 8.0的项目版本。我已经验证了在具有指定版本的新项目中可以正常工作。

英文:

Try adding kapt to your project by replacing annotationProcessor with the following code:

build.gradle project level:

plugins {
    ...
    id &#39;com.google.dagger.hilt.android&#39; version &#39;2.44&#39; apply false
}

build.gradle app level:

plugins {
    ...
    id &#39;kotlin-kapt&#39;
    id &#39;com.google.dagger.hilt.android&#39;
}

android {
    ...
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
        kotlinOptions {
            jvmTarget = &quot;1.8&quot;
        }
    }
    composeOptions {
        kotlinCompilerExtensionVersion &#39;1.4.6&#39; // Install this version if using Compose
    }
	...
}

dependencies {
    ...
    implementation &quot;com.google.dagger:hilt-android:2.44&quot;
    kapt &quot;com.google.dagger:hilt-compiler:2.44&quot;
}

kapt {
    correctErrorTypes true
}

This solution is for AGP 8.0.0, Kotlin 1.8.20 and Gradle 8.0 you are using. I have verified that this works in a new project with the specified versions.

huangapple
  • 本文由 发表于 2023年5月7日 03:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定