Skip to content

Commit b7138f1

Browse files
[Xamarin.Android.Build.Tasks] downgrade d8/r8 warning messages to 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.
1 parent 22f2001 commit b7138f1

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

Documentation/guides/building-apps/build-properties.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ or use `-p:AndroidCreateProguardMappingFile=False` on the command line.
11101110

11111111
This property was added in Xamarin.Android 11.2.
11121112

1113+
## AndroidD8IgnoreWarnings
1114+
1115+
Specifies `--map-diagnostics warning info` to be passed to `d8`. The
1116+
default value is `True`, but can be set to `False` to enforce more
1117+
strict behavior. See the [D8 and R8 source code][r8-source] for details.
1118+
1119+
Added in .NET 8.
1120+
11131121
## AndroidR8IgnoreWarnings
11141122

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

1129+
Starting in .NET 8, specifies `--map-diagnostics warning info`. See
1130+
the [D8 and R8 source code][r8-source] for details.
1131+
11211132
Added in Xamarin.Android 10.3.
11221133

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

11251138
The path to `r8.jar` for use with the

src/Xamarin.Android.Build.Tasks/Tasks/D8.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Build.Utilities;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Text.RegularExpressions;
65
using Xamarin.Android.Tools;
76
using Microsoft.Android.Build.Tasks;
87

@@ -39,6 +38,7 @@ public class D8 : JavaToolTask
3938
public ITaskItem [] JavaLibrariesToEmbed { get; set; }
4039
public ITaskItem [] AlternativeJarLibrariesToEmbed { get; set; }
4140
public ITaskItem [] JavaLibrariesToReference { get; set; }
41+
public ITaskItem [] MapDiagnostics { get; set; }
4242

4343
public string ExtraArguments { get; set; }
4444

@@ -110,22 +110,19 @@ protected virtual CommandLineBuilder GetCommandLineBuilder ()
110110
foreach (var jar in injars)
111111
cmd.AppendFileNameIfNotNull (jar);
112112

113-
return cmd;
114-
}
115-
116-
/// <summary>
117-
/// r8 tends to print:
118-
/// Warning: Resource 'META-INF/MANIFEST.MF' already exists.
119-
/// </summary>
120-
static readonly Regex resourceWarning = new Regex ("Warning: Resource.+already exists", RegexOptions.IgnoreCase | RegexOptions.Compiled);
121-
122-
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
123-
{
124-
if (resourceWarning.IsMatch (singleLine)) {
125-
Log.LogMessage (messageImportance, singleLine);
126-
} else {
127-
base.LogEventsFromTextOutput (singleLine, messageImportance);
113+
if (MapDiagnostics != null) {
114+
foreach (var diagnostic in MapDiagnostics) {
115+
var from = diagnostic.ItemSpec;
116+
var to = diagnostic.GetMetadata ("To");
117+
if (string.IsNullOrEmpty (from) || string.IsNullOrEmpty (to))
118+
continue;
119+
cmd.AppendSwitch ("--map-diagnostics");
120+
cmd.AppendSwitch (from);
121+
cmd.AppendSwitch (to);
122+
}
128123
}
124+
125+
return cmd;
129126
}
130127
}
131128
}

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
261261
<AndroidEnableProguard Condition=" '$(AndroidLinkTool)' != '' ">True</AndroidEnableProguard>
262262
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' And ('$(AndroidDexTool)' == 'd8' Or '$(AndroidLinkTool)' == 'r8') ">True</AndroidEnableDesugar>
263263
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' ">False</AndroidEnableDesugar>
264+
<AndroidD8IgnoreWarnings Condition=" '$(AndroidD8IgnoreWarnings)' == '' ">True</AndroidD8IgnoreWarnings>
264265
<AndroidR8IgnoreWarnings Condition=" '$(AndroidR8IgnoreWarnings)' == '' ">True</AndroidR8IgnoreWarnings>
265266
<AndroidCreateProguardMappingFile Condition="'$(AndroidCreateProguardMappingFile)' == '' And '$(AndroidLinkTool)' == 'r8'">True</AndroidCreateProguardMappingFile>
266267

src/Xamarin.Android.Build.Tasks/Xamarin.Android.D8.targets

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Copyright (C) 2018 Xamarin. All rights reserved.
4848
Condition=" '$(AndroidLinkTool)' != '' "
4949
/>
5050

51+
<ItemGroup>
52+
<_AndroidD8MapDiagnostics Condition=" '$(AndroidD8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
53+
<_AndroidR8MapDiagnostics Condition=" '$(AndroidR8IgnoreWarnings)' == 'true' " Include="warning" To="info" />
54+
</ItemGroup>
55+
5156
<R8
5257
Condition=" '$(_UseR8)' == 'True' "
5358
ToolPath="$(JavaToolPath)"
@@ -77,6 +82,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
7782
ExtraArguments="$(AndroidR8ExtraArguments)"
7883
IntermediateOutputPath="$(IntermediateOutputPath)"
7984
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
85+
MapDiagnostics="@(_AndroidR8MapDiagnostics)"
8086
/>
8187
<D8
8288
Condition=" '$(_UseR8)' != 'True' "
@@ -96,6 +102,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
96102
ExtraArguments="$(AndroidD8ExtraArguments)"
97103
IntermediateOutputPath="$(IntermediateOutputPath)"
98104
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
105+
MapDiagnostics="@(_AndroidD8MapDiagnostics)"
99106
/>
100107

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

0 commit comments

Comments
 (0)