Android 版 .NET 錯誤 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
{
}

或者,對於沒有活動的應用程式,請宣告一個儀器:

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

你也可以使用以下方式明確指定要啟動的類別 $(AndroidLaunchActivity) 或 $(AndroidInstrumentation) MSBuild 屬性。