Flutter crashes on startup: Didn't find class .MainActivity on path DexPathList

6,580

Solution 1

My findings suggest this is not an issue with Flutter itself, but occurs after particular Flutter package installations. After upgrading Gradle and the AndroidX libraries, the crash disappeared and then re-appeared after another Flutter package addition. Then I cleaned (build->clean in the IDE) the android project again and it was fixed once again. So it may be due to some Gradle confusions in my environment and probably it just doesn't compile some classes the right way from time to time.

Solution 2

I had similar error, after a "package name" change. My solution was: Update package name also in MainActivity.kt.

In short, all package names should be fixed accordingly.

after that, it is fixed.

Solution 3

This happened to me when I [partially] renamed the package name (app ID) from com.example.myappone to com.myapp.one. I thought it'd be sufficient to change just the values in android/app/src/mainAndroidManifest.xml and ios/Runner.xcodeproj/project.pbxproj.

Turns out the actual package names also need to be changed so that the source files are located by the compiler

Solution 4

The simplest way to solve this if using Android Studio/Flutter is to:

1:Stop the app,then Open MainActivity.kt:

path : "**android/src/main/kotlin/MainActivity.kt**"

If your package name in MainActivity does not match your app,update it in your MainActivity.kt. Android Studio

2:Then go to Edit>Find > Replace in Files and replace the previous package name with the new one across the entire app and all files.Restart Android Studio before proceeding to the next step.(Invalidate Caches and Restart)

3:After the restart...Open app/src/build.gradle and enable multidex:

defaultConfig {
    applicationId "com.your.apppackage"
    minSdkVersion 28
    targetSdkVersion 30
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    multiDexEnabled true //add multidex enabled
}

...and dont forget to add the multidex dependency in the same gradle file:

**dependencies {**
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.multidex:multidex:2.0.1' // add it as a dependency here
}

and finally invalidate caches and restart. (File>Invalidate Caches/Restart).

Solution 5

Solution: Its simple solution is to go to MainActivity.kt and match the Package-name with AndroidManifest.xml.

Share:
6,580
John
Author by

John

Updated on December 18, 2022

Comments

  • John
    John over 1 year

    I'm using Flutter with the MultiDex builder enabled for Android and encountered a pretty tricky issue. My app was running without any exceptions but when the video_player package was added it just crashed on startup:

    FATAL EXCEPTION: main
    Process: com.rsg.anygoal, PID: 5883
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rsg.anygoal/com.rsg.anygoal.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.rsg.anygoal.MainActivity" on path: DexPathList[[zip file "/data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/base.apk"],nativeLibraryDirectories=[/data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/lib/x86, /data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/base.apk!/lib/x86, /system/lib, /vendor/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.rsg.anygoal.MainActivity" on path: DexPathList[[zip file "/data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/base.apk"],nativeLibraryDirectories=[/data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/lib/x86, /data/app/com.rsg.anygoal-tCKWlQXJD-iwdHtVRl0qqA==/base.apk!/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
    

    Later while investigating the issue I tried to disable MultiDex and that seemingly fixed it, but then adding additional flutter package broke the app again, so I decided it's not strictly caused by multiDex and re-enabled it.

    Another fix attempt I found in forums was to rename ".MainActivity" to the exact class path "com.*...*.MainActivity" but that did not help either.

    Hopefully someone will suggest a possible fix to this! Leaving a few more logs and code snippets I collected during my investigation, which may include clues helpful to you.

    Full Android logcat logs: https://pastebin.com/vPs614jj

    Flutter pubspec (name and description intentionally omitted):

    version: 1.0.6+16
    environment:
      sdk: ">=2.1.0 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^0.1.2
      intl: ^0.15.8
      firebase_core: ^0.4.3+1
      firebase_admob: ^0.9.0+10
      firebase_analytics: ^5.0.9
      firebase_auth: ^0.15.3
      firebase_crashlytics: ^0.1.2+4
      firebase_performance: ^0.3.1+5
      firebase_remote_config: ^0.3.0+1
      firebase_messaging: ^6.0.9
      firebase_dynamic_links: ^0.5.0+9
      cloud_firestore: ^0.13.0+1
      cloud_functions: ^0.4.1+6
      google_sign_in: ^4.1.1
      provider: ^4.0.1
      data_connection_checker: ^0.3.4
      shared_preferences: ^0.5.6
      flutter_markdown: ^0.3.2
      url_launcher: ^5.4.1
      barcode_scan: ^1.0.0
      qr_flutter: ^3.2.0
      share: ^0.6.3+5
      video_player: ^0.10.11+2
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
    
    flutter:
      fonts:
        - family: Raleway
          fonts:
            - asset: fonts/Raleway-Regular.ttf
      uses-material-design: true
    

    Android Manifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="$APP_DOMAIN">
        <application android:name="io.flutter.app.FlutterApplication" android:label="$APP_NAME" android:icon="@mipmap/ic_launcher">
            <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
                <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" />
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
                <intent-filter>
                    <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="$APP_ADS_ID"/>
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@drawable/notification_icon" />
        </application>
    
        <uses-permission android:name="android.permission.INTERNET"/>
    </manifest>
    

    MainActivity.java:

    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    
    public class MainActivity extends FlutterActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
      }
    }
    

    Flutter doctor output (XCode shouldn't matter):

    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.4 19E287, locale en-BG)
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    [!] Xcode - develop for iOS and macOS
        ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
          Download at: https://developer.apple.com/xcode/download/
          Or install Xcode via the App Store.
          Once installed, run:
            sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
            sudo xcodebuild -runFirstLaunch
    [✓] Android Studio (version 3.5)
    [✓] VS Code (version 1.46.1)
    [✓] Connected device (1 available)
    
  • positrix
    positrix almost 3 years
    This worked for me. I did a search and replaced and replaced all the old package names with the new one. I had used the change_app_package_name library from pub.dev - but it did not change the MainActivity.kt and project.pbxproj files. The search replace fixed this.
  • positrix
    positrix almost 3 years
    This is wrong. It's not a problem with Flutter. It happened to me after I changed the package name using the change_app_package_name from pub.dev. Apparently It did not change the package name in all places. It did not change the MainActivity.kt and project.pbxproj files. I did a search and replace and this fixed the problem.
  • egarro
    egarro over 2 years
    As a temporary solution it works like a charm, thanks!
  • wesleyhodaj
    wesleyhodaj about 2 years
    this is the correct answer, best regards bro
  • Deekshith Xetty
    Deekshith Xetty almost 2 years
    You saved my day ✨
  • Imtiaz
    Imtiaz almost 2 years
    @DeekshithXetty Great