Skip to content

Commit 3659766

Browse files
authored
[Xamarin.Android.Build.Tasks] Add support for @(AndroidMavenLibrary). (#8420)
Context: #4528 Add support for the `@(AndroidMavenLibrary)` item group, which will download the requested Java artifact version from a Maven repository and add it as an `@(AndroidLibrary)` for binding. For example, to download the Maven package [pkg:maven/com.google.auto.value/auto-value-annotations@1.10.4][0] from Maven Central and create an Android binding for it: <ItemGroup> <AndroidMavenLibrary Include="com.google.auto.value:auto-value-annotations" Version="1.10.4" /> </ItemGroup> ~~ Specification ~~ The `//AndroidMavenLibrary/@Include` format is `{GroupId}:{ArtifactId}`. Any additional attributes such as `%(Bind)` or `%(Pack)` will be copied to the created `@(AndroidLibrary)` item. `%(Bind)` and `%(Pack)` default to `true` and control the binding process as specified for [`@(AndroidLibrary)`][1]. `%(Repository)` controls which Maven Repository to download artifacts from. Supported values include: * `Central` for [Maven Central][2]. This is the default value if `%(Repository)` is not specified. * `Google` for [Google's Maven][3]. * A URL, for downloading from a custom Maven instance. *Note*: Maven authentication is *not* supported. ~~Examples:~~ <ItemGroup> <AndroidMavenLibrary Include="androidx.core:core" Version="1.9.0" Repository="Google" Bind="false" /> <AndroidMavenLibrary Include="com.github.chrisbanes:PhotoView" Version="2.3.0" Repository="https://repository.mulesoft.org/nexus/content/repositories/public" Pack="false" /> </ItemGroup> There are 2 parts to #4528: 1. Downloading artifacts from Maven 2. Ensuring all dependencies specified in the POM file are met Only (1) is currently addressed. (2) will be addressed in the future. [0]: https://repo1.maven.org/maven2/com/google/auto/value/auto-value-annotations/1.10.4/ [1]: https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/OneDotNetEmbeddedResources.md#msbuild-item-groups [2]: https://repo1.maven.org/maven2/ [3]: https://maven.google.com/web/index.html
1 parent a831d62 commit 3659766

File tree

16 files changed

+922
-2
lines changed

16 files changed

+922
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# AndroidMavenLibrary
2+
3+
Note: This feature is only available in .NET 9+.
4+
5+
## Description
6+
7+
`<AndroidMavenLibrary>` allows a Maven artifact to be specified which will automatically be downloaded and added to a .NET Android binding project. This can be useful to simplify maintenance of .NET Android bindings for artifacts hosted in Maven.
8+
9+
## Specification
10+
11+
A basic use of `<AndroidMavenLibrary>` looks like:
12+
13+
```xml
14+
<!-- Include format is {GroupId}:{ArtifactId} -->
15+
<ItemGroup>
16+
<AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
17+
</ItemGroup>
18+
```
19+
20+
This will do two things at build time:
21+
- 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).
22+
- Add the cached package to the .NET Android bindings build as an [`<AndroidLibrary>`](https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/building-apps/build-items.md#androidlibrary).
23+
24+
Note that only the requested Java artifact is added to the .NET Android bindings build. Any artifact dependencies are not added. If the requested artifact has dependencies, they must be fulfilled individually.
25+
26+
### Options
27+
28+
`<AndroidMavenLibrary>` defaults to using Maven Central, however it should support any Maven repository that does not require authentication. This can be controlled with the `Repository` attribute.
29+
30+
Supported values are `Central` (default), `Google`, or a URL to another Maven repository.
31+
32+
```xml
33+
<ItemGroup>
34+
<AndroidMavenLibrary
35+
Include="androidx.core:core"
36+
Version="1.9.0"
37+
Repository="Google" />
38+
</ItemGroup>
39+
```
40+
41+
```xml
42+
<ItemGroup>
43+
<AndroidMavenLibrary
44+
Include="com.github.chrisbanes:PhotoView"
45+
Version="2.3.0"
46+
Repository="https://repository.mulesoft.org/nexus/content/repositories/public" />
47+
</ItemGroup>
48+
```
49+
50+
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`.)
51+
52+
```xml
53+
<ItemGroup>
54+
<AndroidMavenLibrary
55+
Include="androidx.core:core"
56+
Version="1.9.0"
57+
Repository="Google"
58+
Bind="false"
59+
Pack="false" />
60+
</ItemGroup>
61+
```

Documentation/guides/building-apps/build-items.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ installing app bundles.
174174

175175
This build action was introduced in Xamarin.Android 11.3.
176176

177+
## AndroidMavenLibrary
178+
179+
`<AndroidMavenLibrary>` allows a Maven artifact to be specified which will
180+
automatically be downloaded and added to a .NET Android binding project.
181+
This can be useful to simplify maintenance of .NET Android bindings for artifacts
182+
hosted in Maven.
183+
184+
```xml
185+
<!-- Include format is {GroupId}:{ArtifactId} -->
186+
<ItemGroup>
187+
<AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
188+
</ItemGroup>
189+
```
190+
See the [AndroidMavenLibrary documentation](../AndroidMavenLibrary.md)
191+
for more details.
192+
193+
This build action was introduced in .NET 9.
194+
177195
## AndroidNativeLibrary
178196

179197
[Native libraries](~/android/platform/native-libraries.md)

build-tools/automation/guardian/source.gdnsuppress

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@
231231
"createdDate": "2023-02-22 23:55:29Z",
232232
"expirationDate": null,
233233
"type": null
234+
},
235+
"243e199c7aec22377e0363bdca82384278cc36b0674f35697935fde6c45cfd0e": {
236+
"signature": "243e199c7aec22377e0363bdca82384278cc36b0674f35697935fde6c45cfd0e",
237+
"alternativeSignatures": [],
238+
"target": "build-tools/xaprepare/xaprepare/ThirdPartyNotices/MavenNet.cs",
239+
"memberOf": [
240+
"default"
241+
],
242+
"tool": "policheck",
243+
"ruleId": "79607",
244+
"justification": "Reference to a proper name.",
245+
"createdDate": "2023-10-26 21:20:54Z",
246+
"expirationDate": null,
247+
"type": null
234248
}
235249
}
236250
}

build-tools/installers/create-installers.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libZipSharp.dll" />
111111
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\libZipSharp.resources.dll')" />
112112
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libZipSharp.pdb" />
113+
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)MavenNet.dll" />
114+
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)MavenNet.pdb" />
113115
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Mono.Unix.dll" />
114116
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Mono.Unix.pdb" />
115117
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Microsoft.Android.Build.BaseTasks.dll" />
@@ -136,6 +138,7 @@
136138
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Application.targets" />
137139
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.ClassParse.targets" />
138140
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Core.targets" />
141+
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Maven.targets" />
139142
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Build.Tasks.dll" />
140143
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Build.Tasks.pdb" />
141144
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\Microsoft.Android.Build.BaseTasks.resources.dll')" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace Xamarin.Android.Prepare
6+
{
7+
[TPN]
8+
class MavenNet_TPN : ThirdPartyNotice
9+
{
10+
static readonly Uri url = new Uri ("https://github.com/Redth/MavenNet/");
11+
12+
public override string LicenseFile => string.Empty;
13+
public override string Name => "Redth/MavenNet";
14+
public override Uri SourceUrl => url;
15+
public override string LicenseText => @"
16+
MIT License
17+
18+
Copyright (c) 2017 Jonathan Dick
19+
20+
Permission is hereby granted, free of charge, to any person obtaining a copy
21+
of this software and associated documentation files (the ""Software""), to deal
22+
in the Software without restriction, including without limitation the rights
23+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
copies of the Software, and to permit persons to whom the Software is
25+
furnished to do so, subject to the following conditions:
26+
27+
The above copyright notice and this permission notice shall be included in all
28+
copies or substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.";
37+
38+
public override bool Include (bool includeExternalDeps, bool includeBuildDeps) => includeExternalDeps;
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!--
2+
***********************************************************************************************
3+
Xamarin.Android.Bindings.Maven.targets
4+
5+
This file contains MSBuild targets used to enable @(AndroidMavenLibrary) support.
6+
7+
***********************************************************************************************
8+
-->
9+
10+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
11+
12+
<UsingTask TaskName="Xamarin.Android.Tasks.MavenDownload" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
13+
14+
<PropertyGroup>
15+
<!-- Maven cache directory location -->
16+
<MavenCacheDirectory Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$(MavenCacheDirectory)'==''">$(LocalAppData)\dotnet-android\MavenCacheDirectory\</MavenCacheDirectory>
17+
<MavenCacheDirectory Condition="$([MSBuild]::IsOSPlatform('OSX')) and '$(MavenCacheDirectory)'==''">$(HOME)/Library/Caches/dotnet-android/MavenCacheDirectory/</MavenCacheDirectory>
18+
<MavenCacheDirectory Condition="'$(MavenCacheDirectory)'==''">$(HOME)/.cache/dotnet-android/MavenCacheDirectory/</MavenCacheDirectory>
19+
<MavenCacheDirectory>$([MSBuild]::EnsureTrailingSlash('$(MavenCacheDirectory)'))</MavenCacheDirectory>
20+
</PropertyGroup>
21+
22+
<Target Name="MavenRestore"
23+
Condition=" '@(AndroidMavenLibrary->Count())' != '0' "
24+
BeforeTargets="_CategorizeAndroidLibraries"
25+
DependsOnTargets="ResolvePackageAssets">
26+
27+
<!-- Download artifacts and POMs from Maven to a local cache. -->
28+
<MavenDownload MavenCacheDirectory="$(MavenCacheDirectory)" AndroidMavenLibraries="@(AndroidMavenLibrary)">
29+
<Output TaskParameter="ResolvedAndroidMavenLibraries" ItemName="_ResolvedAndroidMavenLibraries" />
30+
</MavenDownload>
31+
32+
<!-- Add @(AndroidMavenLibrary)'s to @(AndroidLibrary)'s. -->
33+
<ItemGroup>
34+
<AndroidLibrary Include="@(_ResolvedAndroidMavenLibraries)" />
35+
</ItemGroup>
36+
37+
</Target>
38+
39+
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.After.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
1717
<Import Project="..\tools\Xamarin.Android.Common.targets" />
1818
<Import Project="..\tools\Xamarin.Android.Bindings.Core.targets" />
1919
<Import Project="..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
20+
<Import Project="..\tools\Xamarin.Android.Bindings.Maven.targets" />
2021
<Import Project="Microsoft.Android.Sdk.AndroidLibraries.targets" />
2122
<Import Project="Microsoft.Android.Sdk.Aot.targets" Condition=" '$(AndroidApplication)' == 'true' " />
2223
<Import Project="Microsoft.Android.Sdk.Application.targets" Condition=" '$(AndroidApplication)' == 'true' " />

src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Xamarin.Android.Build.Tasks/Properties/Resources.resx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,51 @@ To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MS
949949
{0} - An Android Resource Identifier.
950950
</comment>
951951
</data>
952-
</root>
952+
<data name="XA4234" xml:space="preserve">
953+
<value>'&lt;{0}&gt;' item '{1}' is missing required metadata '{2}'</value>
954+
<comment>{0} - The MSBuild ItemGroup Item name
955+
{1} - The MSBuild Item ItemSpec
956+
{2} - The omitted MSBuild Item metadata attribute</comment>
957+
</data>
958+
<data name="XA4235" xml:space="preserve">
959+
<value>Maven artifact specification '{0}' is invalid. The correct format is 'group_id:artifact_id'.</value>
960+
<comment>The following are literal names and should not be translated: Maven, group_id, artifact_id
961+
{0} - A Maven artifact specification</comment>
962+
</data>
963+
<data name="XA4236" xml:space="preserve">
964+
<value>Cannot download Maven artifact '{0}:{1}'.
965+
- {2}: {3}
966+
- {4}: {5}</value>
967+
<comment>The following are literal names and should not be translated: Maven
968+
{0} - Maven artifact group id
969+
{1} - Maven artifact id
970+
{2} - The .jar filename we tried to download
971+
{3} - The HttpClient reported download exception message
972+
{4} - The .aar filename we tried to download
973+
{5} - The HttpClient provided download exception message</comment>
974+
</data>
975+
<data name="XA4237" xml:space="preserve">
976+
<value>Cannot download POM file for Maven artifact '{0}:{1}'.
977+
- {2}: {3}</value>
978+
<comment>The following are literal names and should not be translated: POM, Maven
979+
{0} - Maven artifact group id
980+
{1} - Maven artifact id
981+
{2} - The .pom filename we tried to download
982+
{3} - The HttpClient reported download exception message
983+
</comment>
984+
</data>
985+
<data name="XA4238" xml:space="preserve">
986+
<value>Cannot download parent POM file for Maven artifact '{0}:{1}'.
987+
- {2}: {3}</value>
988+
<comment>The following are literal names and should not be translated: POM, Maven
989+
{0} - Maven artifact group id
990+
{1} - Maven artifact id
991+
{2} - The .pom filename we tried to download
992+
{3} - The HttpClient reported download exception message</comment>
993+
</data>
994+
<data name="XA4239" xml:space="preserve">
995+
<value>Unknown Maven repository: '{0}'.</value>
996+
<comment>The following are literal names and should not be translated: Maven
997+
{0} - User supplied Maven repository type</comment>
998+
</data>
999+
</root>

0 commit comments

Comments
 (0)