Xamarin.Android

There are a number of bugs that XamRight can detect in the Android native part of a Xamarin codebase. These checks are applicable for both native Xamarin.Android as well as the Android portion of a Xamarin.Forms codebase.

[XR5701] Bad image naming

Android image resource name cannot contain spaces or dashes (-). XamRight does this check Note: unlike almost all other checks, this check is not performed in the background, but only when the user explicitly runs Build > Analyzer Solution with XamRight.

Improperly named image resources can cause aapt.exe to fail during build, or cause failure to load the image at runtime.

Note that this is a restriction based on the underlying Android system, not a limitation imposed by the Xamarin layer.

[XR5702] Accessing views within a layout before layout is inflated

The method FindViewById accesses a view using the id attribute on the view within a xml file. This method call however can only be made after the layout file is inflated using the SetContentView method. XamRight checks the OnCreate method in an Activity file to ensure that no calls to FindViewById is made before a call to SetContentView is made

[XR5703, XR5704] ArrayAdapter Layout error

An ArrayAdapter populates a ListView with data and also provides a view for each row in the list. When using the default ArrayAdapter the layout passed as an argument has to be of type TextView or the layout should contain a TextView. The layout also cannot contain a ListView. XamRight does two checks here:

  1. XR5703 reports that the layout does not contain a TextView
  2. XR5704 reports that the layout is a ListView or contains a ListView

[XR5705] Inconsistent assembly level attribute declarations in C# and Android Manifest files

Xamarin.Android allows for the declaration of assembly level attributes either in the Android Manifest file or in a C# file (usually the MainActivity.cs file). If the attributes are declared in both places, the Android Manifest attribute definition overrides the C# attribute value. XamRight checks inconsistencies for the following assembly attribute declarations:

  1. UsesFeature
  2. UsesLibrary

[XR5706, XR5707, XR5708, XR5709] Casing and type errors when referencing classes in Android Manifest

There are a some tags in the Android Manifest that can reference a class using the property “android:name”. The android:name value on the tag should be a fully qualified class name of the C# class where the namespace preceding the class should all be in lower case.

XamRight checks this, and also ensures the namespace and class exist and the class derives from the right type. These checks are done for the android:name property values on the following tags:

Tagandroid:name value class type
ProviderContentProvider
ReceiverBroadcastReceiver

Author