Skip to content

Commit 9b060fa

Browse files
[Xamarin.Android.Build.Tasks] copy all assembles to android/assets/shrunk
In the case where `$(AndroidLinkMode)` is not `None`, we run a `<RemoveRegisterAttribute/>` to remove all `[Register]` attributes from `Mono.Android.dll`. This is a step to save on APK size. However, we found that the AOT compiler is seeing *two* `Mono.Android.dll` files: * obj\Debug\android\assets\Mono.Android.dll * obj\Debug\android\assets\shrunk\Mono.Android.dll @vargaz pointed out that this is bad, some of the AOT images will be linked against the wrong one and fail to load at runtime. We could possibly be silently falling back to the JIT... I first attempted to just remove the `shrunk` directory and write `Mono.Android.dll` *in place*. This idea didn't pan out: dotnet#3925 (comment) That idea broke incremental builds... So the next idea is to just copy *every assembly* into the `android\assets\shrunk` directory. Then the `AOT` compiler only operates against the `shrunk` directory. This should only happen during `Release` builds, so the build performance hit should be OK. We can also investigate removing `[Register]` from *all* assemblies in a future PR, as that will likely save further APK size from the support libraries, Google Play Services, etc.
1 parent 6dbf4d4 commit 9b060fa

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,30 @@ because xbuild doesn't support framework reference assemblies.
20412041
<Output TaskParameter="Include" ItemName="_ResolvedFrameworkAssemblies" />
20422042
</CreateItem>
20432043

2044+
<CreateItem
2045+
Include="@(_ResolvedAssemblies)"
2046+
Condition="'$(AndroidLinkMode)' == 'None' OR '$(AndroidUseSharedRuntime)' == 'true'">
2047+
<Output TaskParameter="Include" ItemName="_ShrunkAssemblies" />
2048+
</CreateItem>
2049+
2050+
<CreateItem
2051+
Include="@(_ResolvedAssemblies->'$(MonoAndroidIntermediateAssemblyDir)shrunk\%(Filename)%(Extension)')"
2052+
Condition="'$(AndroidLinkMode)' != 'None' AND '$(AndroidUseSharedRuntime)' != 'true'">
2053+
<Output TaskParameter="Include" ItemName="_ShrunkAssemblies" />
2054+
</CreateItem>
2055+
2056+
<CreateItem
2057+
Include="@(_ResolvedUserAssemblies)"
2058+
Condition="'$(AndroidLinkMode)' == 'None' OR '$(AndroidUseSharedRuntime)' == 'true'">
2059+
<Output TaskParameter="Include" ItemName="_ShrunkUserAssemblies" />
2060+
</CreateItem>
2061+
2062+
<CreateItem
2063+
Include="@(_ResolvedUserAssemblies->'$(MonoAndroidIntermediateAssemblyDir)shrunk\%(Filename)%(Extension)')"
2064+
Condition="'$(AndroidLinkMode)' != 'None' AND '$(AndroidUseSharedRuntime)' != 'true'">
2065+
<Output TaskParameter="Include" ItemName="_ShrunkUserAssemblies" />
2066+
</CreateItem>
2067+
20442068
<CreateItem
20452069
Include="@(_ResolvedFrameworkAssemblies)"
20462070
Condition="'$(AndroidLinkMode)' == 'None' OR '$(AndroidUseSharedRuntime)' == 'true'">
@@ -2520,20 +2544,19 @@ because xbuild doesn't support framework reference assemblies.
25202544
Condition="'$(AndroidLinkMode)' != 'None' AND '$(AndroidUseSharedRuntime)' != 'true'">
25212545

25222546
<!-- Make a copy of every assembly into assets\shrunk -->
2523-
<Copy
2524-
SourceFiles="@(_ResolvedFrameworkAssemblies)"
2525-
DestinationFiles="@(_ShrunkFrameworkAssemblies)"
2526-
SkipUnchangedFiles="true" />
2547+
<CopyIfChanged
2548+
SourceFiles="@(_ResolvedAssemblies)"
2549+
DestinationFiles="@(_ShrunkAssemblies)" />
25272550

25282551
<CopyIfChanged
2529-
SourceFiles="@(_ResolvedFrameworkAssemblies->'%(Identity).config')"
2530-
DestinationFiles="@(_ShrunkFrameworkAssemblies->'%(Identity).config')" />
2552+
SourceFiles="@(_ResolvedAssemblies->'%(Identity).config')"
2553+
DestinationFiles="@(_ShrunkAssemblies->'%(Identity).config')" />
25312554

25322555
<!-- Shrink Mono.Android.dll by removing attribute only needed for GenerateJavaStubs -->
25332556
<RemoveRegisterAttribute
25342557
Condition="'$(AndroidLinkMode)' != 'None' AND '$(AndroidUseSharedRuntime)' != 'true'"
25352558
Deterministic="$(Deterministic)"
2536-
ShrunkFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)" />
2559+
ShrunkFrameworkAssemblies="@(_ShrunkAssemblies)" />
25372560

25382561
<MakeDir Directories="$(MonoAndroidIntermediateAssemblyDir)shrunk" />
25392562
<Touch Files="$(_RemoveRegisterFlag)" AlwaysCreate="true" />
@@ -2678,8 +2701,7 @@ because xbuild doesn't support framework reference assemblies.
26782701
<_BuildApkEmbedInputs>
26792702
$(MSBuildAllProjects)
26802703
;$(_PackagedResources)
2681-
;@(_ResolvedUserAssemblies)
2682-
;@(_ShrunkFrameworkAssemblies)
2704+
;@(_ShrunkAssemblies)
26832705
;@(AndroidNativeLibrary)
26842706
;@(_DexFile)
26852707
;$(_AndroidBuildPropertiesCache)
@@ -2717,7 +2739,7 @@ because xbuild doesn't support framework reference assemblies.
27172739
AndroidSequencePointsMode="$(_SequencePointsMode)"
27182740
AotAdditionalArguments="$(AndroidAotAdditionalArguments)"
27192741
ExtraAotOptions="$(AndroidExtraAotOptions)"
2720-
ResolvedAssemblies="@(_ResolvedUserAssemblies);@(_ShrunkFrameworkAssemblies)"
2742+
ResolvedAssemblies="@(_ShrunkAssemblies)"
27212743
AotOutputDirectory="$(_AndroidAotBinDirectory)"
27222744
IntermediateAssemblyDir="$(MonoAndroidIntermediateAssemblyDir)"
27232745
LinkMode="$(AndroidLinkMode)"
@@ -2742,7 +2764,7 @@ because xbuild doesn't support framework reference assemblies.
27422764
Condition="'$(BundleAssemblies)' == 'True'"
27432765
KeepTemp="$(AndroidMakeBundleKeepTemporaryFiles)"
27442766
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
2745-
Assemblies="@(_ResolvedUserAssemblies);@(_AndroidResolvedSatellitePaths);@(_ShrunkFrameworkAssemblies)"
2767+
Assemblies="@(_ShrunkAssemblies);@(_AndroidResolvedSatellitePaths)"
27462768
IncludePath="$(MonoAndroidIncludeDirectory)"
27472769
SupportedAbis="@(_BuildTargetAbis)"
27482770
TempOutputPath="$(IntermediateOutputPath)"
@@ -2759,7 +2781,7 @@ because xbuild doesn't support framework reference assemblies.
27592781
BundleAssemblies="$(BundleAssemblies)"
27602782
BundleNativeLibraries="$(_BundleResultNativeLibraries)"
27612783
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
2762-
ResolvedUserAssemblies="@(_ResolvedUserAssemblies);@(_AndroidResolvedSatellitePaths)"
2784+
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
27632785
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
27642786
NativeLibraries="@(AndroidNativeLibrary)"
27652787
ApplicationSharedLibraries="@(_ApplicationSharedLibrary)"
@@ -2788,7 +2810,7 @@ because xbuild doesn't support framework reference assemblies.
27882810
BundleAssemblies="$(BundleAssemblies)"
27892811
BundleNativeLibraries="$(_BundleResultNativeLibraries)"
27902812
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
2791-
ResolvedUserAssemblies="@(_ResolvedUserAssemblies);@(_AndroidResolvedSatellitePaths)"
2813+
ResolvedUserAssemblies="@(_ShrunkUserAssemblies);@(_AndroidResolvedSatellitePaths)"
27922814
ResolvedFrameworkAssemblies="@(_ShrunkFrameworkAssemblies)"
27932815
NativeLibraries="@(AndroidNativeLibrary)"
27942816
ApplicationSharedLibraries="@(_ApplicationSharedLibrary)"

0 commit comments

Comments
 (0)