You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Hilt时出现ChapiiApplication类找不到的问题求助

Hilt集成导致ClassNotFoundException问题排查与解决

我在Android项目中集成Hilt依赖,定义了如下自定义Application类:

package com.example.madproject

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

@HiltAndroidApp
class ChapiiApplication : Application(){

}

运行项目时抛出如下异常:

java.lang.RuntimeException: Unable to instantiate application com.example.madproject.ChapiiApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.madproject.ChapiiApplication" on path: DexPathList[[dex file "/data/data/com.example.madproject/code_cache/.overlay/base.apk/classes6.dex", zip file "/data/app/~~42a4EQWkQ8BFYWNyJsF5jw==/com.example.madproject-SSE81BrA_UcR9xrfG6md4g==/base.apk"],nativeLibraryDirectories=[/data/app/~~42a4EQWkQ8BFYWNyJsF5jw==/com.example.madproject-SSE81BrA_UcR9xrfG6md4g==/lib/arm64, /system/lib64, /system/system_ext/lib64]]

我的AndroidManifest.xml配置如下:

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

    <application
        android:name="com.example.madproject.ChapiiApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/Theme.MadProject"
        tools:targetApi="31">

        <activity
            android:name=".activities.LoginRegisterActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

已配置的Hilt相关依赖和插件如下:

Hilt依赖

implementation("com.google.dagger:hilt-android:2.49")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.5")
annotationProcessor("com.google.dagger:hilt-compiler:2.49")

Plugins

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.gms.google-services")
kotlin("kapt")
id("com.google.dagger.hilt.android")
}

解决方案

  • 替换注解处理器依赖:由于项目使用Kotlin,Hilt的注解处理器必须用kapt而非annotationProcessor,修改依赖配置:
    // 替换原annotationProcessor行
    kapt("com.google.dagger:hilt-compiler:2.49")
    
  • 清理重建+清除缓存:执行Build -> Clean Project,再执行Build -> Rebuild Project;同时通过File -> Invalidate Caches... -> Invalidate and Restart清除AS缓存并重启
  • 检查MultiDex配置:如果项目开启了MultiDex,确保在build.gradle的android/defaultConfig中添加:
    multiDexEnabled true
    
    若targetSdk低于21,还需添加MultiDex依赖:
    implementation "androidx.multidex:multidex:2.0.1"
    
  • 验证包名一致性:确认Manifest中android:name的包名与ChapiiApplication类的实际包名完全一致,避免拼写错误
  • 确认Hilt插件版本匹配:确保根build.gradle中的Hilt插件版本与依赖版本一致(若根目录未配置,需添加):
    buildscript {
        dependencies {
            classpath("com.google.dagger:hilt-android-gradle-plugin:2.49")
        }
    }
    

内容的提问来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 04:45:10