Home

Awesome

.NET for Android samples

This repository contains .NET for Android samples, showing usage of various Android API wrappers from C#. Visit the Android Sample Gallery to download individual samples.

See the .NET MAUI Installation docs for setup instructions.

Galleries

We love samples! Application samples show off our platform and provide a great way for people to learn our stuff. And we even promote them as a first-class feature of the docs site. You can find the sample galleries here:

Sample Requirements

We welcome sample submissions, please start by creating an issue with your proposal.

Because the sample galleries are powered by the github sample repos, each sample needs to have the following things:

This approach ensures that all samples integrate with the Microsoft sample code browser.

Tips for .NET Migration

The goal here is to fully "modernize" the template for .NET and C# 11.

Compare a dotnet new android template named the same as the existing project.

  1. If the root namespace doesn't match the project name, to get the existing code to compile, you may need:
<RootNamespace>Xamarin.AidlDemo</RootNamespace>
  1. Update any dependencies, NuGet packages, etc.

  2. Remove android:versionCode, android:versionName, package, <uses-sdk/>, and <application label="". These are defined in the .csproj file.

  3. Remove all unused using statements, since we now have ImplicitUsings=enable.

  4. Fix all namespace declarations to use C# 10 file-scoped namespaces.

  5. Build. Fix any warnings related to nullable reference types (Nullable=enable).

  6. Run the app and ensure the sample still works.

Porting to .NET

When porting a legacy sample to .NET, please make sure to preserve as much history of the original sample as possible. Some samples have their project, source and resource files in the same directory where the readme file, screenshot folder and other files not directly related to the sample code reside. Since .NET defaults to importing all the files in the project directory as if they were part of the project, the application code must first be moved to a subdirectory (with the exception of the .sln file).

New subdirectory should use the same name as the solution file, without the .sln extension. After creating it first move all the relevant files and directories (source code, project file(s), the Properties and Resources directories etc), using the git mv command to the newly created directory, modify the .sln file to update project file path(s) and commit these changes. This ensures that further changes will preserve commit history.

Now the sample is ready for porting. After creating new project file (using dotnet new android -n SampleName) in a separate directory, copy any necessary package and project references from the old project, updating them as needed and after that replace the old project file with the new one.

A handful of useful tips (copied from the dotnet branch's README in this repository):

  1. If the root namespace doesn't match the project name, to get the existing code to compile, you may need:
<RootNamespace>Xamarin.AidlDemo</RootNamespace>

  1. Update any dependencies, NuGet packages, etc.
  2. Remove android:versionCode, android:versionName, package, <uses-sdk/>, and <application label="". These are defined in the .csproj file.
  3. Remove all unused using statements, since we now have ImplicitUsings=enable.
  4. Fix all namespace declarations to use C# 10 file-scoped namespaces.
  5. Build. Fix any warnings related to nullable reference types (Nullable=enable).
  6. Run the app and ensure the sample still works.

Another collection of tips can be found here

License

.NET (including the android-samples repo) is licensed under the MIT license.

Code of conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.