Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Comment on lines +13 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we show an example of how you'd copy paste from here:

https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/4.9.3

And then turn it into our XML/MSBuild version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/4.9.3

mvnrepository is agregate repo and most packages point to MavenCentral repo.

Pobst used proper maven name for artifactId which is groupId:artifactId

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#minimal-pom

It would be nice to have FQ Maven Id with version too: groupId:artifactId:version then MsBuild Version attribute is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale for a separate Version attribute is to mirror <PackageReference> and to allow MSBuild central versioning strategies to work, like:

<AndroidMavenLibrary Include="com.squareup.okhttp3:okhttp" Version="$(OKHTTP3_VERSION)" />

or

<AndroidMavenLibrary Update="com.squareup.okhttp3:okhttp" Version="5.0.0" />

I suppose we could allow either format, but let's start with this one and see if there is any demand for alternative formats.

```

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

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