Skip to content

Commit

Permalink
Improved readability "optimize-android-builds-and-start-up-times" (#8612
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tiagov8 authored May 29, 2024
1 parent 43724f1 commit ee250a5
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions rules/optimize-android-builds-and-start-up-times/rule.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
type: rule
archivedreason:
title: Do you optimise your Android builds and start-up times?
title: Do you optimize your Android builds and start-up times?
guid: fa142c74-97bb-4150-be0d-abb997ea9d30
uri: optimize-android-builds-and-start-up-times
created: 2020-10-08T23:04:23.0000000Z
Expand All @@ -15,10 +15,10 @@ authors:
related: []
redirects:
- do-you-optimise-your-android-builds-and-start-up-times

---

.NET MAUI provides several ways to optimize an Android application. Some of them complement each other while others can be mutually exclusive.
It's important to understand what options you have at hands and how they affect your application.
.NET MAUI provides several ways to optimize an Android application. Some of them complement each other while others can be mutually exclusive. It's important to understand what options you have at hands and how they affect your application.

<!--endintro-->

Expand All @@ -30,49 +30,51 @@ When it comes to app optimizations, developers usually try to strike the right b

Quite often improvements in one area lead to degradation in another. In most cases it's a tradeoff which developers need to take depending on their circumstances. Different Debug and Release configurations partially address this problem, but may lead to configuration-specific bugs.

.NET MAUI Android combines two worlds - native Android and .NET, which means that optimization can be performed in both worlds.
.NET MAUI Android combines 2 worlds - native Android and .NET, which means that optimization can be performed in both worlds.

## Native Layer optimizations
### Native Layer optimizations

These optimizations are not specific to .NET MAUI and are used in other cross-platform frameworks or native Android development.

### Use AAB (Android App Bundle or just App Bundle)
#### Use AAB (Android App Bundle or just App Bundle)

❌ Debug
❌ Debug
✅ Release

.aab files are used only for publishing on Google Play and cannot be installed on Android devices. AAB allows smaller builds to be targeted to individual hardware specifications by packaging only required resources (e.g. icons, images). It also deligates app signing to Google Play.

App Bundle [is mandatory for Google Play since 2021](https://developer.android.com/guide/app-bundle).

### Use R8 (instead of ProGuard)
#### Use R8 (instead of ProGuard)

❌ Debug
✅ Release

R8 is a code shrinker and obfuscator which processes **only** native java/kotlin code. It doesn't touch your .NET code.

As R8 increases build time it's only recommented for Release configuration.

### Use AAPT2 (instead of AAPT)
#### Use AAPT2 (instead of AAPT)

✅ Debug
✅ Debug
✅ Release

AAPT2 parses, indexes, and compiles the resources into a binary format that is optimized for the Android platform.

## .NET Layer optimizations
### .NET Layer optimizations

These optimizations are .NET MAUI specific.

### Concurrent GC
#### Concurrent GC

✅ Debug
✅ Debug
✅ Release

Concurrent GC improves app performance by collecting garbage alongside the running program. This approach prevents program from freeze which happen for non-concurrent GCs.

### AOT
#### AOT

❌ Debug
❌ Debug
✅ Release

Whereas iOS enforces AOT (ahead of time) compilation, Android supports (and uses by default) JIT (just in time) compilation which happens at runtime, but AOT can be enabled on Android to improve performance. As the code will be pre-compiled, it comes at a cost of significantly larger app size and longer build times.
Expand All @@ -88,37 +90,32 @@ Instead of compiling as much of the app as possible to unmanaged code, Profiled

AOT increases app size and build time (especially Full AOT). Recommended for Release.

### LLVM
#### LLVM

❌ Debug
❌ Debug
✅ Release

Mono support two compilation engines, a fast, JIT-friendly compilation engine which does not generate very fast code, and a slower compilation engine based on the LLVM optimizing compiler that produces superior code.
When set LLVM will be used when Ahead-of-Time compiling assemblies into native code.
Mono support 2 compilation engines, a fast, JIT-friendly compilation engine which does not generate very fast code, and a slower compilation engine based on the LLVM optimizing compiler that produces superior code.

If Full AOT is not enabled, this property is **ignored**.
When set LLVM will be used when Ahead-of-Time compiling assemblies into native code. If Full AOT is not enabled, this property is **ignored**.

Increases build time. Recommended for Release.

### IL Stripping
#### IL Stripping

❌ Debug
❌ Debug
✅ Release

$(AndroidStripILAfterAOT) removes IL code after performing AOT as it is no longer required.

---

### More information

* [Optimize Xamarin Android Builds](https://devblogs.microsoft.com/xamarin/optimize-xamarin-android-builds)

* [IL Stripping](https://devblogs.microsoft.com/dotnet/dotnet-8-performance-improvements-in-dotnet-maui/#androidstripilafteraot)

* [AOT and LLVM](https://devblogs.microsoft.com/dotnet/performance-improvements-in-dotnet-maui/#aot-and-llvm#aot-everything)

* [Profiled AOT](https://devblogs.microsoft.com/xamarin/faster-startup-times-with-startup-tracing-on-android)

* [Profiled ATO Release Notes](https://learn.microsoft.com/en-us/xamarin/android/release-notes/9/9.4#option-to-compile-app-startup-methods-to-unmanaged-code)

* [Mono AOT doc](https://www.mono-project.com/docs/advanced/aot)

* [Mono LLVM doc](https://www.mono-project.com/docs/advanced/mono-llvm)

0 comments on commit ee250a5

Please sign in to comment.