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

MAUI中在AndroidManifest.xml定义MainActivity遇类找不到错误求助

MAUI AndroidManifest.xml 配置 MainActivity 找不到类的解决方法

问题本质

你的项目中,<RootNamespace>D</RootNamespace> 是C#代码的根命名空间,<ApplicationId>A.B.C.D</ApplicationId> 是Android应用的包名。MAUI编译Android代码时,会将C#的命名空间直接映射为Java的包结构,但你手动配置android:name时,没有匹配到编译后实际生成的Java类全限定名,导致系统找不到对应的Activity类。

解决方案

方案1:用C#特性自动生成Manifest(推荐)

MAUI不建议手动修改AndroidManifest.xml来配置Activity,直接通过C#特性就能自动生成正确的配置,完全避免路径错误:

  1. 打开Platforms/Android/MainActivity.cs
  2. 确保MainActivity类上的Activity特性配置正确(如果没有就加上):
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace D.Platforms.Android;

[Activity(
    Label = "D",
    Icon = "@mipmap/appicon",
    Theme = "@style/MainTheme",
    MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density
)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }
}

编译时MAUI会自动在生成的AndroidManifest.xml中添加正确的Activity条目,无需手动操作。

方案2:手动修改AndroidManifest.xml的正确写法

如果一定要手动配置,必须保证android:name完全匹配编译后Java类的全限定名:

  1. 打开Platforms/Android/AndroidManifest.xml
  2. 将activity的android:name设置为D.Platforms.Android.MainActivity,同时确保package属性与ApplicationId一致:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="A.B.C.D">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
        <activity 
            android:name="D.Platforms.Android.MainActivity"
            android:exported="true"
            android:label="D"
            android:theme="@style/MainTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

这里的android:name必须和C#中MainActivity的完整命名空间+类名完全一致,因为MAUI编译时会直接将C#命名空间转换为Java的包结构。

额外排查步骤

  • 检查MainActivity.cs的命名空间是否确实是D.Platforms.Android,有没有拼写错误
  • 清理项目缓存:删除bin和obj目录,然后重新编译,避免旧缓存干扰
  • 确认AndroidManifest.xml中的package属性和D.csproj里的ApplicationId完全相同

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 17:08:16