12

I am getting an error which reads The activity 'MainActivity' is not declared in AndroidManifest.xml

Screenshot of the error

What is the problem here?

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
Jodhvir Singh
  • 345
  • 1
  • 3
  • 11

20 Answers20

17

I got this error when moving a lot of files, to fix just resync your gradle files.

File->Sync Project with Gradle Files

MRDJR97
  • 818
  • 2
  • 10
  • 27
  • This worked for me. anyway, frustrating bugs of android studio. the bug still can happen in AS 4.0.1. The confusing part of the bug is that it can find the activity for choosing the activity, but then just wont build/run it for device as it thinks that there's no activity, but in the other window it can clearly scan and see it. – Lassi Kinnunen Aug 15 '20 at 10:08
12

Try to go to File->Invalidate Caches / Restart and choose invalidate and restart ,it worked for me

df778899
  • 10,703
  • 1
  • 24
  • 36
Lucian Gabriel
  • 164
  • 2
  • 11
6

Sometimes when moving or renaming files, you can have broken XML files. My case that the ic_launcher.xml was corrupt. I regenerated that and worked just fine.

Check every XML in your proyect.

Mariano L
  • 1,809
  • 4
  • 29
  • 51
4

You are most likely just missing the below from your AndroidManifest:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"

Here is a full example of an AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pejner.myapplication">

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>
</manifest>
Prad
  • 515
  • 7
  • 15
3

this happened for me when my laptop suddenly shut down when working with project. if you see your activity declaration in your manifest, do not open any xml files. they maybe broken and corrupt after opening files. so do these steps:

  1. delete all .iml files from your project
  2. go to Files => invalidate cache / Restart
  3. Rebuild your project gradle
  4. finally if problem still exists, go to Files => Sync project with gradle files

https://stacklearn.ir

Hossein Karami
  • 776
  • 10
  • 11
1

This can be only two reason,

the bold one, missing extend statement

class MainActvity extends AppCompatActivity 

or

you are using a wrong package name to register your activity so just in this case use ALT+Space then studio will show the options itself.

Andre Batista
  • 336
  • 3
  • 11
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

All of my XML file were cut in half for some reason, I'm not sure when it happened (I use Git VCS, so maybe that). I had to go through each of them and restore the code.

sp00ky
  • 151
  • 9
1

It have three ways to resolve

1:- Go File->Invalidate Caches / choose invalidate and restart ,it works

2:- If you make MainActivity make sure you have onCreate method in it and extend with AppCompatActivity .

3:- And last option sync project with Gradle file

Ahmad
  • 201
  • 2
  • 12
1

Follow these steps:

  1. File => Invalidate Caches / Restart...
  2. Files => Sync project with gradle files

If the steps above don't work, write:

com.example.(project name).MainActivity

It should work, worked for me.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
1

For those cases the you recently changed the android:allowBackup settings, make sure you also set the tools:replace="android:allowBackup" in case some of your dependency have it declared in them to override it. I wouldn't know this solution until I manually execute Gradle > app > cleanbuild > lintFix

The exact error I received was:

Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:41:9-36 is also present at [com.kaopiz:kprogresshud:1.2.0] AndroidManifest.xml:12:9-35 value=(true). Suggestion: add 'tools:replace="android:allowBackup"' to element at AndroidManifest.xml:39:5-254:19 to override.

mr5
  • 3,438
  • 3
  • 40
  • 57
1
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.YOURPAKAGE">

    <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/Theme.YOURPAKAGE">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

NOTE: Replace your AndroidManifest.xml file code with the above code and you just have to change the name of your package at those places where I have mentioned (YOURPAKAGE) hopefully the problem will be solved

m4n0
  • 29,823
  • 27
  • 76
  • 89
  • This is actually the answer. Just not formatted very nicely. Nadir, please format. – Stevo Mar 11 '22 at 23:00
  • MainActivity is not declared error requires manifest to include: activity with name and exported, and the intent filters nested in the activity. So many answers came close. Shame this one is buried for now. – Stevo Mar 11 '22 at 23:05
0

replace in your AndroidManifest.xml

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.justjava">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
    </application>

</manifest>
meladandroid
  • 49
  • 2
  • 9
0

You should try this

<activity
            android:name="com.example.stockquote.StockInfoActivity"
            android:label="@string/app_name"
             />

For more detail you can check hereActivity declare

Community
  • 1
  • 1
0

Thanks everyone for taking the time out to help a beginner. I found out that I had somehow misspelled the Package in MainActivity.java When I corrected it the error was gone. Thank you all.

Jodhvir Singh
  • 345
  • 1
  • 3
  • 11
0

I was getting an error "The activity 'MainActivity' is not declared in AndroidManifest.xml", even though it was correct in the manifest file.

My problem was that when I created the project I had to mark the item "Use AndroidX artifacts".

Kseniya
  • 11
  • 1
0

If you see that error occur after upgrading versions of IntelliJ IDEA or Android Studio, or after Generating a new APK, you may need to refresh the IDE's cache.

File -> Invalidate Caches / Restart...
Zubair
  • 31
  • 1
  • 9
0

I was working on my project suddenly my laptop restarts i tried lot of methods but i was not able to fix it. But at the end i did it do these steps and let me know it is working or not?

1. File > Invalidate Cache and restart the the android studio
2. Build > Clean Project and then Rebuild project
3. Sync project with gradle files

if it is still showing you error try these steps

1. Create new activity
   Remember to clink on checkBox Launcher Activity
   Hope this will Fix your problem

In the end go to AndroidManifest.xml and change New activity Default to previous activity

zinonX
  • 330
  • 2
  • 20
0

In my case, none of the answers helped. Luckily, I remembered that I added a variable in build.gradle, which I tried to access from MainActivity.

Problem was that this new variable wasn't present in AndroidManifest.xml. Once I added the variable to AndroidManifest.xml, the problem was solved.

domaci_a_nas
  • 220
  • 2
  • 11
0

This is an odd one. For me I had to open Edit Configurations (run configuration) and select <no module> under the Module drop-down. Then reselect my project under the Module drop-down and Apply. It the built and ran just fine.

0

Had the same problem with a project which I just downloaded the starter code for an app , everything looked fine in the AndroidManifest.xml , I just deleted the com.example.android.(yourappname).MainActivity and wrote .dMainActivity and it worked.

Xolot
  • 13
  • 2