forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] Add $(AndroidLinkResources) (dotnet#5317)
The generated `Resource` class in `Resource.designer.cs` can be quite large. When doing "Release" builds, update the linker to *remove* the `Resource` class and its nested types. We do this by examining all the locations where the fields are used and replacing those IL calls with a call which specifies the actual resource id value directly. This will remove the need to call `global::Android.Runtime.ResourceIdManager.UpdateIdValues();` from the static constructors in the `Resource` class and its nested types, which will improve app launch times (as `UpdateIdValues()` involves Reflection, which is not "fast"). Due to an odd linker failure we cannot completely remove the `Resource` class itself. It *will* be completely empty. "Debug" builds will *not* remove the `Resource` type, and will retain the call to `UpdateIdValues()`, in order to ensure that app rebuild and redeploy times are as fast as possible. The new `$(AndroidLinkResources)` MSBuild property has been added to control this feature. It is disabled by default for now. `$(AndroidLinkResources)`=True appears to save between 100-280kb for an `.apk`. This is dependent on how many assemblies are Android assemblies and contain or use Resources. Xamarin.Forms base app `apkdiff` results Size difference in bytes ([*1] apk1 only, [*2] apk2 only): - 10 assemblies/Mono.Android.dll - 4,001 assemblies/Xamarin.Essentials.dll - 45,426 assemblies/Xamarin.Forms.Platform.Android.dll - 102,839 assemblies/formstest.Android.dll Summary: + 0 Other entries 0.00% (of 887,808) + 0 Dalvik executables 0.00% (of 3,341,152) - 152,276 Assemblies -3.23% (of 4,709,077) + 0 Shared libraries 0.00% (of 41,693,796) - 151,552 Package size difference -0.73% (of 20,820,695) Maps sample `apkdiff` results Size difference in bytes ([*1] apk1 only, [*2] apk2 only): - 4 lib/armeabi-v7a/libxamarin-app.so - 14 assemblies/Mono.Android.dll - 4,757 assemblies/Xamarin.Essentials.dll - 46,051 assemblies/Xamarin.Forms.Platform.dll - 47,586 assemblies/Xamarin.Forms.Platform.Android.dll - 48,470 assemblies/Xamarin.Forms.Maps.Android.dll - 135,589 assemblies/FormsMapsSample.Android.dll Summary: + 0 Other entries 0.00% (of 1,137,209) + 0 Dalvik executables 0.00% (of 2,519,972) - 282,467 Assemblies -5.51% (of 5,123,833) - 4 Shared libraries -0.00% (of 41,843,596) - 282,624 Package size difference -1.34% (of 21,075,664)
- Loading branch information
1 parent
905878b
commit 9e6ce03
Showing
14 changed files
with
377 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AndroidLinkConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Mono.Cecil; | ||
using Mono.Linker; | ||
using Mono.Linker.Steps; | ||
using System; | ||
using System.Linq; | ||
using Xamarin.Android.Tasks; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace MonoDroid.Tuner | ||
{ | ||
public class AndroidLinkConfiguration { | ||
public List<AssemblyDefinition> Assemblies { get; private set; } = new List<AssemblyDefinition> (); | ||
|
||
static ConditionalWeakTable<LinkContext, AndroidLinkConfiguration> configurations = new ConditionalWeakTable<LinkContext, AndroidLinkConfiguration> (); | ||
|
||
public static AndroidLinkConfiguration GetInstance (LinkContext context) | ||
{ | ||
if (!configurations.TryGetValue (context, out AndroidLinkConfiguration config)) { | ||
config = new AndroidLinkConfiguration (); | ||
configurations.Add (context, config); | ||
} | ||
return config; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GetAssembliesStep.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Mono.Cecil; | ||
using Mono.Linker; | ||
using Mono.Linker.Steps; | ||
using System; | ||
using System.Linq; | ||
using Xamarin.Android.Tasks; | ||
using System.Collections.Generic; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace MonoDroid.Tuner | ||
{ | ||
public class GetAssembliesStep : BaseStep | ||
{ | ||
AndroidLinkConfiguration config = null; | ||
|
||
protected override void Process () | ||
{ | ||
config = AndroidLinkConfiguration.GetInstance (Context); | ||
} | ||
|
||
protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
{ | ||
if (config == null) | ||
return; | ||
config.Assemblies.Add (assembly); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.