From 589b6e30aa015d313d99321eb0ffae1e754022fe Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 21 Sep 2023 22:38:45 +0100 Subject: [PATCH] [Xamarin.Android.Build.Tasks] AndroidJavaSource refs dependent jars (#8194) Fixes: https://github.com/xamarin/xamarin-android/issues/8191 The implementation of `@(AndroidJavaSource)` was missing one component: if your project contains `@(PackageReference)`s to NuGet packages which pull in `.jar` files, those `.jar` files would *not* be referenced when building `@(AndroidJavaSource)`. For example, if you reference the [Xamarin.AndroidX.AppCompat][0] package, there is an expectation that `@(AndroidJavaSource)` code should be able to use the type `androidx.appcompat.widget.Toolbar`. Unfortunately, this would fail: Error JAVAC0000 error: package androidx.appcompat.widget does not exist This would fail because the `javac` invocation was missing references to the `classes.jar` files which are extracted into the `$(IntemediateOutputPath)lp` directory. Update the (internal) `$(_CompileBindingJavaDependsOnTargets)` property so that the `_GetLibraryImports` target is a dependency. This will call the chain of targets which extracts the dependent `classes.jar` files and populates the `@(Jars)` ItemGroup which is used in the `_CompileBindingJava` target. This should allow users to write simple wrapper methods in Java that wrap more complex APIs. [0]: https://www.nuget.org/packages/Xamarin.AndroidX.AppCompat/1.1.0.1 --- .../Microsoft.Android.Sdk.BuildOrder.targets | 1 + .../Xamarin.Android.Build.Tests/BuildTest.cs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets index 4a4a3432258..4bfa0ff3d74 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets @@ -187,6 +187,7 @@ properties that determine build ordering. <_CompileBindingJavaDependsOnTargets> _AdjustJavacVersionArguments; + _GetLibraryImports; _DetermineBindingJavaLibrariesToCompile; _GetJavaPlatformJar; diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index 0d4182e2003..9405f155328 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -1554,6 +1554,35 @@ public void CheckLintConfigMerging () } } + [Test] + public void BuildApplicationWithJavaSourceUsingAndroidX ([Values(true, false)] bool isRelease) + { + var proj = new XamarinAndroidApplicationProject () { + IsRelease = isRelease, + OtherBuildItems = { + new BuildItem (AndroidBuildActions.AndroidJavaSource, "ToolbarEx.java") { + TextContent = () => @"package com.unnamedproject.unnamedproject; +import android.content.Context; +import androidx.appcompat.widget.Toolbar; +public class ToolbarEx { + public static Toolbar GetToolbar (Context context) { + return new Toolbar (context); + } +} +", + Encoding = Encoding.ASCII + }, + } + }; + proj.PackageReferences.Add (KnownPackages.AndroidXAppCompat); + using (var b = CreateApkBuilder ()) { + b.ThrowOnBuildFailure = false; + Assert.IsTrue (b.Build (proj), "Build should have succeeded"); + + Assert.IsTrue (b.Clean (proj), "Clean should have succeeded."); + } + } + [Test] public void BuildApplicationCheckThatAddStaticResourcesTargetDoesNotRerun () {