dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.94k stars 533 forks source link

Error MSB4096 while compiling app in release mode with "TrimMode" set to "partial" #8775

Closed Iron2213 closed 9 months ago

Iron2213 commented 9 months ago

Android application type

.NET Android (net7.0-android, net8.0-android, etc.)

Affected platform version

.NET 8, VS2022 17.8.7

Description

I'm trying to upgrade my Android APP to .NET 8 from .NET 7. I can successfully compile and run the application in debug mode without any problem, but when i try to build in release mode i get the following error:

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.52\targets\Microsoft.Android.Sdk.ILLink.targets(106,5): error MSB4096: The item "System.Private.CoreLib" in item list "TrimmerRootAssembly" does not define a value for metadata "RootMode". In order to use this metadata, either qualify it by specifying %(TrimmerRootAssembly.RootMode), or ensure that all items in this list define a value for this metadata.

The error is repeated 4 times in the output section.

I have no Nuget packages to update (not counting preview updates).

This is my current .csproj file:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0-android</TargetFramework>
        <SupportedOSPlatformVersion>24</SupportedOSPlatformVersion>
        <OutputType>Exe</OutputType>
        <Nullable>disable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseMauiEssentials>true</UseMauiEssentials>
        <ApplicationId>[package name]</ApplicationId>
        <ApplicationVersion>1</ApplicationVersion>
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <Debugger>Xamarin</Debugger>
        <TrimMode>link</TrimMode>
        <AndroidKeyStore>False</AndroidKeyStore>
        <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
        <PublishTrimmed>False</PublishTrimmed>
        <AndroidEnableMultiDex>False</AndroidEnableMultiDex>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
        <Debugger>Xamarin</Debugger>
        <TrimMode>partial</TrimMode>
        <AndroidKeyStore>False</AndroidKeyStore>
        <RunAOTCompilation>False</RunAOTCompilation>
        <AndroidPackageFormat>apk</AndroidPackageFormat>
        <AndroidUseAapt2>True</AndroidUseAapt2>
        <AndroidCreatePackagePerAbi>False</AndroidCreatePackagePerAbi>
        <PublishTrimmed>True</PublishTrimmed>
        <AndroidEnableMultiDex>False</AndroidEnableMultiDex>
    </PropertyGroup>
    <ItemGroup>
        <AndroidResource Include="Resources\*\*.xml" />
    </ItemGroup>
    <ItemGroup>
        <GoogleServicesJson Include="google-services.json" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.2" />
        <PackageReference Include="Mzsoft.BCrypt" Version="1.0.0" />
        <PackageReference Include="NetStandard.AppCore" Version="1.7.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="QRCoder" Version="1.4.3" />
        <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
        <PackageReference Include="Syncfusion.Licensing" Version="22.1.36" />
        <PackageReference Include="Syncfusion.Xamarin.DocIO" Version="22.1.36" />
        <PackageReference Include="Syncfusion.Xamarin.Pdf" Version="22.1.36" />
        <PackageReference Include="XAB.FlexboxLayout" Version="3.0.0.2" />
        <PackageReference Include="Xamarin.AndroidX.Annotation" Version="1.7.1.1" />
        <PackageReference Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.0.1.6" />
        <PackageReference Include="Xamarin.AndroidX.Lifecycle.Common" Version="2.7.0.1" />
        <PackageReference Include="Xamarin.AndroidX.SwipeRefreshLayout" Version="1.1.0.18" />
        <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.9.0.1" />
        <PackageReference Include="Xamarin.FFImageLoading" Version="2.4.11.982" />
        <PackageReference Include="Xamarin.Firebase.Messaging" Version="123.1.2.1" />
        <PackageReference Include="Xamarin.Google.Android.Material" Version="1.10.0.3" />
        <PackageReference Include="Xamarin.GooglePlayServices.Code.Scanner" Version="116.1.0.2" />
        <PackageReference Include="Xamarin.GooglePlayServices.Location" Version="120.0.0.2" />
        <PackageReference Include="Xamarin.GooglePlayServices.Maps" Version="118.1.0.2" />
        <PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.7.0.1" />
        <PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.7" />

        <!-- This fixes: https://github.com/xamarin/GooglePlayServicesComponents/issues/775#issuecomment-1572512630 -->
        <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" />

        <!-- This fixes: https://github.com/xamarin/AndroidX/issues/800#issuecomment-1774639627 -->
        <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.1" />
        <PackageReference Include="Xamarin.AndroidX.Collection.Jvm" Version="1.4.0.1" />
        <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
    </ItemGroup>
    <ItemGroup>
        <ProjectReference Include="..\MedicalNoteSharedModels\MedicalNoteSharedModels.csproj" />
        <ProjectReference Include="..\StrykerHubLib\StrykerHubLib.csproj" />
    </ItemGroup>
    <ItemGroup>
        <TrimmerRootAssembly Include="System.Private.CoreLib" />
    </ItemGroup>
</Project>

Steps to Reproduce

Unable to reproduce with new .NET 8 application.

Did you find any workaround?

Setting "PublishTrimmed" to false resolves the issue but creates a much bigger APK when archiving.

Relevant log output

[ServiceRelease.binlog.txt](https://github.com/xamarin/xamarin-android/files/14448775/ServiceRelease.binlog.txt)
dellis1972 commented 9 months ago

Looks like you need to add the RootMode attribute/metadata to the TrimmerRootAssembly element. Here is a link to the list of valid values https://github.com/dotnet/runtime/blob/89f7ad3b276fb0b48f20cb4e8408bdce85c2b415/src/tools/illink/src/linker/Linker/AssemblyRootMode.cs#L6

Or do like the message says and try.

<TrimmerRootAssembly Include="System.Private.CoreLib" RootMode="%(TrimmerRootAssembly.RootMode)"/>
Iron2213 commented 9 months ago

Adding did <TrimmerRootAssembly Include="System.Private.CoreLib" RootMode="%(TrimmerRootAssembly.RootMode)"/> resolve the issue, thank you!