Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add XA1031 error for `AndroidHttpClient…
Browse files Browse the repository at this point in the history
…HandlerType`

Fixes dotnet#7326

We should check the value of the `AndroidHttpClientHandlerType` property
during the build process. This property can have a number of
different values depending on what version of Xamarin.Android is being
used.

Under Classic a valid value is `Xamarin.Android.Net.AndroidClientHandler`,
however under .NET 6+ the valid values are

- `Xamarin.Android.Net.AndroidMessageHandler`.
- `System.Net.Http.SocketsHttpHandler, System.Net.Http`.

We should raise an error if the user is using an invalid
value.
  • Loading branch information
dellis1972 committed Oct 10, 2022
1 parent 5044377 commit ed9f7fc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/guides/messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ 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.

## XA2xxx: Linker

Expand Down
20 changes: 20 additions & 0 deletions Documentation/guides/messages/xa1031.md
Original file line number Diff line number Diff line change
@@ -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' has an invalid value of 'xxx' please check your project settings.
```

## Solution

Open your project settings in Visual Studio and check that the
'AndroidHttpClientHandlerType' has a valid value. Alternatively
you can edit your csproj directly and change the 'AndroidHttpClientHandlerType'.

Valid values can be found at `~/android/deploy-test/building-apps/build-properties.md#AndroidHttpClientHandlerType`.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
<PropertyGroup Condition=" '$(AndroidApplication)' == 'True' ">
<BuildDependsOn>
_ValidateLinkMode;
_CheckAndroidHttpClientHandlerType;
_CheckNonIdealConfigurations;
_SetupMSBuildAllProjects;
_SetupDesignTimeBuildForBuild;
Expand Down
5 changes: 5 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ In this message, the term "binding" means a piece of generated code that makes i
<value>The 'RunAOTCompilation' MSBuild property is only supported when trimming is enabled. Edit the project file in a text editor to set 'PublishTrimmed' to 'true' for this build configuration.</value>
<comment>The following are literal names and should not be translated: 'RunAOTCompilation', 'PublishTrimmed'</comment>
</data>
<data name="XA1031" xml:space="preserve">
<value>The 'AndroidHttpClientHandlerType' has an invalid value of '{0}' please check your project settings.</value>
<comment>The following are literal names and should not be translated: 'AndroidHttpClientHandlerType',
{0} - The value of the property.</comment>
</data>
<data name="XA2000" xml:space="preserve">
<value>Use of AppDomain.CreateDomain() detected in assembly: {0}. .NET 6 and higher will only support a single AppDomain, so this API will no longer be available in Xamarin.Android once .NET 6 is released.</value>
<comment>The following are literal names and should not be translated: AppDomain.CreateDomain(), AppDomain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ public void CheckBuildIdIsUnique ([Values ("apk", "aab")] string packageFormat)
}
}

[Test]
public void CheckForInvalidHttpClientHandlerType ()
{
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
};
using (var b = CreateApkBuilder ()) {
b.ThrowOnBuildFailure = false;
proj.SetProperty ("AndroidHttpClientHandlerType", "invalidvalue");
Assert.IsFalse (b.Build (proj), "Build should have succeeded.");
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "XA1031"), "Output should contain XA1031");
}
}

[Test]
public void CheckHttpClientHandlerType ()
{
Expand Down
14 changes: 14 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,20 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
</ItemGroup>
</Target>

<Target Name="_CheckAndroidHttpClientHandlerType"
Condition=" '$(AndroidHttpClientHandlerType)' != ''">
<PropertyGroup>
<ValidAndroidHttpClientHandlerTypes Condition="'$(UsingAndroidNETSdk)' != 'true'">Xamarin.Android.Net.AndroidClientHandler</ValidAndroidHttpClientHandlerTypes>
<ValidAndroidHttpClientHandlerTypes Condition="'$(UsingAndroidNETSdk)' == 'true'">Xamarin.Android.Net.AndroidMessageHandler</ValidAndroidHttpClientHandlerTypes>
<ValidAndroidHttpClientHandlerTypes Condition="'$(UsingAndroidNETSdk)' == 'true'">$(ValidAndroidHttpClientHandlerTypes);System.Net.Http.SocketsHttpHandler, System.Net.Http</ValidAndroidHttpClientHandlerTypes>
</PropertyGroup>
<AndroidError Code="XA1031"
ResourceName="XA1031"
FormatArguments="$(AndroidHttpClientHandlerType)"
Condition=" !($(ValidAndroidHttpClientHandlerTypes.Contains ('$(AndroidHttpClientHandlerType)')))"
/>
</Target>

<Target Name="_CheckNonIdealConfigurations">
<AndroidWarning Code="XA0119"
ResourceName="XA0119_AOT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ projects. .NET 5 projects will not import this file.
<PropertyGroup Condition=" '$(AndroidApplication)' == 'True' And '$(_AndroidIsBindingProject)' != 'True' ">
<BuildDependsOn>
_ValidateLinkMode;
_CheckAndroidHttpClientHandlerType;
_CheckNonIdealConfigurations;
_SetupMSBuildAllProjects;
_SetupDesignTimeBuildForBuild;
Expand Down

0 comments on commit ed9f7fc

Please sign in to comment.