Error type 3 Error: Activity class {} does not exist

Learn error type 3 error: activity class {} does not exist with practical examples, diagrams, and best practices. Covers android, android-studio, android-manifest development techniques with visual...

Resolving 'Error: Activity class {} does not exist' in Android Development

Hero image for Error type 3 Error: Activity class {} does not exist

Understand and fix the common Android runtime error 'Activity class {} does not exist' by correctly configuring your AndroidManifest.xml and build settings.

Encountering the error Error: Activity class {} does not exist is a common hurdle for Android developers, especially when setting up new activities or refactoring existing code. This error typically indicates that the Android system cannot find the activity class you are trying to launch. While the message itself is straightforward, the root causes can vary, ranging from simple typos to more complex build configuration issues. This article will guide you through diagnosing and resolving this error, ensuring your activities launch successfully.

Understanding the Android Manifest's Role

The AndroidManifest.xml file is the heart of every Android application. It declares all the components of your application, including activities, services, broadcast receivers, and content providers. When you try to launch an activity, the Android system consults this manifest to locate the corresponding class. If an activity is not declared correctly, or if there's a mismatch between the declared name and the actual class path, the system will report that the activity does not exist.

flowchart TD
    A[App Attempts to Launch Activity] --> B{Android System Checks Manifest}
    B -->|Activity Declared?| C{Is Class Path Correct?}
    C -->|Yes| D[Activity Launched Successfully]
    C -->|No| E["Error: Activity class {} does not exist"]
    B -->|No| E

Flowchart illustrating how Android resolves activity launches and potential error points.

Common Causes and Solutions

This error can stem from several common issues. Addressing these systematically will help you pinpoint and resolve the problem efficiently.

1. Missing or Incorrect Activity Declaration in AndroidManifest.xml

The most frequent cause is simply forgetting to declare your activity in the AndroidManifest.xml or providing an incorrect class name. Every activity must have an <activity> tag within the <application> tag.

<!-- Correct Declaration -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <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">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- Your new activity declaration -->
        <activity android:name=".MyNewActivity" />

    </application>
</manifest>

Example of correctly declaring an activity in AndroidManifest.xml.

Ensure the android:name attribute exactly matches the fully qualified name of your activity class. If your activity is in the root package (defined by package="com.example.myapp"), you can use a shorthand like .MyNewActivity. If it's in a subpackage (e.g., com.example.myapp.ui), you would declare it as android:name="com.example.myapp.ui.MyNewActivity" or .ui.MyNewActivity.

2. Incorrect Package Name or Class Path

Sometimes, the package name in your AndroidManifest.xml might not match the actual package structure of your project, or you might have moved an activity class without updating its declaration. Double-check the package attribute in your manifest and compare it with the actual package structure of your activity files.

// MyNewActivity.java
package com.example.myapp.ui;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MyNewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_new);
    }
}

Example of an Activity class in a subpackage.

<!-- Manifest declaration for the above activity -->
<activity android:name=".ui.MyNewActivity" />
<!-- OR -->
<activity android:name="com.example.myapp.ui.MyNewActivity" />

Corresponding manifest declarations for an activity in a subpackage.

3. Build Configuration Issues (Gradle)

Less common but equally frustrating, build configuration problems can lead to this error. This can happen if your build.gradle file has incorrect settings, especially regarding applicationId or sourceSets, which might interfere with how your manifest is processed or how classes are compiled.

// app/build.gradle
android {
    compileSdk 34

    defaultConfig {
        applicationId "com.example.myapp" // Ensure this matches your manifest's package
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    // ... other configurations
}

Verify applicationId in build.gradle matches your manifest's package.

4. Dynamic Activity Launching and Reflection

If you are launching an activity dynamically using reflection (e.g., Class.forName()) or through a custom class loader, ensure that the class name string is absolutely correct and that the class is accessible at runtime. This scenario is less common for standard app development but can occur in advanced use cases or plugin architectures.

1. Step 1: Verify Manifest Declaration

Open AndroidManifest.xml and confirm that an <activity> tag exists for the activity causing the error. Double-check the android:name attribute for typos and ensure it matches the full class path (e.g., com.yourpackage.YourActivity or .YourActivity if in the root package).

2. Step 2: Check Package Structure

Navigate to your activity's Java/Kotlin file in the project explorer. Verify that its package declaration (e.g., package com.yourpackage.ui;) aligns with how it's declared in the manifest. If the activity is in a subpackage, ensure the manifest declaration includes the subpackage path (e.g., .ui.YourActivity).

3. Step 3: Clean and Rebuild Project

In Android Studio, go to Build > Clean Project, then Build > Rebuild Project. This often resolves issues where the build system hasn't correctly processed recent changes.

4. Step 4: Inspect build.gradle

Open your app/build.gradle file and confirm that the applicationId in defaultConfig matches the package attribute in your AndroidManifest.xml.

5. Step 5: Review ProGuard/R8 Rules (if applicable)

If you're using code shrinking, check your proguard-rules.pro file. Add a keep rule for your activity if it's being launched dynamically or if you suspect it's being optimized away: -keep public class com.yourpackage.YourActivity { *; }