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

Flutter应用真机/模拟器启动失败:MainActivity类未找到求解

解决Flutter真机/模拟器启动时MainActivity类未找到的问题

报错信息

Unable to instantiate activity ComponentInfo{com.example.tutorial_app/com.example.tutorial_app.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.tutorial_app.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.tutorial_app-00_yPYh84Ld0wplZj_0YwA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tutorial_app-00_yPYh84Ld0wplZj_0YwA==/lib/arm, /data/app/com.example.tutorial_app-00_yPYh84Ld0wplZj_0YwA==/base.apk!/lib/armeabi-v7a, /system/lib, /product/lib]]
E/AndroidRuntime(30828):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3264)
E/AndroidRuntime(30828):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3489)
E/AndroidRuntime(30828):    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
E/AndroidRuntime(30828):    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)

解决步骤

  • 清理重建项目
    执行以下命令彻底清理缓存并重新构建:

    flutter clean
    flutter pub get
    flutter build apk --debug
    

    完成后重新运行应用:flutter run

  • 检查MainActivity文件路径与配置
    确认android/app/src/main/kotlin/com/example/tutorial_app/MainActivity.kt(Java版本为MainActivity.java)存在,且包名与AndroidManifest.xml中的声明一致。
    打开AndroidManifest.xml,确保activity标签的android:name属性正确:

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
    
  • 启用Multidex支持
    若应用依赖较多,需开启Multidex:

    1. 在android/app/build.gradle中添加依赖:
      dependencies {
          implementation 'androidx.multidex:multidex:2.0.1'
      }
      
    2. 在defaultConfig块中启用Multidex:
      defaultConfig {
          // 其他配置项
          multiDexEnabled true
      }
      
    3. 若未自定义Application类,在AndroidManifest.xml的application标签中指定:
      <application
          android:name="androidx.multidex.MultiDexApplication"
          <!-- 其他属性 -->
          >
      
  • 检查版本兼容性
    运行flutter doctor查看Flutter与Android插件、SDK的版本匹配情况,按照提示更新不兼容的组件版本。

  • 手动删除构建缓存
    删除android/.gradle和android/app/build文件夹,之后重新执行flutter pub get和flutter run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 13:05:26