Skip to content

MavenDependencyVerifierTask Advanced Scenarios

Jonathan Pobst edited this page Oct 6, 2022 · 1 revision

When a Java dependency needs to be fulfilled, ideally there is already a NuGet package that has a binding for it. However there are additional ways to fulfill dependencies.

<PackageReference>

When a <PackageReference> is encountered, it is examined to determine which Java dependencies, if any, it fulfills. This is done by looking for a specially crafted NuGet tag. For example, for the Kotlin StdLib, the tag looks like this:

<!-- artifact={GroupId}:{ArtifactId}:{Java Library Version} -->
<PackageTags>artifact=org.jetbrains.kotlin:kotlin-stdlib:1.7.10</PackageTags>

However there may be NuGet packages which fulfill a dependency but have not had this metadata added to it. In this case, you will need to explicitly specify which dependency the package contains with JavaArtifact and JavaVersion:

<PackageReference 
  Include="Xamarin.Kotlin.StdLib" 
  Version="1.7.10" 
  JavaArtifact="org.jetbrains.kotlin:kotlin-stdlib" 
  JavaVersion="1.7.10" />

With this, the binding process knows the Java dependency is satisfied by the NuGet package.

Note: NuGet packages specify their own dependencies, so you will not need to worry about transitive dependencies.

<ProjectReference>

If the needed Java dependency is provided by another project in your solution, you can annotate the <ProjectReference> to specify the dependency it fulfills:

<ProjectReference 
  Include="..\My.Other.Binding\My.Other.Binding.csproj" 
  JavaArtifact="my.other.binding:helperlib" 
  JavaVersion="1.0.0" />

With this, the binding process knows the Java dependency is satisfied by the referenced project.

Note: Each project specifies their own dependencies, so you will not need to worry about transitive dependencies.

<AndroidMavenLibrary>

If you are creating a public NuGet package, you will want to follow NuGet's "one library per package" policy so that the NuGet dependency graph works. However, if creating a binding for private use, you may want to include your Java dependencies directly inside the parent binding.

This can be done by adding additional <AndroidMavenLibrary> items to the project:

<ItemGroup>
  <AndroidMavenLibrary Include="my.library:main-library" Version="1.0.0" />
  <AndroidMavenLibrary Include="my.library:dependency-library" Version="1.0.0" />
</ItemGroup>

If you only want to include the Java library but not produce C# bindings for it, you can mark it with Bind="false":

<ItemGroup>
  <AndroidMavenLibrary Include="my.library:main-library" Version="1.0.0" />
  <AndroidMavenLibrary Include="my.library:dependency-library" Version="1.0.0" Bind="false" />
</ItemGroup>

Note: If the dependency library has its own dependencies, you will be required to ensure they are fulfilled.

<IgnoredMavenDependency>

As a last resort, a needed Java dependency can be ignored. An example of when this is useful is if the dependency library is a collection of Java annotations that are only used at compile type and not runtime.

Note that while the error message will go away, it does not mean the package will magically work. If the dependency is actually needed at runtime and not provided the Android application will crash with a Java.Lang.NoClassDefFoundError error.

<ItemGroup>
  <IgnoredMavenDependency Include="com.google.errorprone:error_prone_annotations" Version="2.15.0" />
</ItemGroup>