Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] allow multiple r-classes.jar files (dot…
Browse files Browse the repository at this point in the history
…net#5691)

Fixes: dotnet#5627

In some cases, Android Studio projects can produce `.aar` files that
contain:

	libs/r-classes.jar

Reviewing the contents of one of these `r-classes.jar` files:

	$ jar tf libs/r-classes.jar
	com/test/R.class

It appears Android Studio is potentially moving any `R.java` files
into this new `r-classes.jar` file.

If you have two `.aar` files that contain a `r-classes.jar` in a
Xamarin.Android application, you would currently run into the error:

	JAR library references with identical file names but different contents were found: r-classes.jar.
	Please remove any conflicting libraries from EmbeddedJar, InputJar and AndroidJavaLibrary.

Make the `<CheckDuplicateJavaLibraries/>` MSBuild task
ignore this issue for `r-classes.jar`, as it does for `classes.jar`.

I can't find any good links to explain this new file, or why it exists.

I added a test for this scenario.
  • Loading branch information
jonathanpeppers authored Mar 5, 2021
1 parent 18723b8 commit a9ca3d4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace Xamarin.Android.Tasks
{
public class CheckDuplicateJavaLibraries : AndroidTask
{
readonly static string [] ExcludedFiles = new [] {
"classes.jar",
"r-classes.jar",
};

public override string TaskPrefix => "CDJ";

public ITaskItem [] JavaSourceFiles { get; set; }
Expand All @@ -26,7 +31,7 @@ public override bool RunTask ()

// Remove duplicate identical jars by name, size and content, and reject any jars that conflicts by name (i.e. different content).
var jars = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths).ToArray ();
var dups = MonoAndroidHelper.GetDuplicateFileNames (jars, new string [] {"classes.jar"});
var dups = MonoAndroidHelper.GetDuplicateFileNames (jars, ExcludedFiles);
if (dups.Any ()) {
Log.LogCodedError ("XA1014", Properties.Resources.XA1014, String.Join (", ", dups.ToArray ()));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,5 +650,42 @@ public void LibraryProjectZipWithLint ()
"`lint.jar` should not be extracted!");
}
}

/// <summary>
/// Tests two .aar files with r-classes.jar
/// </summary>
[Test]
public void AarWithRClassesJar ()
{
var path = Path.Combine ("temp", TestName);
var lib1 = new XamarinAndroidBindingProject {
ProjectName = "Library1",
AndroidClassParser = "class-parse",
Jars = {
new AndroidItem.LibraryProjectZip ("Library1.aar") {
BinaryContent = () => ResourceData.Library1Aar
}
},
};
var lib2 = new XamarinAndroidBindingProject {
ProjectName = "Library2",
AndroidClassParser = "class-parse",
Jars = {
new AndroidItem.LibraryProjectZip ("Library2.aar") {
BinaryContent = () => ResourceData.Library2Aar
}
},
};
var app = new XamarinAndroidApplicationProject ();
app.AddReference (lib1);
app.AddReference (lib2);
using (var lib1Builder = CreateDllBuilder (Path.Combine (path, lib1.ProjectName)))
using (var lib2Builder = CreateDllBuilder (Path.Combine (path, lib2.ProjectName)))
using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) {
Assert.IsTrue (lib1Builder.Build (lib1), "Library1 build should have succeeded.");
Assert.IsTrue (lib2Builder.Build (lib2), "Library2 build should have succeeded.");
Assert.IsTrue (appBuilder.Build (app), "App build should have succeeded.");
}
}
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ static class ResourceData
static Lazy<byte[]> javaSourceJarTestJar = new Lazy<byte[]>(() => GetResourceData ("javasourcejartest.jar"));
static Lazy<byte[]> javaSourceJarTestSourcesJar = new Lazy<byte[]>(() => GetResourceData ("javasourcejartest-sources.jar"));
static Lazy<byte[]> javaSourceJarTestJavadocJar = new Lazy<byte[]>(() => GetResourceData ("javasourcejartest-javadoc.jar"));
static Lazy<byte []> library1Aar = new Lazy<byte []> (() => GetResourceData ("library1.aar"));
static Lazy<byte []> library2Aar = new Lazy<byte []> (() => GetResourceData ("library2.aar"));

public static byte[] JavaSourceJarTestJar => javaSourceJarTestJar.Value;
public static byte[] JavaSourceJarTestSourcesJar => javaSourceJarTestSourcesJar.Value;
public static byte[] JavaSourceJarTestJavadocJar => javaSourceJarTestJavadocJar.Value;
public static byte [] Library1Aar => library1Aar.Value;
public static byte [] Library2Aar => library2Aar.Value;

static byte[] GetResourceData (string name)
{
using var s = typeof (InlineData).Assembly.GetManifestResourceStream (name);
using var s = typeof (ResourceData).Assembly.GetManifestResourceStream (name);
using var m = new MemoryStream (checked ((int) s.Length));
s.CopyTo (m);
return m.ToArray ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\javasourcejartest.jar">
<LogicalName>javasourcejartest.jar</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\javasourcejartest-sources.jar">
<LogicalName>javasourcejartest-sources.jar</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\javasourcejartest-javadoc.jar">
<LogicalName>javasourcejartest-javadoc.jar</LogicalName>
<EmbeddedResource Include="Resources\*">
<LogicalName>%(FileName)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>

Expand Down

0 comments on commit a9ca3d4

Please sign in to comment.