Skip to content

Commit 589b6e3

Browse files
authored
[Xamarin.Android.Build.Tasks] AndroidJavaSource refs dependent jars (#8194)
Fixes: #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
1 parent 3242de0 commit 589b6e3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ properties that determine build ordering.
187187
</_ResolveLibraryProjectsDependsOn>
188188
<_CompileBindingJavaDependsOnTargets>
189189
_AdjustJavacVersionArguments;
190+
_GetLibraryImports;
190191
_DetermineBindingJavaLibrariesToCompile;
191192
_GetJavaPlatformJar;
192193
</_CompileBindingJavaDependsOnTargets>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,35 @@ public void CheckLintConfigMerging ()
15541554
}
15551555
}
15561556

1557+
[Test]
1558+
public void BuildApplicationWithJavaSourceUsingAndroidX ([Values(true, false)] bool isRelease)
1559+
{
1560+
var proj = new XamarinAndroidApplicationProject () {
1561+
IsRelease = isRelease,
1562+
OtherBuildItems = {
1563+
new BuildItem (AndroidBuildActions.AndroidJavaSource, "ToolbarEx.java") {
1564+
TextContent = () => @"package com.unnamedproject.unnamedproject;
1565+
import android.content.Context;
1566+
import androidx.appcompat.widget.Toolbar;
1567+
public class ToolbarEx {
1568+
public static Toolbar GetToolbar (Context context) {
1569+
return new Toolbar (context);
1570+
}
1571+
}
1572+
",
1573+
Encoding = Encoding.ASCII
1574+
},
1575+
}
1576+
};
1577+
proj.PackageReferences.Add (KnownPackages.AndroidXAppCompat);
1578+
using (var b = CreateApkBuilder ()) {
1579+
b.ThrowOnBuildFailure = false;
1580+
Assert.IsTrue (b.Build (proj), "Build should have succeeded");
1581+
1582+
Assert.IsTrue (b.Clean (proj), "Clean should have succeeded.");
1583+
}
1584+
}
1585+
15571586
[Test]
15581587
public void BuildApplicationCheckThatAddStaticResourcesTargetDoesNotRerun ()
15591588
{

0 commit comments

Comments
 (0)