Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] downgrade d8/r8 warning messages to `…
Browse files Browse the repository at this point in the history
…info` (#7643)

Fixes: dotnet/maui#10901

Context: https://r8.googlesource.com/r8/+/refs/tags/3.3.75/src/main/java/com/android/tools/r8/BaseCompilerCommandParser.java#246

Building a .NET MAUI project such as:

	dotnet new maui
	dotnet build -c Release -f net6.0-android -p:AndroidLinkMode=r8

Results in build warnings from R8 such as:

	R8 : warning : Missing class androidx.window.extensions.WindowExtensions

In 92bc705, we already had attempted to silence warnings from R8:

	R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.

At the time, there was no option to hide warnings, but now there is!

	--map-diagnostics[:<type>] <from-level> <to-level>
	    # Map diagnostics of <type> (default any) reported as
	    # <from-level> to <to-level> where <from-level> and
	    # <to-level> are one of 'info', 'warning', or 'error' and the
	    # optional <type> is either the simple or fully qualified
	    # Java type name of a diagnostic. If <type> is unspecified,
	    # all diagnostics at <from-level> will be mapped.
	    # Note that fatal compiler errors cannot be mapped.

We can pass:

	--map-diagnostics warning info

Which can be done in existing apps via:

	<AndroidD8ExtraArguments>--map-diagnostics warning info</AndroidD8ExtraArguments>
	<AndroidR8ExtraArguments>--map-diagnostics warning info</AndroidR8ExtraArguments>

To solve this problem, let's create a new `$(AndroidD8IgnoreWarnings)`
MSBuild property and make use of one we already have
`$(AndroidR8IgnoreWarnings)`:

	<ItemGroup>
	  <_AndroidD8MapDiagnostics Condition=" '$(AndroidD8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
	  <_AndroidR8MapDiagnostics Condition=" '$(AndroidR8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
	</ItemGroup>

We can then use these item groups to pass `--map-diagnostics` as
appropriate.  Developers can turn off either property to disable this
behavior.

We can then remove the weird `Regex` code added in 92bc705.
The existing tests from 92bc705 should also be sufficient for
testing this change.
  • Loading branch information
jonathanpeppers authored Jan 4, 2023
1 parent 22f2001 commit b7138f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
13 changes: 13 additions & 0 deletions Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,14 @@ or use `-p:AndroidCreateProguardMappingFile=False` on the command line.

This property was added in Xamarin.Android 11.2.

## AndroidD8IgnoreWarnings

Specifies `--map-diagnostics warning info` to be passed to `d8`. The
default value is `True`, but can be set to `False` to enforce more
strict behavior. See the [D8 and R8 source code][r8-source] for details.

Added in .NET 8.

## AndroidR8IgnoreWarnings

Specifies
Expand All @@ -1118,8 +1126,13 @@ to continue with dex compilation even if certain warnings are
encountered. The default value is `True`, but can be set to `False` to
enforce more strict behavior. See the [ProGuard manual](https://www.guardsquare.com/manual/configuration/usage) for details.

Starting in .NET 8, specifies `--map-diagnostics warning info`. See
the [D8 and R8 source code][r8-source] for details.

Added in Xamarin.Android 10.3.

[r8-source]: https://r8.googlesource.com/r8/+/refs/tags/3.3.75/src/main/java/com/android/tools/r8/BaseCompilerCommandParser.java#246

## AndroidR8JarPath

The path to `r8.jar` for use with the
Expand Down
29 changes: 13 additions & 16 deletions src/Xamarin.Android.Build.Tasks/Tasks/D8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Build.Utilities;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;

Expand Down Expand Up @@ -39,6 +38,7 @@ public class D8 : JavaToolTask
public ITaskItem [] JavaLibrariesToEmbed { get; set; }
public ITaskItem [] AlternativeJarLibrariesToEmbed { get; set; }
public ITaskItem [] JavaLibrariesToReference { get; set; }
public ITaskItem [] MapDiagnostics { get; set; }

public string ExtraArguments { get; set; }

Expand Down Expand Up @@ -110,22 +110,19 @@ protected virtual CommandLineBuilder GetCommandLineBuilder ()
foreach (var jar in injars)
cmd.AppendFileNameIfNotNull (jar);

return cmd;
}

/// <summary>
/// r8 tends to print:
/// Warning: Resource 'META-INF/MANIFEST.MF' already exists.
/// </summary>
static readonly Regex resourceWarning = new Regex ("Warning: Resource.+already exists", RegexOptions.IgnoreCase | RegexOptions.Compiled);

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
if (resourceWarning.IsMatch (singleLine)) {
Log.LogMessage (messageImportance, singleLine);
} else {
base.LogEventsFromTextOutput (singleLine, messageImportance);
if (MapDiagnostics != null) {
foreach (var diagnostic in MapDiagnostics) {
var from = diagnostic.ItemSpec;
var to = diagnostic.GetMetadata ("To");
if (string.IsNullOrEmpty (from) || string.IsNullOrEmpty (to))
continue;
cmd.AppendSwitch ("--map-diagnostics");
cmd.AppendSwitch (from);
cmd.AppendSwitch (to);
}
}

return cmd;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidEnableProguard Condition=" '$(AndroidLinkTool)' != '' ">True</AndroidEnableProguard>
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' And ('$(AndroidDexTool)' == 'd8' Or '$(AndroidLinkTool)' == 'r8') ">True</AndroidEnableDesugar>
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' ">False</AndroidEnableDesugar>
<AndroidD8IgnoreWarnings Condition=" '$(AndroidD8IgnoreWarnings)' == '' ">True</AndroidD8IgnoreWarnings>
<AndroidR8IgnoreWarnings Condition=" '$(AndroidR8IgnoreWarnings)' == '' ">True</AndroidR8IgnoreWarnings>
<AndroidCreateProguardMappingFile Condition="'$(AndroidCreateProguardMappingFile)' == '' And '$(AndroidLinkTool)' == 'r8'">True</AndroidCreateProguardMappingFile>

Expand Down
7 changes: 7 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.D8.targets
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Copyright (C) 2018 Xamarin. All rights reserved.
Condition=" '$(AndroidLinkTool)' != '' "
/>

<ItemGroup>
<_AndroidD8MapDiagnostics Condition=" '$(AndroidD8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
<_AndroidR8MapDiagnostics Condition=" '$(AndroidR8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
</ItemGroup>

<R8
Condition=" '$(_UseR8)' == 'True' "
ToolPath="$(JavaToolPath)"
Expand Down Expand Up @@ -77,6 +82,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
ExtraArguments="$(AndroidR8ExtraArguments)"
IntermediateOutputPath="$(IntermediateOutputPath)"
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
MapDiagnostics="@(_AndroidR8MapDiagnostics)"
/>
<D8
Condition=" '$(_UseR8)' != 'True' "
Expand All @@ -96,6 +102,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
ExtraArguments="$(AndroidD8ExtraArguments)"
IntermediateOutputPath="$(IntermediateOutputPath)"
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
MapDiagnostics="@(_AndroidD8MapDiagnostics)"
/>

<Touch Files="$(_AndroidStampDirectory)_CompileToDalvik.stamp" AlwaysCreate="true" />
Expand Down

0 comments on commit b7138f1

Please sign in to comment.