What is AndroidResource type under Android-Maui?

dg2k 1,391 Reputation points
2023-07-25T14:04:53.2633333+00:00

When migrating from XAMARIN to .NET MAUI, what was AndroidResource in Android Resources folder defaults to Content which doesn't look right.

What is AndroidResource type when migrated to MAUI? The file properties (for *.png, *.xml files) give no option for AndroidResource.

Surprisingly, when I create a new MAUI project, I can see Reources/values/colors.xml file which shows AndroidResource when the file is right-clicked and Properties command is invoked.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,101 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,534 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chathura Munasinghe 170 Reputation points
    2023-07-25T16:37:15.02+00:00

    In .NET MAUI, the resource management system has undergone changes compared to Xamarin. In Xamarin, you typically use the "AndroidResource" build action for files like *.png and *.xml in the Android project, which would make them available as resources within the Android application.

    However, in .NET MAUI, the resource management system has been unified across platforms, and the build actions used in Xamarin projects, such as "AndroidResource," "BundleResource" (iOS), and "EmbeddedResource" (shared), have been replaced with a more consistent and simpler approach.

    In .NET MAUI, you no longer use specific build actions like "AndroidResource" for Android-specific files. Instead, you place your resource files in the appropriate folders within the "Resources" folder, and they will be automatically included as resources for the respective platforms.

    For example, for Android-specific resources, you would place the *.png and *.xml files in the "Resources/drawable" and "Resources/values" folders, respectively. When you build the MAUI project, the files in these folders will be included as resources for the Android platform.

    Regarding the "AndroidResource" label you see when creating a new MAUI project for the "Resources/values/colors.xml" file, this is likely just a label in the IDE to indicate that this file is specific to Android resources. The actual build action used for this file in the MAUI project might be different, such as "EmbeddedResource" or another appropriate build action.

    In summary, when migrating from Xamarin to .NET MAUI, you should organize your resource files in the appropriate folders within the "Resources" directory without using specific platform-specific build actions like "AndroidResource" for Android files. The resource management system in .NET MAUI will take care of including them correctly for each platform during the build process.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Gautam Jain 0 Reputation points
    2024-02-23T07:59:53.8066667+00:00

    The accepted answer may have been true when it was written. But it is no longer so. In my MAUI app, I kept getting error that Resource.Id.xxx, Resource.Drawable.xxx, Resource.Layout.xxx, Resource.String.xxx were not found. So I changed each to "Embedded Resource" and again switched to "Android Resource". Then it compiled successfully.