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

Compose+Hilt应用启动报错:无法实例化Application类(ClassNotFoundException)

问题描述

使用Jetpack Compose和Hilt开发Android应用时,启动出现致命错误,日志如下:

FATAL EXCEPTION: main
Process: com.example.stockmarketapp, PID: 13269
java.lang.RuntimeException: Unable to instantiate application com.example.stockmarketapp.StockApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.stockmarketapp.StockApplication" on path: DexPathList[[zip file "/data/app/~~o30bU7nyuY245CegeM3Z4w==/com.example.stockmarketapp-A-XrWTo_D8jU9CrU9oqFdA==/base.apk"],nativeLibraryDirectories=[/data/app/~~o30bU7nyuY245CegeM3Z4w==/com.example.stockmarketapp-A-XrWTo_D8jU9CrU9oqFdA==/lib/arm, /system/lib, /system/system_ext/lib]]

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.stockmarketapp.StockApplication" on path: DexPathList[[zip file "/data/app/~~o30bU7nyuY245CegeM3Z4w==/com.example.stockmarketapp-A-XrWTo_D8jU9CrU9oqFdA==/base.apk"],nativeLibraryDirectories=[/data/app/~~o30bU7nyuY245CegeM3Z4w==/com.example.stockmarketapp-A-XrWTo_D8jU9CrU9oqFdA==/lib/arm, /system/lib, /system/system_ext/lib]]

Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/stockmarketapp/Hilt_StockApplication;
... 17 more

相关代码配置:

  • 自定义Application类:
@HiltAndroidApp
class StockApplication: Application() {
}
  • Manifest配置:
<?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.stockmarketapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name="com.example.stockmarketapp.StockApplication"
        android:required="false"
        android:allowBackup="true"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.StockMarketApp"
        tools:targetApi="31"
        tools:ignore="MissingClass">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.StockMarketApp">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
  • MainActivity代码:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            StockMarketAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting("Android")
                }
            }
        }
    }
}
  • 依赖与插件配置:
ext {
        compose_version = '1.2.0'
        hilt_version = '2.45'
        kotlin_version = '1.0.0'
    }
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-parcelize'
    id 'com.google.devtools.ksp' version '1.7.0-1.0.6'
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'

//Dagger - Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    annotationProcessor "com.google.dagger:hilt-compiler:$hilt_version"   //annotationProcessor
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

}

额外配置:

kapt {
    correctErrorTypes true
}
解决方案

针对这个ClassNotFoundException问题,按以下步骤排查修复:

  • 修正Gradle配置结构:
    当前把依赖写在了plugins块内部,属于语法错误。必须将依赖移到单独的dependencies块中,plugins块仅保留插件声明。同时补充缺失的Android核心配置,修改后的完整配置如下:

    ext {
        compose_version = '1.2.0'
        hilt_version = '2.45'
        kotlin_version = '1.7.0'
    }
    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android' version "$kotlin_version"
        id 'kotlin-parcelize'
        id 'com.google.devtools.ksp' version '1.7.0-1.0.6'
        id 'kotlin-kapt'
        id 'com.google.dagger.hilt.android' version "$hilt_version" apply false
    }
    
    android {
        compileSdk 33
        defaultConfig {
            applicationId "com.example.stockmarketapp"
            minSdk 21
            targetSdk 33
            versionCode 1
            versionName "1.0"
        }
        buildFeatures {
            compose true
        }
        composeOptions {
            kotlinCompilerExtensionVersion compose_version
        }
    }
    
    dependencies {
        // Hilt依赖
        implementation "com.google.dagger:hilt-android:$hilt_version"
        kapt "com.google.dagger:hilt-compiler:$hilt_version"
        implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
        kapt 'androidx.hilt:hilt-compiler:1.0.0'
        implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
    
        // Compose核心依赖
        implementation "androidx.compose.ui:ui:$compose_version"
        implementation "androidx.compose.material:material:$compose_version"
        implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
        debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    }
    
    kapt {
        correctErrorTypes true
    }
    
  • 修正版本不兼容问题:
    原配置中kotlin_version = '1.0.0'版本过低,与使用的KSP版本1.7.0-1.0.6完全不兼容,必须将Kotlin版本改为1.7.0,确保和KSP、Kotlin插件版本匹配。同时将annotationProcessor替换为kapt,适配Kotlin项目的注解处理。

  • 清理重建项目:
    执行以下操作确保缓存和编译产物被清理:

    1. 点击Android Studio菜单栏Build -> Clean Project
    2. 点击Build -> Rebuild Project
    3. 重启Android Studio,选择File -> Invalidate Caches... -> Invalidate and Restart
  • 修复Manifest无效配置:
    删除Manifest中<application>标签下的android:required="false"属性,该属性属于<uses-feature>标签,放在<application>下会导致配置解析异常。

  • 验证Hilt注解处理器运行:
    检查Build输出日志,确认Hilt_StockApplication类是否正常生成。如果未生成,说明注解处理器未运行,需再次检查kapt插件和Hilt依赖的配置是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 18:35:01