.NET for Android 错误 XA1043

示例消息

error XA1043: Could not determine what to launch: 'obj/Debug/net11.0-android/android/AndroidManifest.xml' does not contain a launchable <activity> or an <instrumentation> element. Set the $(AndroidLaunchActivity) or $(AndroidInstrumentation) MSBuild property to specify one.

Issue

dotnet run 需要一个可在设备上启动的目标。 它会在 AndroidManifest.xml 中查找可启动的 <activity>,对于没有用户界面的应用(例如基准测试宿主),则回退为使用 <instrumentation> 元素。 两者均未找到。

解决方案

使用 MAIN/LAUNCHER 意向筛选器声明活动:

[Activity (MainLauncher = true)]
public class MainActivity : Activity
{
}

或者,对于不含 Activity 的应用,请声明一个 Instrumentation:

[Instrumentation (Name = "com.companyname.myapp.MyInstrumentation")]
public class MyInstrumentation : Instrumentation
{
    protected MyInstrumentation (IntPtr handle, JniHandleOwnership ownership)
        : base (handle, ownership) { }
}

你还可以使用以下内容显式指定要启动的类 $(AndroidLaunchActivity) 或 $(AndroidInstrumentation) MSBuild 属性。