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.93k stars 529 forks source link

`LinkAssembliesNoShrink` is expensive #8421

Open Youssef1313 opened 1 year ago

Youssef1313 commented 1 year ago

Android application type

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

Affected platform version

Android 33.0.46

Description

image

This is very very slow.

Steps to Reproduce

Build the app in https://github.com/unoplatform/Uno.Samples/pull/584

Did you find any workaround?

No response

Relevant log output

No response

dellis1972 commented 1 year ago

I tried to see if I could repo but the app does not build using dotnet build XamlBrewerUnoApp.Mobile.csproj -bl

error UXAML0001: The type initializer for 'Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGeneration' threw an exception.
 error CS0103: The name 'InitializeComponent' does not exist in the current context
error CS1061: 'PythagorasTreePage' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'PythagorasTreePage' could be found (are you missing a using directive or an assembly reference?)
 error CS0103: The name 'Root' does not exist in the current context 
Youssef1313 commented 1 year ago

It's strange. It's building perfectly fine in CI. What exception did XamlCodeGeneration throw?

One thing I noticed is that this task might be doing extra work because we are building in Release where AddKeepAlives is true. So I was suspecting this to be what is taking lots of time. I blindly opened a performance optimization but it might still not be where the task is spending most of its time https://github.com/xamarin/xamarin-android/pull/8422.

dellis1972 commented 1 year ago

LinkAssembliesNoShrink should not run in Release, that is a Debug Task. Might be worth checking the properties for AndroidLinkMode in your build log to see why its running that task in Release when it should be running the normal .net linker.

Youssef1313 commented 1 year ago

Ah, AndroidLinkMode is None. That is because we explicitly pass PublishTrimmed to false. I think we did that to actually make the build faster

Youssef1313 commented 1 year ago

Note: Setting AndroidAddKeepAlives to false takes it down from 9 minutes to 3 seconds.

jonathanpeppers commented 9 months ago

Note: Setting AndroidAddKeepAlives to false takes it down from 9 minutes to 3 seconds.

So, are we testing Debug or Release builds? We are generally more interested in improving build times for Debug builds.

The <LinkAssembliesNoShrink/> task normally runs when the linker/trimmer is disabled (Debug mode).

However, $(AndroidKeepAlives) is only true in a Release build by default:

https://github.com/xamarin/xamarin-android/blob/2f192386e8072f8e0ecaf0de2fe48654f3ade423/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets#L944

$(AndroidIncludeDebugSymbols) is based on DebugSymbols=true or DebugType being set.

I was going to look into skipping AddKeepAlives() for any assembly that is newer than MonoAndroid12.0. But I don't see this code path even hit in a dotnet new maui project build.

@Youssef1313 do you have a .binlog of a build where you were seeing this? Was it, perhaps, Release mode?

jonathanpeppers commented 9 months ago

@Youssef1313 your picture above is Release mode:

image

Do you have trimming disabled in this app?

jonpryor commented 9 months ago

@jonathanpeppers asked:

Do you have trimming disabled in this app?

Yes, they do:

Ah, AndroidLinkMode is None. That is because we explicitly pass PublishTrimmed to false. I think we did that to actually make the build faster

Youssef1313 commented 9 months ago

@jonpryor Yes, it's the release build in CI.

jonpryor commented 2 months ago

We should look into further optimizing AddKeepAlivesStep.cs/etc. to only process assemblies built against Xamarin.Android 11.1 or older, as those versions lack #5278.