Skip to content

Commit 9cddd05

Browse files
authored
[docs] Sync docs with dotnet/docs-mobile@1d79b501 (#9436)
The `docs-mobile` content has been synced with dotnet/docs-mobile@1d79b50. A `docs-mobile/binding-libs/advanced-concepts/native-library-interop.md` file containing an overview of Native Library interop has been added.
1 parent d31c701 commit 9cddd05

31 files changed

+1925
-81
lines changed

Documentation/docs-mobile/TOC.yml

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- name: .NET for Android
22
items:
3-
- name: Getting Started
3+
- name: Get started
44
items:
55
- name: Installation
66
items:
@@ -10,7 +10,7 @@
1010
href: getting-started/installation/net-android.md
1111
- name: Install dependencies
1212
href: getting-started/installation/dependencies.md
13-
- name: Building apps
13+
- name: Build apps
1414
items:
1515
- name: Build process
1616
href: building-apps/build-process.md
@@ -20,18 +20,50 @@
2020
href: building-apps/build-properties.md
2121
- name: Build items
2222
href: building-apps/build-items.md
23-
- name: Features
23+
- name: Fundamentals
2424
items:
2525
- name: Layout code behind
2626
href: features/layout-code-behind/index.md
27-
- name: Maven
27+
- name: Bind Java libraries
28+
items:
29+
- name: Overview
30+
href: binding-libs/binding-java-libs/index.md
31+
- name: Bind a Java library
32+
href: binding-libs/binding-java-libs/binding-java-library.md
33+
- name: Bind a Java library from Maven
34+
href: binding-libs/binding-java-libs/binding-java-maven-library.md
35+
- name: Migrate a Xamarin.Android bindings project
36+
href: /dotnet/maui/migration/android-binding-projects?toc=/dotnet/android/toc.json&bc=/dotnet/breadcrumb/android/toc.json
37+
- name: Customize bindings
38+
items:
39+
- name: Overview
40+
href: binding-libs/customizing-bindings/index.md
41+
- name: Customize namespaces
42+
href: binding-libs/customizing-bindings/namespace-customization.md
43+
- name: Java bindings metadata
44+
href: binding-libs/customizing-bindings/java-bindings-metadata.md
45+
- name: Create enumerations
46+
href: binding-libs/customizing-bindings/creating-enums.md
47+
- name: MSBuild reference
48+
items:
49+
- name: MSBuild bindings project properties
50+
href: binding-libs/msbuild-reference/build-properties.md
51+
- name: MSBuild bindings project items
52+
href: binding-libs/msbuild-reference/build-items.md
53+
- name: Advanced concepts
2854
items:
29-
- name: "@(AndroidMavenLibrary) build item"
30-
href: features/maven/android-maven-library.md
31-
- name: Resolving Java ependencies
32-
href: features/maven/resolving-java-dependencies.md
55+
- name: AndroidMavenLibrary reference
56+
href: binding-libs/advanced-concepts/android-maven-library.md
3357
- name: Java dependency verification
34-
href: features/maven/java-dependency-verification.md
58+
href: binding-libs/advanced-concepts/java-dependency-verification.md
59+
- name: Resolve Java dependencies
60+
href: binding-libs/advanced-concepts/resolving-java-dependencies.md
61+
- name: Distribute bindings libraries
62+
href: binding-libs/advanced-concepts/distributing.md
63+
- name: Native Library Interop
64+
href: binding-libs/advanced-concepts/native-library-interop.md
65+
- name: Troubleshoot bindings
66+
href: binding-libs/customizing-bindings/troubleshooting-bindings.md
3567
- name: Message reference
3668
items:
3769
- name: Overview
@@ -296,6 +328,8 @@
296328
href: messages/xa4247.md
297329
- name: XA4248
298330
href: messages/xa4248.md
331+
- name: XA4249
332+
href: messages/xa4249.md
299333
- name: XA4301
300334
href: messages/xa4301.md
301335
- name: XA4302

Documentation/docs-mobile/features/maven/android-maven-library.md renamed to Documentation/docs-mobile/binding-libs/advanced-concepts/android-maven-library.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: AndroidMavenLibrary Build Action .NET for Android
33
description: AndroidMavenLibrary Build Action .NET for Android
4-
ms.date: 04/11/2024
4+
ms.author: jopobst
5+
ms.date: 05/11/2024
56
---
67
# AndroidMavenLibrary
78

8-
Note: This feature is only available in .NET 9+.
9-
10-
## Description
9+
> [!NOTE]
10+
> This feature is only available in .NET 9+.
1111
1212
`<AndroidMavenLibrary>` allows a Maven artifact to be specified which will automatically be downloaded and added to a .NET for Android binding project. This can be useful to simplify maintenance of .NET for Android bindings for artifacts hosted in Maven.
1313

@@ -24,7 +24,7 @@ Note: This feature is only available in .NET 9+.
2424

2525
This will do several things at build time:
2626
- Download the Java [artifact](https://central.sonatype.com/artifact/com.squareup.okhttp3/okhttp/4.9.3) with group id `com.squareup.okhttp3`, artifact id `okhttp`, and version `4.9.3` from [Maven Central](https://central.sonatype.com/) to a local cache (if not already cached).
27-
- Add the cached package to the .NET for Android bindings build as an [`<AndroidLibrary>`](../../building-apps/build-items.md#androidlibrary).
27+
- Add the cached package to the .NET for Android bindings build as an [`<AndroidLibrary>`](../msbuild-reference/build-items.md#androidlibrary).
2828
- Download the Java artifact's POM file (and any needed parent/imported POM files) to enable [Java Dependency Verification](java-dependency-verification.md). To opt out of this feature, add `VerifyDependencies="false"` to the `<AndroidMavenLibrary>` item.
2929

3030
Note that only the requested Java artifact is added to the .NET for Android bindings build. Any artifact dependencies are not added. If the requested artifact has dependencies, they must be fulfilled individually.
@@ -53,7 +53,7 @@ Supported values are `Central` (default), `Google`, or a URL to another Maven re
5353
</ItemGroup>
5454
```
5555

56-
Additionally, any attributes applied to the `<AndroidMavenLibrary>` element will be copied to the `<AndroidLibrary>` it creates internally. Thus, [attributes](https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/OneDotNetEmbeddedResources.md#msbuild-item-groups) like `Bind` and `Pack` can be used to control the binding process. (Both default to `true`.)
56+
Additionally, any attributes applied to the `<AndroidMavenLibrary>` element will be copied to the `<AndroidLibrary>` it creates internally. Thus, [attributes](../msbuild-reference/build-items.md#androidlibrary) like `Bind` and `Pack` can be used to control the binding process. (Both default to `true`.)
5757

5858
```xml
5959
<ItemGroup>
12.2 KB
Loading
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Distributing bindings libraries
3+
description: Once a Java library has been bound, it may be desirable to distribute it internally or via NuGet for consumption.
4+
ms.author: jopobst
5+
ms.date: 05/08/2024
6+
---
7+
8+
# Distributing bindings libraries
9+
10+
Once a Java library has been bound to be used in .NET for Android, there
11+
are multiple ways it can be consumed:
12+
13+
- **Project Reference** - If the binding project and the application project
14+
are in the same solution file, using a `<ProjectReference>` is generally
15+
the easiest way to consume the binding.
16+
17+
- **NuGet Package** - A NuGet package is ideal for public publishing or for
18+
an internal distributed development environment that has an internal NuGet
19+
server.
20+
21+
- **File Reference** - A consuming application can directly add a `<Reference>`
22+
to a binding `.dll` if neither the binding project nor a NuGet server is
23+
available.
24+
25+
## Controlling binding and packaging options
26+
27+
A bindings library project has two options for controlling if an `<AndroidLibrary>`
28+
gets bound and gets redistributed:
29+
30+
- **Bind** (`true`/`false`) - Defaults to `true`, meaning managed bindings are
31+
created for the specified `<AndroidLibrary>`. Setting to `false` means the Java
32+
library will be included in the output, but will not have managed bindings. This
33+
is useful if the library a needed dependency of another Java library, but it will
34+
not be called from C#.
35+
36+
```xml
37+
<!-- Java library will have C# bindings and be included in the output -->
38+
<AndroidLibrary Include="okhttp-4.12.0.jar" />
39+
40+
<!-- Java library will not have C# bindings but will still be included in the output -->
41+
<AndroidLibrary Include="okio-3.9.0.jar" Bind="false" />
42+
```
43+
44+
- **Pack** (`true`/`false`) - Defaults to `true`, meaning the specified `<AndroidLibrary>`
45+
will be included in the output (such as a NuGet package). Setting to `false` means the Java
46+
library will not be included in the output. This is a rare scenario in case the dependency is
47+
already being provided via alternative means.
48+
49+
```xml
50+
<!-- Java library will have C# bindings and be included in the output -->
51+
<AndroidLibrary Include="okhttp-4.12.0.jar" />
52+
53+
<!-- Java library will have C# bindings but will *not* be included in the output -->
54+
<AndroidLibrary Include="okio-3.9.0.jar" Pack="false" />
55+
```
56+
57+
## ProjectReference
58+
59+
If the binding project and the application project are in the same solution file,
60+
using a `<ProjectReference>` is generally the easiest way to consume the binding:
61+
62+
```xml
63+
<ProjectReference Include="mybindinglib.csproj" />
64+
```
65+
66+
The build system will be responsible for adding the managed bindings as well as any
67+
`.jar`/`.aar` files to the application project.
68+
69+
## NuGet package
70+
71+
As a property inherited from .NET SDK-style projects, any bindings library can be
72+
trivially packaged into a redistributable NuGet package by using the "Pack" command
73+
in Visual Studio or from the command line:
74+
75+
```dotnetcli
76+
dotnet pack
77+
```
78+
79+
The bindings library as well as the Java library will be included in the NuGet package:
80+
81+
![NuGet package contents](distributing-images/distributing-images-01.png)
82+
83+
Use the `Pack` attribute documented above to control which Java libraries are desired
84+
in the NuGet package.
85+
86+
NuGet packages can be customized using the [standard .NET MSBuild elements](/nuget/create-packages/creating-a-package-dotnet-cli#set-properties).
87+
88+
## File reference
89+
90+
If neither of the above options are possible, the binding `.dll` can be referenced
91+
directly with a `<Reference>` element. Note that any Java libraries that are part
92+
of the binding project output must be in the **same directory** as the binding `.dll`.
93+
94+
If you move/copy just the binding `.dll` to another location and do not also move/copy
95+
any `.jar`/`.dll` files they will not end up in your application and the binding will
96+
fail at runtime.
97+
98+
> [!IMPORTANT]
99+
> In Classic Xamarin.Android using an item action like `EmbeddedJar` would place the `.jar` file inside the `.dll` and only one file would be needed. Support for this was removed in .NET for Android because it greatly increased application build time to scan and extract embedded Java library files. Any needed `.jar`/`.dll` files MUST be located in the same directory as the binding `.dll`.

Documentation/docs-mobile/features/maven/java-dependency-verification.md renamed to Documentation/docs-mobile/binding-libs/advanced-concepts/java-dependency-verification.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
---
22
title: Java Dependency Verification in .NET for Android
33
description: Java Dependency Verification in .NET for Android
4-
ms.date: 04/11/2024
4+
ms.author: jopobst
5+
ms.date: 05/11/2024
56
---
6-
# Java Dependency Verification
77

8-
Note: This feature is only available in .NET 9+.
8+
# Java dependency verification
99

10-
## Description
10+
> [!NOTE]
11+
> This feature is only available in .NET 9+.
1112
1213
A common problem when creating Java binding libraries for .NET for Android is not providing the required Java dependencies. The binding process ignores API that requires missing dependencies, so this can result in large portions of desired API not being bound.
1314

1415
Unlike .NET assemblies, a Java library does not specify its dependencies in the package. The dependency information is stored in external files called POM files. In order to consume this information to ensure correct dependencies an additional layer of files must be added to a binding project.
1516

16-
Note: the preferred way of interacting with this system is to use [`<AndroidMavenLibrary>`](android-maven-library.md) which will automatically download any needed POM files.
17+
> [!TIP]
18+
> The preferred way of interacting with this system is to use [`<AndroidMavenLibrary>`](android-maven-library.md) which will automatically download any needed POM files.
1719
1820
For example:
1921

@@ -27,13 +29,11 @@ automatically gets expanded to:
2729
<AndroidLibrary
2830
Include="<MavenCacheDir>/Central/com.squareup.okio/okio/1.17.4/com.squareup.okio_okio.jar"
2931
Manifest="<MavenCacheDir>/Central/com.squareup.okio/okio/1.17.4/com.squareup.okio_okio.pom"
30-
JavaArtifact="com.squareup.okio:okio"
31-
JavaVersion="1.17.4" />
32+
JavaArtifact="com.squareup.okio:okio:1.17.4" />
3233

3334
<AndroidAdditionalJavaManifest
3435
Include="<MavenCacheDir>/Central/com.squareup.okio/okio-parent/1.17.4/okio-parent-1.17.4.pom"
35-
JavaArtifact="com.squareup.okio:okio-parent"
36-
JavaVersion="1.17.4" />
36+
JavaArtifact="com.squareup.okio:okio-parent:1.17.4" />
3737

3838
etc.
3939
```
@@ -42,16 +42,15 @@ However it is also possible to manually opt in to Java dependency verification u
4242

4343
## Specification
4444

45-
To manually opt in to Java dependency verification, add the `Manifest`, `JavaArtifact`, and `JavaVersion` attributes to an `<AndroidLibrary>` item:
45+
To manually opt in to Java dependency verification, add the `Manifest` and `JavaArtifact` attributes to an `<AndroidLibrary>` item:
4646

4747
```xml
48-
<!-- JavaArtifact format is {GroupId}:{ArtifactId} -->
48+
<!-- JavaArtifact format is {GroupId}:{ArtifactId}:{Version} -->
4949
<ItemGroup>
5050
<AndroidLibrary
5151
Include="my_binding_library.jar"
5252
Manifest="my_binding_library.pom"
53-
JavaArtifact="com.example:mybinding"
54-
JavaVersion="1.0.0" />
53+
JavaArtifact="com.example:mybinding:1.0.0" />
5554
</ItemGroup>
5655
```
5756

@@ -65,7 +64,7 @@ error : Java dependency 'androidx.collection:collection' version '1.0.0' is not
6564

6665
Seeing these error(s) or no errors should indicate that the Java dependency verification is working. Follow the [Resolving Java Dependencies](resolving-java-dependencies.md) guide to fix any missing dependency errors.
6766

68-
## Additional POM Files
67+
## Additional POM files
6968

7069
POM files can sometimes have some optional features in use that make them more complicated than the above example.
7170

@@ -104,12 +103,11 @@ error : Unable to resolve POM for artifact 'com.squareup.okio:okio-parent:1.17.4
104103
In this case, we need to provide the POM file for `com.squareup.okio:okio-parent:1.17.4`:
105104

106105
```xml
107-
<!-- JavaArtifact format is {GroupId}:{ArtifactId} -->
106+
<!-- JavaArtifact format is {GroupId}:{ArtifactId}:{Version} -->
108107
<ItemGroup>
109108
<AndroidAdditionalJavaManifest
110109
Include="com.square.okio.okio-parent.1.17.4.pom"
111-
JavaArtifact="com.squareup.okio:okio-parent"
112-
JavaVersion="1.17.4" />
110+
JavaArtifact="com.squareup.okio:okio-parent:1.17.4" />
113111
</ItemGroup>
114112
```
115113

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: AndroidMavenLibrary Build Action .NET for Android
3+
description: AndroidMavenLibrary Build Action .NET for Android
4+
ms.date: 10/21/2024
5+
---
6+
# NativeLibraryInterop
7+
8+
## Overview
9+
Native Library Interop (formerly referred to as the "Slim Binding" approach), refers to a
10+
pattern for accessing native SDKs in .NET for Android and .NET MAUI projects.
11+
12+
Starting in .NET 9, the .NET for Android SDK supports building Gradle projects
13+
by using the `@(AndroidGradleProject)` build action. This is declared in
14+
an MSBuild ItemGroup in a project file:
15+
16+
```xml
17+
<ItemGroup>
18+
<AndroidGradleProject Include="path/to/project/build.gradle.kts" ModuleName="mylibrary" />
19+
</ItemGroup>
20+
```
21+
22+
When an `@(AndroidGradleProject)` item is added to a .NET for Android project, the build process
23+
will attempt to create an AAR or APK file from the specified Gradle project. Any AAR output files
24+
will be added to the .NET project as an `@(AndroidLibrary)` to be bound.
25+
26+
Please see the [build-items](../../building-apps/build-items.md) docs for more information about
27+
the `@(AndroidGradleProject)` build action.
28+
29+
Additional documentation and references can be found below:
30+
31+
* https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/native-library-interop
32+
* https://github.com/CommunityToolkit/Maui.NativeLibraryInterop

0 commit comments

Comments
 (0)