Edit

Build targets

The following build targets are defined in .NET for Android projects.

Build

Builds the source code within a project and all dependencies.

This target does not create an Android package (.apk file). To create an Android package, use the SignAndroidPackage target, or set the `$(AndroidBuildApplicationPackage) property to True when building:

msbuild /p:AndroidBuildApplicationPackage=True App.slnx

BuildAndStartAotProfiling

Builds the app with an embedded AOT profiler, sets the profiler TCP port to $(AndroidAotProfilerPort), and starts the default activity.

This legacy target applies to supported .NET 10-and-earlier projects that use Mono. It does not configure CoreCLR or NativeAOT profiling.

The default TCP port is 9999.

Added in Xamarin.Android 10.2.

Clean

Removes all files generated by the build process.

ComputeAvailableDevices

Queries and returns a list of available Android devices and emulators that can be used with dotnet run.

This target is called automatically by the .NET SDK's dotnet run command to support device selection via the --device option. It returns an @(Devices) item group where each device has the following metadata:

  • Description: A human-friendly name (e.g., "Pixel 7 - API 35" for emulators, "Pixel 6 Pro" for physical devices)
  • Type: Either "Device" or "Emulator"
  • Status: Device status - "Online", "Offline", "Unauthorized", or "NoPermissions"
  • Model: The device model identifier (optional)
  • Product: The product name (optional)
  • Device: The device name (optional)
  • TransportId: The adb transport ID (optional)

For example, to list all available devices:

dotnet build -t:ComputeAvailableDevices

This target is part of the .NET SDK device selection specification and enables commands like:

dotnet run --device <device-serial>

When a device is selected via the $(Device) property, the $(AdbTarget) property is automatically set to target that specific device for all adb operations.

Added in .NET 11.

FinishAotProfiling

Must be called after the BuildAndStartAotProfiling target.

This legacy target applies to supported .NET 10-and-earlier projects that use Mono.

Collects the AOT profiler data from the device or emulator through the TCP port $(AndroidAotProfilerPort) and writes them to $(AndroidAotCustomProfilePath).

The default values for port and custom profile are 9999 and custom.aprof.

To pass additional options to aprofutil, set them in the $(AProfUtilExtraOptions) property.

This is equivalent to:

aprofutil $(AProfUtilExtraOptions) -s -v -p $(AndroidAotProfilerPort) -o "$(AndroidAotCustomProfilePath)"

Added in Xamarin.Android 10.2.

GetAndroidDependencies

Creates the @(AndroidDependency) item group, which is used by the InstallAndroidDependencies target to determine which Android SDK packages to install.

GetApplicationArtifacts

Creates and returns the @(ApplicationArtifact) item group, which contains the APK and Android App Bundle files produced by the build.

This target always depends on the required Build target, which produces and collects platform artifacts into @(ApplicationArtifact). Later imports can set or append targets to $(GetApplicationArtifactsDependsOn) to update those existing items with additional metadata before this target or the Publish target returns them. Replacing $(GetApplicationArtifactsDependsOn) does not remove the required Build dependency.

Common application metadata is read from the final merged AndroidManifest.xml before the artifacts are collected. Final manifest values take precedence over corresponding project properties, including when a custom manifest overrides generated values or $(GenerateApplicationManifest) is false. An android:label resource reference such as @string/app_name remains a resource reference; no locale is selected when the metadata is returned.

Call this target directly when a CI job or custom tool needs the build output artifact paths:

dotnet build MyApp.csproj -t:GetApplicationArtifacts -getTargetResult:GetApplicationArtifacts

Use the Publish target result when the caller needs the copied publish outputs in $(PublishDir):

dotnet build MyApp.csproj -t:Publish -getTargetResult:Publish

Install

Creates, signs, and installs the Android package onto the default device or virtual device.

The $(AdbTarget) property specifies the Android target device the Android package may be installed to or removed from.

# Install package onto emulator via -e
# Use `/Library/Frameworks/Mono.framework/Commands/msbuild` on OS X
MSBuild /t:Install ProjectName.csproj /p:AdbTarget=-e

InstallAndroidDependencies

Calls the GetAndroidDependencies target, then installs the Android SDK packages specified in the @(AndroidDependency) item group.

dotnet build -t:InstallAndroidDependencies -f net8.0-android "-p:AndroidSdkDirectory=<path to sdk>" "-p:JavaSdkDirectory=<path to java sdk>"

The -f net8.0-android is required as this target is a .NET for Android specific target. If you omit this argument you will get the following error:

error MSB4057: The target "InstallAndroidDependencies" does not exist in the project.

The AndroidSdkDirectory and JavaSdkDirectory properties are required as we need to know where to install the required components. These directories can be empty or existing. Sdk components will be installed on top on an existing sdk installation.

The $(AndroidManifestType) MSBuild property controls which Visual Studio SDK Manager repository is used for package name and package version detection, and URLs to download.

Publish

Builds the application, copies final APK and Android App Bundle files to $(PublishDir), and returns the @(ApplicationArtifact) item group. Returned items use the copied publish-directory paths and preserve artifact metadata such as %(PackageFormat), %(Signed), %(PackageId), %(Abi), %(ApplicationId), %(ApplicationTitle), %(ApplicationName), %(ApplicationDisplayVersion), and %(ApplicationVersion).

Publish first runs GetApplicationArtifacts, which builds the project and populates @(ApplicationArtifact) with the platform-produced artifacts. Targets appended to $(GetApplicationArtifactsDependsOn) then run against those existing items before Publish calculates publish files and before Publish returns @(ApplicationArtifact), so later imports can add metadata for publish callers.

For example, to query the published artifacts:

dotnet build MyApp.csproj -t:Publish -getTargetResult:Publish

RunWithLogging

Runs the application with additional logging enabled. Helpful when reporting or investigating an issue with either the application or the runtime. If successful, messages printed to the screen will show location of the logcat file with the logged messages.

Properties which affect how the target works:

  • /p:RunLogVerbose=true enables even more verbose runtime logging
  • /p:RunLogDelayInMS=X where X should be replaced with time in milliseconds to wait before writing the log output to file. Defaults to 1000.

SignAndroidPackage

Creates and signs the Android package (.apk) file.

Use with /p:Configuration=Release to generate self-contained "Release" packages.

Package files created by this target are available in the @(ApplicationArtifact) item group.

StartAndroidActivity

Starts the default activity on the device or the running emulator.

To start a different activity, set the $(AndroidLaunchActivity) property to the activity name.

This is equivalent to:

adb shell am start -S -n @PACKAGE_NAME@/$(AndroidLaunchActivity)

Added in Xamarin.Android 10.2.

StopAndroidPackage

Completely stops the application package on the device or the running emulator.

This is equivalent to:

adb shell am force-stop @PACKAGE_NAME@

Added in Xamarin.Android 10.2.

Uninstall

Uninstalls the Android package from the default device or virtual device.

The $(AdbTarget) property specifies the Android target device the Android package may be installed to or removed from.

UpdateAndroidResources

Updates the Resource.designer.cs file.

This target is usually called by the IDE when new resources are added to the project.