Skip to content

Conversation

@dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Jan 22, 2025

So one of the big issues we have with design time builds is the shear amount of time they take.
This is principally because we have to discover and extract ALL the .aar/.zip files which we need before we can do a build.
This used to be because we embedded the data into the assemblies, but we ship these resources as .aar file these days.
So lets take advantage of that.

Rather then extracting to disk and then reading the files from disk, we instead read the .aar directly for a design time build. We extract the required data into memory which will allow us to generate the required R.txt file. Note that only a small amount of data is needed from the .aar , its mostly entry filenames and the contents of any .xml/.axml file which we find in the res folder. So this will cut down the amount of data we need.

We only use this for design time builds. The typical build is left as is , and if the on disk cache is available the design time build will use that instead of the .aars.

One of the things that was missing from our design time build tests was the SkipCompilerExecution MSbuild parameter. This is one of the important parameters which VS uses, it stops the Csc Task from creating an assembly on disk. This was the main speed issue we had in our unit tests, and adding this flag cut a second of our design time build times.

Average Time
Before 2026.2504ms
This Commit 592.1775ms

@dellis1972
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@dellis1972 dellis1972 force-pushed the dev/dellis1972/dtbrework branch from c71d8d9 to 9f9f973 Compare January 22, 2025 09:19
@dellis1972 dellis1972 changed the title Dev/dellis1972/dtbrework Rework Design Time Build to use .aar files directly. Jan 22, 2025
@dellis1972 dellis1972 marked this pull request as ready for review January 22, 2025 13:26
@dellis1972
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me as-is. 👍

<ItemGroup>
<!-- Only use the aar files if we have not extracted the data -->
<_DesignTimeAarFiles Include="@(AndroidAarLibrary)" Condition=" '@(LibraryResourceDirectories->Count())' == '0' " />
<_DesignTimeAarFiles Include="@(LibraryProjectZip)" Condition="'%(LibraryProjectZip.Extension)' == '.aar' and '@(LibraryResourceDirectories->Count())' == '0'" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not worth changing, but I wonder if it's better if this expression was first:

'@(LibraryResourceDirectories->Count())' == '0'

So, it would prevent MSBuild from batching/expanding the first part:

'%(LibraryProjectZip.Extension)' == '.aar'

It probably runs the ->Count() N times, but probably not slow enough to matter, though.

Assert.IsFalse (libProj.CreateBuildOutput (libBuilder).IsTargetSkipped ("_ManagedUpdateAndroidResgen"),
"Target '_ManagedUpdateAndroidResgen' should have run.");
Assert.IsFalse (appBuilder.DesignTimeBuild (appProj), "Application project should have built");
Assert.IsTrue (appBuilder.DesignTimeBuild (appProj), "Application project should have built");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change confuses me; before this change, the design-time build would fail; why? Are the reasons for that failure important? Or is it better to not have failures during a design-time build, as "real" errors will "surface" during a real build?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So before the DTB would fail because the library project .dll would not be found (cos it hand not been built yet).
In the new system we no longer need the project reference to be built we can still get the data we need as long as it has been restored because we read the .aar file directly.

As a result this now builds ok.

@jonpryor
Copy link
Contributor

Context: https://developer.android.com/studio/projects/android-library#aar-contents

One of the big issues we have with design time builds is the shear
amount of time they take.  This is principally because we have to
discover and extract *all* the `.aar`/`.zip` files which we need
before we can do a build.

This used to be because we embedded the data into the assemblies, but
we ship these resources as `.aar` file these days.
Let's take advantage of that.

Rather then extracting to disk and then reading the files from disk,
we instead read the `.aar` directly for a design time build.
We extract the required data into memory which will allow us to
generate the required `R.txt` file.  Note that only a small amount of
data is needed from the `.aar`, its mostly entry filenames and the
contents of any `.xml`/`.axml` files which we find in the `res` folder.
This will cut down the amount of data we need.

We only use this for design time builds.

The typical build is left as-is, and if the on disk cache is available
the design time build will use that instead of the `.aar`s.

One of the things that was missing from our design time build tests
was the `SkipCompilerExecution` MSBuild parameter.  This is one of
the important parameters which VS uses, as it stops the `<Csc/>` Task
from creating an assembly on disk.  This was the main speed issue we
had in our unit tests, and adding this flag cut a second off of our
design time build times. 


|               |  Average Time |
| ------------- | ------------: |
|      ef9912af |   2026.2504ms |
|   This Commit |    592.1775ms |

@jonpryor jonpryor merged commit 9ed1469 into main Jan 23, 2025
31 of 58 checks passed
@jonpryor jonpryor deleted the dev/dellis1972/dtbrework branch January 23, 2025 17:08
grendello added a commit that referenced this pull request Jan 23, 2025
* main:
  [Xamarin.Android.Build.Tasks] Rework DTB to use .aar files directly (#9700)
@github-actions github-actions bot locked and limited conversation to collaborators Feb 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants