Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add XA1031 error (#7448)
Browse files Browse the repository at this point in the history
Fixes: #7326

Commit d3e8767 added a requirement that, under .NET 6+, the
`$(AndroidHttpClientHandlerType)` MUST inherit from
[`System.Net.Http.HttpMessageHandler`][0].  This was unfortunately a
change from Classic, which allowed/required that
[`System.Net.Http.HttpClientHandler`][1] be the base class.
Allowing `HttpClientHandler` to be in the inheritance hierarchy for
`$(AndroidHttpClientHandlerType)` would result in a
`NullReferenceException` at runtime under .NET 6.

Commit d3e8767 contained a TODO:

> TODO: [Emit a build-time warning][2] if
> `$(AndroidHttpClientHandlerType)` is set to an invalid value

Implement this TODO: add a build-time check that verifies that
`$(AndroidHttpClientHandlerType)` is a valid type for the target,
e.g. is an `HttpMessageHandler` subclass on .NET 6.

If `$(AndroidHttpClientHandlerType)` is not a valid type, then an
XA1031 error is raised:

	error XA1031: The 'AndroidHttpClientHandlerType' has an invalid value of '{0}' please check your project settings.

Additionally, if `$(AndroidHttpClientHandlerType)` is
`System.Net.Http.SocketsHttpHandler, System.Net.Http`, *and*
`$(UseNativeHttpHandler)` isn't set, then
set `$(UseNativeHttpHandler)` to false.  This is necessary to ensure
that `SocketsHttpHandler` is preserved by the linker.

[0]: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpmessagehandler?view=net-6.0
[1]: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler?view=net-6.0
[2]: #7326
  • Loading branch information
dellis1972 committed Dec 12, 2022
1 parent e8f1f48 commit 311b41e
Show file tree
Hide file tree
Showing 14 changed files with 494 additions and 166 deletions.
3 changes: 3 additions & 0 deletions Documentation/guides/messages/README.md
Expand Up @@ -130,6 +130,9 @@ or 'Help->Report a Problem' in Visual Studio for Mac.
+ [XA1027](xa1027.md): The 'EnableProguard' MSBuild property is set to 'true' and the 'AndroidLinkTool' MSBuild property is empty, so 'AndroidLinkTool' will default to 'proguard'.
+ [XA1028](xa1028.md): The 'AndroidEnableProguard' MSBuild property is set to 'true' and the 'AndroidLinkTool' MSBuild property is empty, so 'AndroidLinkTool' will default to 'proguard'.
+ [XA1029](xa1029.md): The 'AotAssemblies' MSBuild property is deprecated. Edit the project file in a text editor to remove this property, and use the 'RunAOTCompilation' MSBuild property instead.
+ [XA1031](xa1031.md): The 'AndroidHttpClientHandlerType' has an invalid value.
+ [XA1032](xa1032.md):Failed to resolve '{0}' from '{1}'. Please check your `AndroidHttpClientHandlerType` setting.
+ [XA1033](xa1033.md): Could not resolve '{0}'. Please check your `AndroidHttpClientHandlerType` setting.

## XA2xxx: Linker

Expand Down
20 changes: 20 additions & 0 deletions Documentation/guides/messages/xa1031.md
@@ -0,0 +1,20 @@
---
title: Xamarin.Android error XA1031
description: XA1031 error code
ms.date: 10/10/2022
---
# Xamarin.Android error XA1031

## Example messages

```
The 'AndroidHttpClientHandlerType' property value 'Foo.Bar.HttpHander, MyApp' must derive from 'System.Net.Http.HttpMessageHandler'.
Please change the value to an assembly-qualifed type name which inherits from '{1}' or remove the property completely.
```

## Solution

Edit your csproj directly and change the 'AndroidHttpClientHandlerType' to
a valid value.

Valid values can be found at `~/android/deploy-test/building-apps/build-properties.md#AndroidHttpClientHandlerType`.
19 changes: 19 additions & 0 deletions Documentation/guides/messages/xa1032.md
@@ -0,0 +1,19 @@
---
title: Xamarin.Android error XA1032
description: XA1032 error code
ms.date: 10/10/2022
---
# Xamarin.Android error XA1032

## Example messages

```
Failed to resolve '{0}' from '{1}'. Please check your `AndroidHttpClientHandlerType` setting.
```

## Solution

Edit your csproj directly and change the 'AndroidHttpClientHandlerType' to
a valid value.

Valid values can be found at `~/android/deploy-test/building-apps/build-properties.md#AndroidHttpClientHandlerType`.
19 changes: 19 additions & 0 deletions Documentation/guides/messages/xa1033.md
@@ -0,0 +1,19 @@
---
title: Xamarin.Android error XA1033
description: XA1033 error code
ms.date: 10/10/2022
---
# Xamarin.Android error XA1033

## Example messages

```
Could not resolve '{0}'. Please check your `AndroidHttpClientHandlerType` setting.
```

## Solution

Edit your csproj directly and change the 'AndroidHttpClientHandlerType' to
a valid value.

Valid values can be found at `~/android/deploy-test/building-apps/build-properties.md#AndroidHttpClientHandlerType`.
Expand Up @@ -135,10 +135,11 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_IncludeLayoutBindingSources;
AddLibraryJarsToBind;
$(CompileDependsOn);
_CheckAndroidHttpClientHandlerType;
</CompileDependsOn>
<CoreCompileDependsOn>
UpdateGeneratedFiles;
$(CoreCompileDependsOn)
$(CoreCompileDependsOn);
</CoreCompileDependsOn>
<DeferredBuildDependsOn>
$(DeferredBuildDependsOn);
Expand Down
Expand Up @@ -95,6 +95,7 @@
<HttpActivityPropagationSupport Condition="'$(HttpActivityPropagationSupport)' == ''">false</HttpActivityPropagationSupport>
<InvariantGlobalization Condition="'$(InvariantGlobalization)' == ''">false</InvariantGlobalization>
<StartupHookSupport Condition="'$(StartupHookSupport)' == ''">false</StartupHookSupport>
<UseNativeHttpHandler Condition=" $(AndroidHttpClientHandlerType.Contains ('System.Net.Http.SocketsHttpHandler')) And '$(UseNativeHttpHandler)' == '' ">false</UseNativeHttpHandler>
<UseNativeHttpHandler Condition="'$(UseNativeHttpHandler)' == ''">true</UseNativeHttpHandler>
<_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true</_AggressiveAttributeTrimming>
<NullabilityInfoContextSupport Condition="'$(NullabilityInfoContextSupport)' == ''">false</NullabilityInfoContextSupport>
Expand Down

0 comments on commit 311b41e

Please sign in to comment.