Skip to content

Commit 3cedec4

Browse files
[Xamarin.Android.Build.Tasks] Add more warning codes
Context: #1560 * I investigated the code paths that led into the `LogWarning()` in `GenerateResourceDesigner.AddRename()`. I could not find any way to hit that message with the current code. Moreover, the only reports of the message I could find on the web were from capitalization mismatches, and those are now explicitly ignored by the use of the `StringComparison.OrdinalIgnoreCase` argument in the call to `string.Compare()`. Since this message does not currently indicate an actionable problem, I changed it from a warning to a debug message. * Neither of the warnings in `GetAdditionalResourcesFromAssemblies.AddAttributeValue()` is actionable for Xamarin.Android app developers. The first warning can only arise if a new attribute name has been added to the `switch` statement in `DoExecute()`. The second warning can only occur in a library that uses one of those attributes. Since the attributes are not formally documented, they are currently only intended for use by the Xamarin team. Since these warnings aren't aimed at end-users, they don't need to have their own codes. So for now, I just updated the warnings to reuse the `errorCode`. * The first warning in `GetAdditionalResourcesFromAssemblies.MakeSureLibraryIsInPlace()` is just an informational message. The download step is *expected* to be skipped during design-time builds. I changed the warning to a debug message accordingly. * I changed the warning in `ReadAdditionalResourcesFromAssemblyCache` to a debug message because it is just informational. For example, in the normal process of building application projects, the Xamarin.Android.Common.targets checks if `CacheFile` exists before it invokes the task, so an absent `CacheFile` is often normal. In cases where the warning could be actionable for an end-user, the warning would indicate that something went wrong earlier when creating the `CacheFile` in `GetAdditionalResourcesFromAssemblies`, so even in that case this warning would not be needed because more actionable messages should come from the earlier task. On top of that, this whole old mechanism of downloading external files is rarely used now that the large majority of the NuGet packages from the Xamarin team have been updated to use the newer Xamarin.Build.Download mechanism. * I also changed the warnings in `ReadImportedLibrariesCache` and `ReadLibraryProjectImportsCache` to debug messages for similar reasons. A missing cache file in one of these tasks indicates that something went wrong during the corresponding earlier `GetImportedLibraries` or `ResolveLibraryProjectImports` task, so any actionable messages would need to get logged in the earlier task instead. * The warnings in `ResolveAssemblies.ResolveRuntimeAssemblyForReferenceAssembly()` should not occur during normal use. They both depend on *generated* content related to the NuGet target framework moniker. Based on these considerations, I assigned just one shared warning code to both warnings for now.
1 parent 81e7306 commit 3cedec4

12 files changed

+78
-15
lines changed

Documentation/guides/messages/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@
4141
+ [XA0113](xa0113.md): Google Play requires that new applications must use a `$(TargetFrameworkVersion)` of v8.0 (API level 26) or above.
4242
+ [XA0114](xa0114.md): Google Play requires that application updates must use a `$(TargetFrameworkVersion)` of v8.0 (API level 26) or above.
4343
+ [XA0115](xa0115.md): Invalid value 'armeabi' in $(AndroidSupportedAbis). This ABI is no longer supported. Please update your project properties.
44+
+ [XA0118](xa0118.md): Could not parse '{TargetMoniker}'
4445

4546
### XA1xxx Project Related
4647

4748
+ [XA1000](xa1000.md): There was an problem parsing {file}. This is likely due to incomplete or invalid xml.
4849
+ [XA1001](xa1001.md): AndroidResgen: Warning while updating Resource XML '{filename}': {Message}
4950
+ [XA1002](xa1002.md): We found a matching key '{Key}' for '{Item}'. But the casing was incorrect. Please correct the casing
5051
+ [XA1003](xa1003.md): '{zip}' does not exist. Please rebuild the project.
52+
+ [XA1004](xa1004.md): Attempting naive type name fixup for element with ID '{id}' and type '{managedType}'
5153

5254
### XA2xxx Linker
5355

5456
### XA3xxx AOT
5557

5658
### XA4xxx Code Generation
5759

60+
+ [XA4214](xa4214.md): Duplicate managed type found! Mappings between managed types and Java types must be unique. First Type: '{0}'; Second Type: '{1}'.
61+
+ [XA4215](xa4215.md): Duplicate Java type found! Mappings between managed types and Java types must be unique. First Type: '{0}'; Second Type: '{1}'.
5862
+ [XA4301](xa4301.md): : Apk already contains the item `xxx`.
5963

6064
### XA5xxx GCC and toolchain
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Compiler Warning XA1004
2+
3+
When [Layout Bindings and Code-Behind][code-behind] are enabled, this warning
4+
will be emitted for every layout element that has the `//*/@android:id`
5+
attribute set and uses a fully-qualified name for the element type.
6+
7+
For example, the warning *will* be emitted for both of the following elements:
8+
9+
```xml
10+
<android.widget.TextView
11+
android:id="@+id/text1" />
12+
<Android.Widget.TextView
13+
android:id="@+id/text2" />
14+
```
15+
16+
But it will *not* be emitted for elements that use just an unqualified class
17+
name like:
18+
19+
```xml
20+
<TextView
21+
android:id="@+id/text1" />
22+
```
23+
24+
The "naive type name fixup" tries to ensure that any fully-qualified type name
25+
is a C# name rather than a Java name. First it checks a short list of known
26+
mappings between Java namespaces and C# namespaces, such as the mapping of
27+
`android.view` to `Android.Views`. For any remaining namespaces, it splits the
28+
namespace on `.` and capitalizes each part.
29+
30+
To resolve this warning, change each element to use its unqualified C# class
31+
name or add a [`xamarin:managedType`][code-behind-attributes] attribute to each
32+
element that specifies the fully qualified C# name.
33+
34+
Example message:
35+
- `warning XA1004: Attempting naive type name fixup for element with ID '@+id/text1' and type 'android.widget.TextView'`
36+
`warning XA1004: If the above fixup fails, please add `xamarin:managedType` attribute to the element with fully qualified managed type name of the element.`
37+
38+
[code-behind]: ../LayoutCodeBehind.md
39+
[code-behind-attributes]: ../LayoutCodeBehind.md#layouqt-xml-attributes
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Compiler Warning XA4214
2+
3+
If the same fully qualified C# type name exists in two or more assemblies and
4+
both C# types inherit from `Java.Lang.Object`, then the build process will only
5+
generate an Android Callable Wrapper for *one* of them.
6+
7+
Example message:
8+
- `warning XA4214: Duplicate managed type found! Mappings between managed types and Java types must be unique. First Type: 'Library.Class1, Library1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; Second Type: 'Library.Class1, Library2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.`
9+
- `warning XA4214: References to the type 'Library.Class1' will refer to 'Library.Class1, Library1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Compiler Error XA4215
2+
3+
This error indicates that two or more C# types are emitting the same fully
4+
qualified Java type name. To resolve this error, change the `[Register]`
5+
attribute on one of the C# types to a different Java type name.
6+
7+
Example message:
8+
- `error XA4215: Duplicate Java type found! Mappings between managed types and Java types must be unique. First Type: 'Library.Class1, Library1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; Second Type: 'Library.Class2, Library1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'`

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ void CreateWidget (XPathNavigator current, string filePath, string androidNS, st
317317
managedType = managedType.Substring (0, idx).Trim ();
318318

319319
if (mayNeedTypeFixup && (idx = managedType.LastIndexOf ('.')) >= 0) {
320-
LogWarning ($"Attempting naive type name fixup for element with ID '{id}' and type '{managedType}'");
321-
LogMessage ("If the above fixup fails, please add `tools:managedType` attribute to the element with fully qualified managed type name of the element");
320+
LogCodedWarning ("XA1004", $"Attempting naive type name fixup for element with ID '{id}' and type '{managedType}'");
321+
LogCodedWarning ("XA1004", "If the above fixup fails, please add `xamarin:managedType` attribute to the element with fully qualified managed type name of the element.");
322322

323323
oldType = managedType;
324324
string ns = managedType.Substring (0, idx);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,23 @@ void Run (DirectoryAssemblyResolver res)
177177

178178
TypeDefinition conflict;
179179
if (managed.TryGetValue (managedKey, out conflict)) {
180-
Log.LogWarning (
180+
Log.LogCodedWarning (
181+
"XA4214",
181182
"Duplicate managed type found! Mappings between managed types and Java types must be unique. " +
182183
"First Type: '{0}'; Second Type: '{1}'.",
183184
conflict.GetAssemblyQualifiedName (),
184185
type.GetAssemblyQualifiedName ());
185-
Log.LogWarning (
186+
Log.LogCodedWarning (
187+
"XA4214",
186188
"References to the type '{0}' will refer to '{1}'.",
187189
managedKey, conflict.GetAssemblyQualifiedName ());
188190
continue;
189191
}
190192
if (java.TryGetValue (javaKey, out conflict)) {
191-
Log.LogError (
193+
Log.LogCodedError (
194+
"XA4215",
192195
"Duplicate Java type found! Mappings between managed types and Java types must be unique. " +
193-
"First Type: '{0}'; Second Type: '{1}'",
196+
"First Type: '{0}'; Second Type: '{1}'.",
194197
conflict.GetAssemblyQualifiedName (),
195198
type.GetAssemblyQualifiedName ());
196199
keep_going = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void AddRename (string android, string user)
267267
var ext = Path.GetExtension (android);
268268
var dir = Path.GetDirectoryName (user);
269269

270-
Log.LogWarning ("Resource target names differ; got '{0}', expected '{1}'.",
270+
Log.LogDebugMessage ("Resource target names differ; got '{0}', expected '{1}'.",
271271
Path.Combine (dir, Path.GetFileName (to) + ext),
272272
Path.Combine (dir, Path.GetFileName (curTo) + ext));
273273
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ void AddAttributeValue (ICollection<string> items, CustomAttribute attr, string
108108
bool isDirectory, string fullPath)
109109
{
110110
if (!attr.HasConstructorArguments || attr.ConstructorArguments.Count != 1) {
111-
LogWarning ("Attribute {0} doesn't have expected one constructor agrument", attr.AttributeType.FullName);
111+
LogCodedWarning (errorCode, "Attribute {0} doesn't have expected one constructor agrument", attr.AttributeType.FullName);
112112
return;
113113
}
114114

115115
CustomAttributeArgument arg = attr.ConstructorArguments.First ();
116116
string path = arg.Value as string;
117117
if (string.IsNullOrEmpty (path)) {
118-
LogWarning ("Attribute {0} contructor argument is empty or not set to string", attr.AttributeType.FullName);
118+
LogCodedWarning (errorCode, "Attribute {0} contructor argument is empty or not set to string", attr.AttributeType.FullName);
119119
return;
120120
}
121121
path = SubstituteEnvVariables (path).TrimEnd (Path.DirectorySeparatorChar);
@@ -298,7 +298,7 @@ string MakeSureLibraryIsInPlace (string destinationBase, string url, string vers
298298
string file = Path.Combine (zipDir, !uri.IsFile ? hash + ".zip" : Path.GetFileName (uri.AbsolutePath));
299299
if (string.IsNullOrEmpty (extraPath) && (!File.Exists (file) || !IsValidDownload (file, sha1) || !MonoAndroidHelper.IsValidZip (file))) {
300300
if (DesignTimeBuild) {
301-
LogWarning ($"DesignTimeBuild={DesignTimeBuild}. Skipping download of {url}");
301+
LogDebugMessage ($"DesignTimeBuild={DesignTimeBuild}. Skipping download of {url}");
302302
return null;
303303
}
304304

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override bool Execute ()
3737
Log.LogDebugMessage ("Task ReadAdditionalResourcesFromAssemblyCache");
3838
Log.LogDebugMessage (" CacheFile: {0}", CacheFile);
3939
if (!File.Exists (CacheFile)) {
40-
Log.LogWarning ("{0} does not exist. No Additional Resources found", CacheFile);
40+
Log.LogDebugMessage ("{0} does not exist. No Additional Resources found", CacheFile);
4141
return !Log.HasLoggedErrors;
4242
}
4343
var doc = XDocument.Load (CacheFile);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override bool Execute ()
5151
Log.LogDebugMessage ("Task ReadImportedLibrariesCache");
5252
Log.LogDebugMessage (" CacheFile: {0}", CacheFile);
5353
if (!File.Exists (CacheFile)) {
54-
Log.LogWarning ("{0} does not exist. No Imported Libraries found", CacheFile);
54+
Log.LogDebugMessage ("{0} does not exist. No Imported Libraries found", CacheFile);
5555
return !Log.HasLoggedErrors;
5656
}
5757
var doc = XDocument.Load (CacheFile);

0 commit comments

Comments
 (0)