You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Manifest中MAIN Action增删引发启动异常及Gradle报错咨询

Fixing Android Launch Activity & Manifest Intent-Filter Issues

Hey there, let's work through your Android launch activity problem step by step—this is a super common gotcha with manifest configurations and run settings!

First, let's recap the core rules Android uses to launch your app:

  • The system identifies your app's default launch activity (the one that opens when you tap the app icon) by looking for an <activity> tag with both <action android:name="android.intent.action.MAIN" /> and <category android:name="android.intent.category.LAUNCHER" /> in its intent-filter.
  • If you try to run an activity directly (like Main2Activity) that doesn't have an intent-filter or set android:exported="true", Android throws that "must be exported or contain an intent-filter" error—because it can't safely launch an activity that's not marked as accessible.

Let's break down your specific scenarios:

  1. Adding/removing the MAIN action still launches Main2Activity: This almost always means one of two things:

    • Your AndroidManifest.xml has the MAIN+LAUNCHER intent-filter attached to Main2Activity instead of Activity1.
    • Your Android Studio Run Configuration is set to launch Main2Activity directly, ignoring the manifest's default entry.
  2. Removing MAIN action makes the app unlaunchable: Without any activity having the MAIN+LAUNCHER intent-filter, the system has no idea which activity to open as the app's entry point—hence the launch failure.


Step-by-Step Fixes

1. Correct your AndroidManifest.xml configuration

Open your manifest and double-check the intent-filters for both activities:

  • For your desired launch activity (Activity1), make sure it has the full launch intent-filter and is marked as exported (required for Android 12+):
<activity
    android:name=".Activity1"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  • For Main2Activity, remove any MAIN+LAUNCHER intent-filters (unless you want a second app icon). Configure it like this for a standard internal activity:
<activity
    android:name=".Main2Activity"
    android:exported="false" /> <!-- Set to true only if other apps need to launch it -->

2. Check your Android Studio Run Configuration

It's easy to accidentally set a specific activity as the launch target:

  • Click the run dropdown at the top of Android Studio (next to the play button) and select Edit Configurations...
  • In the left panel, select your app's configuration.
  • Under Launch options, set the Launch dropdown to:
    • Default Activity: This tells Android to use the manifest's configured launch activity.
    • If you need to test Main2Activity temporarily, select Specified activity and pick it—but make sure Main2Activity has android:exported="true" in the manifest to avoid the error.

3. Fix the "must be exported or contain an intent-filter" error

If you need to run Main2Activity directly (for testing), do one of the following:

  • Add android:exported="true" to Main2Activity's manifest entry (safe for testing, but only leave it true if the activity needs to be accessible from outside your app).
  • Or, add a custom intent-filter to Main2Activity (useful if you want to launch it via a specific intent, but overkill for simple testing):
<activity
    android:name=".Main2Activity"
    android:exported="true">
    <intent-filter>
        <action android:name="com.yourpackage.action.LAUNCH_MAIN2" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

4. Clean and rebuild to clear cache

Sometimes old build cache can cause manifest changes to not take effect:

  • Go to Build > Clean Project
  • Then Build > Rebuild Project
  • Uninstall the app from your device/emulator before re-running it to ensure no old configuration is cached.

内容的提问来源于stack exchange,提问作者Yash Tibrewal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:48:13