XAML Spy for Android

XAML Spy 3

As of version 3, Xamarin Android apps are first-class citizens in the XAML Spy world. All features previously available to Windows apps, are now available to Android apps, including browsing the visual tree, exploring and editing visual properties at runtime, etc. XAML Spy supports Android v5.0 (API level 21, also known as Lollipop) and up.

XAML Spy support for Android targets developers that are using Windows to develop Xamarin Android apps, preferably using Visual Studio. XAML Spy integrates deeply with Visual Studio and provides a great user experience when spying Android apps at runtime. See also the Visual Studio user guide to learn more about the features that XAML Spy brings to Visual Studio. The use of Visual Studio is not required, but highly recommended. 

Requirements

Developing Android applications requires running the application, either on a device or using an emulator. In both cases, the Android app needs to connect to the XAML Spy service running on the Windows machine connected to the device, or hosting the emulator. XAML Spy uses its remote debugging feature, to enable the connection. Make sure XAML Spy and the firewall are configured to allow remote debugging. See the remote debugging documentation for more details.

Client library

In order to spy an Android app, a small XAML Spy client library needs to be referenced by the app. The client library includes a communication protocol for connectivity with the XAML Spy Windows service, and exposes a set of services that enables querying the app for a wide variety of data. Among others this includes read/write access to the live visual tree, and access to the full property set of any visual that is located in the user interface

How it works

To be discoverable by XAML Spy, the client library needs to connect to the XAML Spy Windows service on start of the app. In order to deliver a smooth and frictionless experience, setting up the connection is completely automated. Other than referencing the client library, there is no need to manually alter the app to enable XAML Spy.

The XAML Spy client library is distributed using the NuGet package XamlSpy. The NuGet package installs a MSBuild targets file, which automatically adds a reference to the XAML Spy client library when a debug build occurs. The targets file includes a task that adds a custom class named _App deriving from Android.App.Application. The _App class is decorated with the ApplicationAttribute, properly registering it within the AndroidManifest.xml. The custom application class overrides the OnCreate method with code to bootstrap the XAML Spy functionality. A build error will occur when an Application class already exists in the Android project, when this is the case, you need to manually connect to XAML Spy. 

Next to adding a custom Application class, XAML Spy adds an assembly attribute PoweredByXamlSpy to the app. Adding this attribute allows for easy detection whether XAML Spy is enabled for an app, or not.

It is important to understand that XAML Spy does not modify the project, nor any of its source files. It does create temporary source files in the obj folder of your project, and instructs the compiler to include these files in the compilation process.

Debug builds

XAML Spy is configured to enable itself only when an app is build in debug mode. When building an app in release mode, XAML Spy is not enabled. Spying an app is typically used in a debug scenario, you would not want to deploy your app with XAML Spy enabled. Next to that, submitting an app to the store with XAML Spy included does violate the store policies, and will cause your app submission to be rejected.

Manually add XAML Spy

Enabling XAML Spy by simply referencing a NuGet package covers most scenarios. For maximum flexibility, it is also possible to manually reference and bootstrap the XAML Spy client library. An in-depth explanation on how to achieve that can be found in the How to connect manually tutorial.