.NET for Android Documentation

repository·main·Indexed 24 days ago

https://github.com/dotnet/android

Open-source bindings for the Android SDK that allow developers to build native Android applications using .NET managed languages like C#. It serves as a core component of .NET MAUI or can be used for standalone native development. The repository includes tooling for API XML generation, manifest attribute codegen, and Java.Interop for bridging the .NET CLR and Java VM.

Tokens
131.5K
Snippets
329
Records
687
Agent score
80%

What's inside .NET for Android

  1. Overview of .NET for Android

    main

    .NET for Android provides open-source bindings of the Android SDK and tooling for use with .NET managed languages like C#. It can be used in two ways:

    1. As part of .NET MAUI: For cross-platform application development.
    2. Independently: For native Android development using .NET.
  2. Overview of java-source-utils

    main

    What is java-source-utils?

    java-source-utils is a Java-based utility designed to process Java source code to extract metadata that is otherwise difficult to obtain reliably from compiled artifacts or HTML documentation.

    It specifically extracts:

    • Method parameter names
    • Javadoc documentation

    Why use it?

    Instead of parsing Javadoc HTML—which is considered less stable and more prone to changes in format—this tool uses JavaParser to analyze the actual Java source code directly, providing a more robust way to retrieve documentation and parameter information.

  3. Overview of customizing .NET for Android bindings

    main

    While .NET for Android automates the binding process between Java and C#, manual customization via metadata is often required to resolve language differences. You can modify the binding metadata to:

    • Resolve build errors: Fix issues caused by missing types, obfuscated types, duplicate names, or class visibility problems.
    • Manage types: Remove unused types that do not require binding, or add types that lack a direct counterpart in the underlying Java API.
    • Improve ergonomics: Shape the resulting API to follow .NET design guidelines, such as changing the namespace of bound types to make them more consistent with C# conventions.
  4. What is Java.Interop

    main

    Java.Interop is a binding of the Java Native Interface (JNI) designed for use from managed languages like C#. It includes a set of code generators that allow Java code to invoke managed code, effectively bridging the gap between code running on the .NET CLR and code running on a Java VM.

    Important Note: This does not enable running Java code on .NET or .NET code on a Java VM directly; it provides the interop layer between the two environments.

    Java.Interop is currently shipped as part of .NET for Android (available via Visual Studio or .NET 6+). While it is designed to be independent of Android and usable by other Java implementations, it does not ship as a standalone product for general use unless compiled from source.

  5. Introduction to TMT (Type Map Tool)

    main

    TMT is a utility program designed to read .NET for Android application type maps. These maps are stored as binary data within the libxamarin-app.so shared library, which is compiled for each native architecture and packaged inside an APK or AAB archive.

    Type maps facilitate the runtime mapping between Managed types and Java types. TMT can process both Debug and Release configurations:

    • Debug maps: Contain full type information.
    • Release maps: Use assembly module GUIDs and type token IDs instead of Managed type names to save space. TMT can resolve these names if the source location allows it.

    Supported input locations:

    • Application APK/AAB archives.
    • Application project top directory (searches obj or obj/$CONFIGURATION subdirectories).
    • Any directory containing libxamarin-app.so.
    • A direct path to a libxamarin-app.so file.
  6. Overview of the Mono.Android API XML generator

    main
    The api-xml-adjuster is a build-only tool used to regenerate the src/Mono.Android/Profiles/api-*.xml.in files. This process involves downloading Android documentation archives, scraping DroidDoc HTML files, and re-parsing them to resolve inheritance hierarchies to produce backward-compatible API description XMLs.
  7. Understand the .NET for Android directory structure

    main

    The repository is organized into several key directories that separate build artifacts, source code, and tools. The $(Configuration) MSBuild property (defaulting to Debug) determines which output subdirectories are created.

    Key Directories

    • bin: Contains build outputs.
      • Build$(Configuration): Artifacts required to build the xamarin-android repository itself. These are not needed for building external apps.
      • $(Configuration): Redistributable artifacts for end-users.
        • bin: Programs for Unix-style installation ($(prefix)/bin).
        • packs\Microsoft.Android.Sdk.$(HostOS)\$(AndroidPackVersion)\tools: MSBuild project integrations.
        • lib\xamarin.android\xbuild-frameworks\MonoAndroid\v1.0: Xamarin.Android BCL assemblies.
        • lib\xamarin.android\xbuild-frameworks\MonoAndroid\*: Framework assemblies, including $(TargetFrameworkVersion)-specific assemblies.
      • Test$(Configuration): Root for unit test outputs.
    • build-tools: Internal tooling used to build the repository (e.g., android-toolchain, api-merge, jnienv-gen, mono-runtimes).
    • src: Redistributable source projects. Outputs are located in bin/$(Configuration). Key projects include:
      • Mono.Android: Builds Mono.Android.dll for specific API levels.
      • Mono.Android.Export: Builds Mono.Android.Export.dll.
      • Xamarin.Android.Build.Tasks: MSBuild tasks.
      • Xamarin.Android.Build.Utilities: MSBuild task support.
      • Xamarin.AndroidTools.Aidl: AIDL processor.
      • Xamarin.Android.Tools.BootstrapTasks: Supplemental build tasks.
    • tools: Utilities built into bin/$(Configuration).
    • external: Git submodules like Java.Interop and mono.
    • packages: NuGet packages (generated via make prepare).
    • samples: Sample applications.
  8. Android Instant Apps support in .NET for Android

    main

    Currently, support for Android Instant Apps in .NET for Android is considered impossible and impractical due to platform-imposed restrictions from Google.

    Android Instant Apps allow users to launch application features instantly via links without a full installation. However, .NET for Android cannot currently meet the requirements for this feature because:

    1. Size Constraints: Google limits the total size of the base and feature APKs to 4MB. A minimal .NET for Android 'Hello World' application in Release mode nearly reaches this limit because the Mono runtime alone is approximately 3MB, excluding the Base Class Library (BCL), SDK, and application assemblies.
    2. Native Code Restrictions: Instant Apps are prohibited from containing or running arbitrary native code/libraries and cannot dynamically load code other than the Instant Apps runtime. Since .NET for Android relies on the Mono and .NET runtimes implemented as shared libraries (and often requires libraries like SQLite), it violates these security restrictions.
  9. Understand the .NET for Android build process

    main

    The .NET for Android build process automates several critical tasks required to create an Android application, including:

    • Generating Resource.designer.cs from Android resources.
    • Supporting build actions like @(AndroidAsset) and @(AndroidResource).
    • Generating Android-callable wrappers for Java integration.
    • Producing .apk or .aab application packages for execution on Android devices.

    Application packages are categorized by the MSBuild Configuration:

    • Release: Fully self-contained packages suitable for App stores.
    • Debug: Packages that may rely on external components (like assemblies) for faster iteration.