Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add support for @(AndroidMavenLibrary). (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
jpobst authored Nov 16, 2023
1 parent a831d62 commit 3659766
Show file tree
Hide file tree
Showing 16 changed files with 922 additions and 2 deletions.
61 changes: 61 additions & 0 deletions Documentation/guides/AndroidMavenLibrary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# AndroidMavenLibrary

Note: This feature is only available in .NET 9+.

## Description

`<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.

## Specification

A basic use of `<AndroidMavenLibrary>` looks like:

```xml
<!-- Include format is {GroupId}:{ArtifactId} -->
<ItemGroup>
<AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
</ItemGroup>
```

This will do two things at build time:
- 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).
- 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).

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.

### Options

`<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.

Supported values are `Central` (default), `Google`, or a URL to another Maven repository.

```xml
<ItemGroup>
<AndroidMavenLibrary
Include="androidx.core:core"
Version="1.9.0"
Repository="Google" />
</ItemGroup>
```

```xml
<ItemGroup>
<AndroidMavenLibrary
Include="com.github.chrisbanes:PhotoView"
Version="2.3.0"
Repository="https://repository.mulesoft.org/nexus/content/repositories/public" />
</ItemGroup>
```

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`.)

```xml
<ItemGroup>
<AndroidMavenLibrary
Include="androidx.core:core"
Version="1.9.0"
Repository="Google"
Bind="false"
Pack="false" />
</ItemGroup>
```
18 changes: 18 additions & 0 deletions Documentation/guides/building-apps/build-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ installing app bundles.

This build action was introduced in Xamarin.Android 11.3.

## AndroidMavenLibrary

`<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.

```xml
<!-- Include format is {GroupId}:{ArtifactId} -->
<ItemGroup>
<AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="4.9.3" />
</ItemGroup>
```
See the [AndroidMavenLibrary documentation](../AndroidMavenLibrary.md)
for more details.

This build action was introduced in .NET 9.

## AndroidNativeLibrary

[Native libraries](~/android/platform/native-libraries.md)
Expand Down
14 changes: 14 additions & 0 deletions build-tools/automation/guardian/source.gdnsuppress
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@
"createdDate": "2023-02-22 23:55:29Z",
"expirationDate": null,
"type": null
},
"243e199c7aec22377e0363bdca82384278cc36b0674f35697935fde6c45cfd0e": {
"signature": "243e199c7aec22377e0363bdca82384278cc36b0674f35697935fde6c45cfd0e",
"alternativeSignatures": [],
"target": "build-tools/xaprepare/xaprepare/ThirdPartyNotices/MavenNet.cs",
"memberOf": [
"default"
],
"tool": "policheck",
"ruleId": "79607",
"justification": "Reference to a proper name.",
"createdDate": "2023-10-26 21:20:54Z",
"expirationDate": null,
"type": null
}
}
}
3 changes: 3 additions & 0 deletions build-tools/installers/create-installers.targets
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libZipSharp.dll" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\libZipSharp.resources.dll')" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libZipSharp.pdb" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)MavenNet.dll" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)MavenNet.pdb" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Mono.Unix.dll" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Mono.Unix.pdb" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Microsoft.Android.Build.BaseTasks.dll" />
Expand All @@ -136,6 +138,7 @@
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Application.targets" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.ClassParse.targets" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Core.targets" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Maven.targets" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Build.Tasks.dll" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Build.Tasks.pdb" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\Microsoft.Android.Build.BaseTasks.resources.dll')" />
Expand Down
40 changes: 40 additions & 0 deletions build-tools/xaprepare/xaprepare/ThirdPartyNotices/MavenNet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace Xamarin.Android.Prepare
{
[TPN]
class MavenNet_TPN : ThirdPartyNotice
{
static readonly Uri url = new Uri ("https://github.com/Redth/MavenNet/");

public override string LicenseFile => string.Empty;
public override string Name => "Redth/MavenNet";
public override Uri SourceUrl => url;
public override string LicenseText => @"
MIT License
Copyright (c) 2017 Jonathan Dick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the ""Software""), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.";

public override bool Include (bool includeExternalDeps, bool includeBuildDeps) => includeExternalDeps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
***********************************************************************************************
Xamarin.Android.Bindings.Maven.targets
This file contains MSBuild targets used to enable @(AndroidMavenLibrary) support.
***********************************************************************************************
-->

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<UsingTask TaskName="Xamarin.Android.Tasks.MavenDownload" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />

<PropertyGroup>
<!-- Maven cache directory location -->
<MavenCacheDirectory Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$(MavenCacheDirectory)'==''">$(LocalAppData)\dotnet-android\MavenCacheDirectory\</MavenCacheDirectory>
<MavenCacheDirectory Condition="$([MSBuild]::IsOSPlatform('OSX')) and '$(MavenCacheDirectory)'==''">$(HOME)/Library/Caches/dotnet-android/MavenCacheDirectory/</MavenCacheDirectory>
<MavenCacheDirectory Condition="'$(MavenCacheDirectory)'==''">$(HOME)/.cache/dotnet-android/MavenCacheDirectory/</MavenCacheDirectory>
<MavenCacheDirectory>$([MSBuild]::EnsureTrailingSlash('$(MavenCacheDirectory)'))</MavenCacheDirectory>
</PropertyGroup>

<Target Name="MavenRestore"
Condition=" '@(AndroidMavenLibrary->Count())' != '0' "
BeforeTargets="_CategorizeAndroidLibraries"
DependsOnTargets="ResolvePackageAssets">

<!-- Download artifacts and POMs from Maven to a local cache. -->
<MavenDownload MavenCacheDirectory="$(MavenCacheDirectory)" AndroidMavenLibraries="@(AndroidMavenLibrary)">
<Output TaskParameter="ResolvedAndroidMavenLibraries" ItemName="_ResolvedAndroidMavenLibraries" />
</MavenDownload>

<!-- Add @(AndroidMavenLibrary)'s to @(AndroidLibrary)'s. -->
<ItemGroup>
<AndroidLibrary Include="@(_ResolvedAndroidMavenLibraries)" />
</ItemGroup>

</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
<Import Project="..\tools\Xamarin.Android.Common.targets" />
<Import Project="..\tools\Xamarin.Android.Bindings.Core.targets" />
<Import Project="..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
<Import Project="..\tools\Xamarin.Android.Bindings.Maven.targets" />
<Import Project="Microsoft.Android.Sdk.AndroidLibraries.targets" />
<Import Project="Microsoft.Android.Sdk.Aot.targets" Condition=" '$(AndroidApplication)' == 'true' " />
<Import Project="Microsoft.Android.Sdk.Application.targets" Condition=" '$(AndroidApplication)' == 'true' " />
Expand Down
60 changes: 59 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 48 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,51 @@ To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MS
{0} - An Android Resource Identifier.
</comment>
</data>
</root>
<data name="XA4234" xml:space="preserve">
<value>'&lt;{0}&gt;' item '{1}' is missing required metadata '{2}'</value>
<comment>{0} - The MSBuild ItemGroup Item name
{1} - The MSBuild Item ItemSpec
{2} - The omitted MSBuild Item metadata attribute</comment>
</data>
<data name="XA4235" xml:space="preserve">
<value>Maven artifact specification '{0}' is invalid. The correct format is 'group_id:artifact_id'.</value>
<comment>The following are literal names and should not be translated: Maven, group_id, artifact_id
{0} - A Maven artifact specification</comment>
</data>
<data name="XA4236" xml:space="preserve">
<value>Cannot download Maven artifact '{0}:{1}'.
- {2}: {3}
- {4}: {5}</value>
<comment>The following are literal names and should not be translated: Maven
{0} - Maven artifact group id
{1} - Maven artifact id
{2} - The .jar filename we tried to download
{3} - The HttpClient reported download exception message
{4} - The .aar filename we tried to download
{5} - The HttpClient provided download exception message</comment>
</data>
<data name="XA4237" xml:space="preserve">
<value>Cannot download POM file for Maven artifact '{0}:{1}'.
- {2}: {3}</value>
<comment>The following are literal names and should not be translated: POM, Maven
{0} - Maven artifact group id
{1} - Maven artifact id
{2} - The .pom filename we tried to download
{3} - The HttpClient reported download exception message
</comment>
</data>
<data name="XA4238" xml:space="preserve">
<value>Cannot download parent POM file for Maven artifact '{0}:{1}'.
- {2}: {3}</value>
<comment>The following are literal names and should not be translated: POM, Maven
{0} - Maven artifact group id
{1} - Maven artifact id
{2} - The .pom filename we tried to download
{3} - The HttpClient reported download exception message</comment>
</data>
<data name="XA4239" xml:space="preserve">
<value>Unknown Maven repository: '{0}'.</value>
<comment>The following are literal names and should not be translated: Maven
{0} - User supplied Maven repository type</comment>
</data>
</root>
Loading

0 comments on commit 3659766

Please sign in to comment.