Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] fix Debug mode and $(PublishTrimmed) (#…
Browse files Browse the repository at this point in the history
…9452)

There is a problem if you have the combination:

* `Configuration=Debug`

* `PublishTrimmed=true`

We emit an `XA0119` warning with this combination, as there is not a good reason to do it.

But unfortunately, the build will be completely broken as all the .NET assemblies don't make it to the `.apk`! I could reproduce this in a test.

The fix is that the logic in the `<ProcessAssemblies/>` MSBuild task:

    if (PublishTrimmed && !AndroidIncludeDebugSymbols) {
        //...
        ShrunkAssemblies = shrunkAssemblies.ToArray ();
    }

Has a case, where we did not fill out the `ShrunkAssemblies` `[Output]` item group:

    if (!AndroidIncludeDebugSymbols) {
        // existing logic...
    } else {
        ShrunkAssemblies = OutputAssemblies;
    }

Going forward, we could probably refactor some of this logic to make things simpler, but this is a reasonable fix for now.
  • Loading branch information
jonathanpeppers authored Oct 30, 2024
1 parent aa668a5 commit 1417776
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Xamarin.Android.Build.Tasks/Tasks/ProcessAssemblies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ public override bool RunTask ()

// Set ShrunkAssemblies for _RemoveRegisterAttribute and <BuildApk/>
// This should match the Condition on the _RemoveRegisterAttribute target
if (PublishTrimmed && !AndroidIncludeDebugSymbols) {
var shrunkAssemblies = new List<ITaskItem> (OutputAssemblies.Length);
foreach (var assembly in OutputAssemblies) {
var dir = Path.GetDirectoryName (assembly.ItemSpec);
var file = Path.GetFileName (assembly.ItemSpec);
shrunkAssemblies.Add (new TaskItem (assembly) {
ItemSpec = Path.Combine (dir, "shrunk", file),
});
if (PublishTrimmed) {
if (!AndroidIncludeDebugSymbols) {
var shrunkAssemblies = new List<ITaskItem> (OutputAssemblies.Length);
foreach (var assembly in OutputAssemblies) {
var dir = Path.GetDirectoryName (assembly.ItemSpec);
var file = Path.GetFileName (assembly.ItemSpec);
shrunkAssemblies.Add (new TaskItem (assembly) {
ItemSpec = Path.Combine (dir, "shrunk", file),
});
}
ShrunkAssemblies = shrunkAssemblies.ToArray ();
} else {
ShrunkAssemblies = OutputAssemblies;
}
ShrunkAssemblies = shrunkAssemblies.ToArray ();
}

if (InputJavaLibraries != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ public void CheckR8InfoMessagesToNotBreakTheBuild ()
}
}

[Test]
public void CheckDebugModeWithTrimming ()
{
bool usesAssemblyStores = false;
var proj = new XamarinAndroidApplicationProject {
ProjectName = "MyApp",
IsRelease = false,
EmbedAssembliesIntoApk = true,
};
proj.SetProperty ("PublishTrimmed", "true");
proj.SetProperty ("AndroidUseAssemblyStore", usesAssemblyStores.ToString ());

using var b = CreateApkBuilder ();
Assert.IsTrue (b.Build (proj), "build should have succeeded.");

var apk = Path.Combine (Root, b.ProjectDirectory,
proj.OutputPath, $"{proj.PackageName}-Signed.apk");
var helper = new ArchiveAssemblyHelper (apk, usesAssemblyStores);
helper.Contains (["Mono.Android.dll", $"{proj.ProjectName}.dll"], out _, out var missingFiles, out _, [AndroidTargetArch.Arm64, AndroidTargetArch.X86_64]);

Assert.IsTrue (missingFiles == null || missingFiles.Count == 0,
string.Format ("The following Expected files are missing. {0}",
string.Join (Environment.NewLine, missingFiles)));
}

[Test]
[NonParallelizable] // Commonly fails NuGet restore
public void CheckIncludedAssemblies ([Values (false, true)] bool usesAssemblyStores)
Expand Down

0 comments on commit 1417776

Please sign in to comment.