How to exclude some libraries to be trimmed in .NET 8 for Android, not MAUI?

Federico Navarrete 616 Reputation points
2024-05-21T14:10:40.6733333+00:00

I've been migrating my app from Xamarin.Android to .NET 8 and I'm getting some issues since I don't have an option to exclude some libs that existed in Xamarin:

Screenshot 2024-05-21 155329

But in .NET 8 for Android in Visual Studio 2022, there is nothing like that:

Screenshot 2024-05-21 155512

I tried to add some code like this one in the .csproj:


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

  <EnableLLVM>True</EnableLLVM>

  <JavaMaximumHeapSize>2G</JavaMaximumHeapSize>

  <AndroidKeyStore>False</AndroidKeyStore>

  <TrimMode>link</TrimMode>

  <PublishTrimmed>True</PublishTrimmed>

  <RunAOTCompilation>True</RunAOTCompilation>

  <AndroidLinkTool>r8</AndroidLinkTool>

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

  <EnableLLVM>True</EnableLLVM>

  <JavaMaximumHeapSize>2G</JavaMaximumHeapSize>

  <AndroidKeyStore>False</AndroidKeyStore>

  <TrimMode>link</TrimMode>

  <RunAOTCompilation>True</RunAOTCompilation>

</PropertyGroup>

<ItemGroup>

  <TrimmerRootAssembly Include="Xamarin-MaterialSearchBar" RootMode="CopyUsed" />

</ItemGroup>

<ItemGroup>

  <TrimmerRootAssembly Include="Xamarin-MaterialSearchBar" RootMode="CopyUsed" />

</ItemGroup>

Or like this:


<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

  <EnableLLVM>True</EnableLLVM>

  <JavaMaximumHeapSize>2G</JavaMaximumHeapSize>

  <AndroidKeyStore>False</AndroidKeyStore>

  <TrimMode>link</TrimMode>

  <PublishTrimmed>True</PublishTrimmed>

  <RunAOTCompilation>True</RunAOTCompilation>

  <AndroidLinkTool>r8</AndroidLinkTool>

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

  <EnableLLVM>True</EnableLLVM>

  <JavaMaximumHeapSize>2G</JavaMaximumHeapSize>

  <AndroidKeyStore>False</AndroidKeyStore>

  <TrimMode>link</TrimMode>

  <RunAOTCompilation>True</RunAOTCompilation>

</PropertyGroup>

<ItemGroup>

  <TrimmerRootAssembly Include="Xamarin-MaterialSearchBar" />

</ItemGroup>

And I got error like these ones:

Severity Code Description Project File Line Suppression State

Error MSB4096 The item "Xamarin-MaterialSearchBar" in item list "TrimmerRootAssembly" does not define a value for metadata "RootMode"

Severity Code Description Project File Line Suppression State

Error IL1037 Invalid assembly root mode 'CopyUsed'.

Severity Code Description Project File Line Suppression State

Error NETSDK1144 Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to false.

Without the trimming, my app increases almost twice its size. Any idea how can I exclude this library?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,530 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,064 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,492 questions
{count} votes