Question
What are the steps to resolve the error 'java.lang.ClassNotFoundException: Didn't find class '.MainActivity' on path: DexPathList' in React Native?
// Example app entry in AndroidManifest.xml
<activity
android:name="com.yourapp.MainActivity"
android:label="YourApp"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Answer
The java.lang.ClassNotFoundException error indicates that the React Native application cannot find the specified class—in this case, MainActivity. This error commonly occurs during app initialization and is often related to configuration issues in Android's build system.
// Ensuring correct class declaration in AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Specify the MainActivity here -->
<activity android:name="com.yourapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Causes
- Incorrect naming of the MainActivity class.
- MainActivity not specified in the AndroidManifest.xml.
- Issues in the build.gradle file, such as incorrect package information.
- The app might not be fully rebuilt after creating or renaming MainActivity.
Solutions
- Verify that the MainActivity class exists and is correctly named in the appropriate package.
- Ensure that the MainActivity is declared properly in AndroidManifest.xml.
- Clean and rebuild the project using `./gradlew clean` followed by `./gradlew assemble` in the android directory.
- Check that the package name declared in build.gradle matches the package name used in your AndroidManifest.xml.
Common Mistakes
Mistake: Not correctly typing the package name in AndroidManifest.xml.
Solution: Double-check the package name in both MainActivity.java and AndroidManifest.xml.
Mistake: Not cleaning the build after making changes.
Solution: Always clean and rebuild your project after changes to the Android configuration.
Mistake: Forgetting to sync Gradle after changes in build.gradle.
Solution: Make sure to click 'Sync Now' in Android Studio after editing the build.gradle file.
Helpers
- ClassNotFoundException
- React Native MainActivity
- AndroidManifest.xml
- Java Exception Handling
- MainActivity not found error
- React Native troubleshooting