Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] AndroidJavaSource refs dependent jars (#…
Browse files Browse the repository at this point in the history
…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
dellis1972 authored Sep 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3242de0 commit 589b6e3
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -187,6 +187,7 @@ properties that determine build ordering.
</_ResolveLibraryProjectsDependsOn>
<_CompileBindingJavaDependsOnTargets>
_AdjustJavacVersionArguments;
_GetLibraryImports;
_DetermineBindingJavaLibrariesToCompile;
_GetJavaPlatformJar;
</_CompileBindingJavaDependsOnTargets>
Original file line number Diff line number Diff line change
@@ -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 ()
{

0 comments on commit 589b6e3

Please sign in to comment.