Skip to content

Commit

Permalink
[One .NET] "greenfield" projects opt into trimming
Browse files Browse the repository at this point in the history
This is WIP depends on dotnet#8778.

As we have solved all trimming warnings in the Android workload, we
can now go "all in" on trimming.

Early in .NET 6 (maybe even 5?) we "hid" many trimming warnings as we
did not yet plan to solve them:

    <SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>

These warnings were not *actionable* at the time for customers, as
many warnings were in `Mono.Android.dll`, `Java.Interop.dll`, etc.

Going forward, let's stop suppressing these warnings if `TrimMode=full`.

We can also enable trimming for new projects:

* `dotnet new android`
* `dotnet new android-wear`

These will have the `TrimMode` property set to `Full` by default:

    <!--
      Enable full trimming in Release mode.
      To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
    -->
    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
      <TrimMode>full</TrimMode>
    </PropertyGroup>

We wouldn't want to do this for existing projects *yet*, as they might
have existing code, NuGet packages, etc. where trimming warnings might
be present.

We can also improve the templates for Android class libraries:

* `dotnet new androidlib`
* `dotnet new android-bindinglib`

    <!--
      Enable trim analyzers for Android class libraries.
      To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
    -->
    <IsTrimmable>true</IsTrimmable>

This way, new class libraries will be "trimmable" by default and be
able to react to trimming warnings.

We can also use `TrimMode=full` in many of our existing tests:

* MSBuild tests that assert 0 warnings can use `TrimMode=full`.

* On-device tests can import `trim-analyzers.props` and use `TrimMode=full`.
  • Loading branch information
jonathanpeppers committed Mar 12, 2024
1 parent f6139e7 commit 46df03f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!--
Enable trim analyzers for Android class libraries.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
-->
<IsTrimmable>true</IsTrimmable>
<!--
NOTE: you can simply add .aar or .jar files in this directory to be included in the project.
To learn more, see: https://learn.microsoft.com/dotnet/maui/migration/android-binding-projects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);XA4218</MSBuildWarningsAsMessages>
</PropertyGroup>
<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TrimMode>full</TrimMode>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.Wear" Version="1.2.0.5" />
</ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.Android.Templates/android/AndroidApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
</PropertyGroup>
<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TrimMode>full</TrimMode>
</PropertyGroup>
</Project>
5 changes: 5 additions & 0 deletions src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!--
Enable trim analyzers for Android class libraries.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
-->
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">link</TrimMode>
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and '$(TrimMode)' == 'full' ">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
<!-- Prefer $(RuntimeIdentifiers) plural -->
<RuntimeIdentifiers Condition=" '$(RuntimeIdentifier)' == '' And '$(RuntimeIdentifiers)' == '' ">android-arm;android-arm64;android-x86;android-x64</RuntimeIdentifiers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ public void BuildHasNoWarnings (bool isRelease, bool xamarinForms, bool multidex
new XamarinFormsAndroidApplicationProject () :
new XamarinAndroidApplicationProject ();
proj.IsRelease = isRelease;
// Enable full trimming
if (!xamarinForms && isRelease) {
proj.TrimModeRelease = TrimMode.Full;
}
if (multidex) {
proj.SetProperty ("AndroidEnableMultiDex", "True");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public enum AndroidLinkMode
SdkOnly,
Full,
}

public enum TrimMode
{
Partial,
Full,
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Xamarin.ProjectTools
{
Expand All @@ -18,6 +18,7 @@ public static class KnownProperties
public const string RuntimeIdentifiers = "RuntimeIdentifiers";
public const string RunAOTCompilation = "RunAOTCompilation";
public const string PublishTrimmed = "PublishTrimmed";
public const string TrimMode = "TrimMode";
public const string SupportedOSPlatformVersion = "SupportedOSPlatformVersion";

public const string Deterministic = "Deterministic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public AndroidLinkMode AndroidLinkModeRelease {
set { SetProperty (ReleaseProperties, KnownProperties.AndroidLinkMode, value.ToString ()); }
}

public TrimMode TrimModeRelease {
get => Enum.TryParse (GetProperty (ReleaseProperties, KnownProperties.TrimMode), out TrimMode trimMode) ? trimMode : TrimMode.Partial;
set => SetProperty (ReleaseProperties, KnownProperties.TrimMode, value.ToString ().ToLowerInvariant ());
}

public bool EnableMarshalMethods {
get { return string.Equals (GetProperty (KnownProperties.AndroidEnableMarshalMethods), "True", StringComparison.OrdinalIgnoreCase); }
set { SetProperty (KnownProperties.AndroidEnableMarshalMethods, value.ToString ()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
</PropertyGroup>

<Import Project="..\..\..\Configuration.props" />
<Import Project="$(XamarinAndroidSourcePath)\build-tools\trim-analyzers\trim-analyzers.props" />

<PropertyGroup>
<OutputPath>..\..\..\bin\Test$(Configuration)</OutputPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<TrimMode Condition=" '$(TrimMode)' == '' ">full</TrimMode>
</PropertyGroup>

<ItemGroup>
<AndroidJavaSource Include="../Xamarin.Android.McwGen-Tests/java/**/*.java" />
<Compile Remove="TimingTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<Import Project="..\..\..\Configuration.props" />
<Import Project="..\Mono.Android-Test.Shared.projitems" Label="Shared" Condition="Exists('..\Mono.Android-Test.Shared.projitems')" />
<Import Project="$(XamarinAndroidSourcePath)\build-tools\trim-analyzers\trim-analyzers.props" />

<PropertyGroup>
<TargetFramework>$(DotNetAndroidTargetFramework)</TargetFramework>
Expand All @@ -19,7 +20,6 @@
<EnableDefaultAndroidAssetItems>false</EnableDefaultAndroidAssetItems>
<_MonoAndroidTestPackage>Mono.Android.NET_Tests</_MonoAndroidTestPackage>
<PlotDataLabelSuffix>-$(TestsFlavor)NET6</PlotDataLabelSuffix>
<WarningsAsErrors>IL2037</WarningsAsErrors>
<AndroidUseNegotiateAuthentication>true</AndroidUseNegotiateAuthentication>
<AndroidNdkDirectory></AndroidNdkDirectory>
<!--
Expand All @@ -36,6 +36,7 @@

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == '' ">r8</AndroidLinkTool>
<TrimMode Condition=" '$(TrimMode)' == '' ">full</TrimMode>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 46df03f

Please sign in to comment.