Flutter Android app builds successfully but crashes with ClassNotFoundException for MainActivity


Yesterday I installed/configured Flutter and Android Studio on a fresh Windows setup and created my first Flutter project:

flutter create first_app

I did not modify any project files after creating it.

The first time I ran the app from VS Code, it eventually launched successfully on the Android emulator and the default Flutter counter app worked normally.

After rebooting the PC today, I started the Android emulator again. The previously installed app was still working normally.

However, when I clicked Run in VS Code (or ran flutter run), Flutter successfully built and installed the APK, but Android immediately crashed when trying to launch MainActivity.

Error

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk
Installing build\app\outputs\flutter-apk\app-debug.apk...

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.example.first_app
E/AndroidRuntime: java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{com.example.first_app/com.example.first_app.MainActivity}

Caused by: java.lang.ClassNotFoundException:
Didn't find class "com.example.first_app.MainActivity"
on path: DexPathList[...]

Environment

Windows 11 Home 64-bit
Flutter 3.35.7 (stable)
Dart 3.9.2
Android Studio 2026.1.3
Android SDK 37.0.0
Android Emulator 37.1.11.0
Emulator device: sdk gphone64 x86 64
Android 13 / API 33
Java: OpenJDK Temurin 17.0.20.1
Gradle: 9.3.1
Android Gradle Plugin: 9.1.0
Kotlin: 2.4.0

flutter doctor -v reports:

[√] Flutter
[√] Android toolchain
[√] Android Studio
[√] Connected device
...
• No issues found!

All Android SDK licenses are accepted.

MainActivity.kt

The file exists at:

android/app/src/main/kotlin/com/example/first_app/MainActivity.kt

and contains only:

package com.example.first_app

import io.flutter.embedding.android.FlutterActivity

class MainActivity : FlutterActivity()

build.gradle.kts

The relevant configuration is:

android {
    namespace = "com.example.first_app"

    ...

    defaultConfig {
        applicationId = "com.example.first_app"
        ...
    }
}

AndroidManifest.xml

The activity is declared as:

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop"
    ...>

So the package, namespace, application ID, file path and MainActivity declaration all appear to match.

Gradle configuration

android/settings.gradle.kts:

plugins {
    id("dev.flutter.flutter-plugin-loader") version "1.0.0"
    id("com.android.application") version "9.1.0" apply false
    id("org.jetbrains.kotlin.android") version "2.4.0" apply false
}

gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-all.zip

gradle.properties:

org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.newDsl=false
android.builtInKotlin=false

Things already tried

I ran:

flutter clean
flutter pub get
flutter run

The result is exactly the same.

The APK builds successfully and installs successfully, but Android cannot find com.example.first_app.MainActivity.

There is also this warning during the build:

Warning: SDK processing. This version only understands SDK XML versions up to 3
but an SDK XML file of version 4 was encountered.

Yesterday I had an issue with the Android SDK Command-line Tools, where version 23 caused problems with Flutter/Android SDK license acceptance. I removed version 23 and installed version 22 through Android Studio's SDK Tools. flutter doctor now reports all licenses as accepted.

Question

What could cause a freshly generated Flutter project to successfully compile and install an APK, but for Android to report:

ClassNotFoundException:
Didn't find class "com.example.first_app.MainActivity"

even though MainActivity.kt exists in the correct package and has the expected contents?

Could this be related to the combination of:

Flutter 3.35.7
AGP 9.1.0
Gradle 9.3.1
Kotlin 2.4.0
Android SDK 37
Java 17

or to the Android SDK Command-line Tools version 22?

Any suggestions on how to diagnose why MainActivity is not ending up in the generated APK would be greatly appreciated.

1
Aug 26 at 9:20 AM
User AvatarRiccardo Grandi
#android#flutter#kotlin#gradle

Accepted Answer

I've had a similar problems back in flutter 3.38, in my case it is because of my android emulator. When I tried to create a new one, the app run just fine. Create a new emulator and try to run it there, if it doesn't work I suggest you to create a new project to see if the error is still persistent.

User Avatarchristian leon
Aug 27 at 3:12 AM
0