diff --git a/Documentation/release-notes/4818.md b/Documentation/release-notes/4818.md new file mode 100644 index 00000000000..4d3e27ab651 --- /dev/null +++ b/Documentation/release-notes/4818.md @@ -0,0 +1,5 @@ +#### Application and library build and deployment + + * [GitHub 4818](https://github.com/xamarin/xamarin-android/issues/4818): + Applications using `AndroidAotMode=Hybrid` did not properly strip + away the IL from the resulting .NET assemblies. diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index 9f1a02967be..fc8ec6b8944 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -4331,5 +4331,37 @@ public void XA4310 ([Values ("apk", "aab")] string packageFormat) } } + + [Test] + public void HybridAOT () + { + var proj = new XamarinAndroidApplicationProject () { + IsRelease = true, + AotAssemblies = true, + }; + proj.SetProperty ("AndroidAotMode", "Hybrid"); + // So we can use Mono.Cecil to open assemblies directly + proj.SetProperty ("AndroidEnableAssemblyCompression", "False"); + + using (var b = CreateApkBuilder ()) { + b.Build (proj); + + var apk = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.PackageName}.apk"); + FileAssert.Exists (apk); + using (var zip = ZipHelper.OpenZip (apk)) { + var entry = zip.ReadEntry ($"assemblies/{proj.ProjectName}.dll"); + Assert.IsNotNull (entry, $"{proj.ProjectName}.dll should exist in apk!"); + using (var stream = new MemoryStream ()) { + entry.Extract (stream); + stream.Position = 0; + using (var assembly = AssemblyDefinition.ReadAssembly (stream)) { + var type = assembly.MainModule.GetType ($"{proj.ProjectName}.MainActivity"); + var method = type.Methods.First (m => m.Name == "OnCreate"); + Assert.LessOrEqual (method.Body.Instructions.Count, 1, "OnCreate should have stripped method bodies!"); + } + } + } + } + } } } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs index 8acaa5bf160..0dcb3ee0731 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs @@ -929,11 +929,6 @@ public void BuildIncrementalAot (string supportedAbis, string androidAotMode, bo Assert.IsFalse (b.Output.IsTargetSkipped (target), $"`{target}` should *not* be skipped on first build!"); } - if (androidAotMode == "Hybrid") { - // FIXME: with Hybrid AOT, modifies assemblies in-place - Assert.Ignore ("Ignoring, Hybrid AOT triggers _BuildApkEmbed."); - } - b.BuildLogFile = "second.log"; b.CleanupAfterSuccessfulBuild = false; b.CleanupOnDispose = false; diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index b074d06f4d4..206d5937f30 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -2151,13 +2151,17 @@ because xbuild doesn't support framework reference assemblies. + + <_CilStripAssemblies Include="@(_ShrunkAssemblies)" Condition=" '%(FileName)' != 'Mono.Android' " /> + + + ResolvedAssemblies="@(_CilStripAssemblies)">