Commit b521911
committed
[One .NET] "greenfield" projects opt into trimming
As we have solved all trimming warnings in the Android workload, we
can now go "all in" on trimming.
Early in .NET 6 (maybe even 5?) we "hid" many trimming warnings as we
did not yet plan to solve them:
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
These warnings were not *actionable* at the time for customers, as
many warnings were in `Mono.Android.dll`, `Java.Interop.dll`, etc.
Going forward, let's stop suppressing these warnings if `TrimMode=full`.
We can also enable trimming for new projects:
* `dotnet new android`
* `dotnet new android-wear`
These will have the `TrimMode` property set to `Full` by default:
<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TrimMode>full</TrimMode>
</PropertyGroup>
We wouldn't want to do this for existing projects *yet*, as they might
have existing code, NuGet packages, etc. where trimming warnings might
be present.
We can also improve the templates for Android class libraries:
* `dotnet new androidlib`
* `dotnet new android-bindinglib`
<!--
Enable trim analyzers for Android class libraries.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
-->
<IsTrimmable>true</IsTrimmable>
This way, new class libraries will be "trimmable" by default and be
able to react to trimming warnings.
We can also use `TrimMode=full` in many of our existing tests:
* MSBuild tests that assert 0 warnings can use `TrimMode=full`.
* On-device tests can use `TrimMode=full`.
~~ General trimming warnings ~~
This was discovered through `Mono.Android-NET-Tests.csproj`, but there
were a few trimmer warnings in the "layout bindings" feature:
dotnet\packs\Microsoft.Android.Sdk.Windows\...\tools\LayoutBinding.cs(79,56): warning IL2091: Xamarin.Android.Design.LayoutBinding.<>c__DisplayClass8_0<T>.<FindFragment>b__0(Activity): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Android.App.FragmentManager.FindFragmentById<T>(Int32)'. The generic parameter 'T' of 'Xamarin.Android.Design.LayoutBinding.<>c__DisplayClass8_0<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
dotnet\packs\Microsoft.Android.Sdk.Windows\...\tools\LayoutBinding.cs(35,5): warning IL2091: Xamarin.Android.Design.LayoutBinding.FindView<T>(Int32, T&): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Android.App.Activity.FindViewById<T>(Int32)'. The generic parameter 'T' of 'Xamarin.Android.Design.LayoutBinding.FindView<T>(Int32, T&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
dotnet\packs\Microsoft.Android.Sdk.Windows\...\tools\LayoutBinding.cs(37,5): warning IL2091: Xamarin.Android.Design.LayoutBinding.FindView<T>(Int32, T&): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Android.Views.View.FindViewById<T>(Int32)'. The generic parameter 'T' of 'Xamarin.Android.Design.LayoutBinding.FindView<T>(Int32, T&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
We can `[DynamicallyAccessedMembers (Constructors)]` to fix these.
~~ Trimming warnings in tests ~~
Several tests that verify "trimming unsafe features" just specify:
[RequiresUnreferencedCode ("Tests trimming unsafe features")]
If the test might have an issue under NativeAOT, I used:
// FIXME: #8724
#pragma warning disable IL3050
Places that use `Assembly.GetType()` can use `Type.GetType()` instead:
--var JavaProxyThrowable_type = typeof (Java.Lang.Object)
-- .Assembly
-- .GetType ("Android.Runtime.JavaProxyThrowable");
++var JavaProxyThrowable_type = Type.GetType ("Android.Runtime.JavaProxyThrowable, Mono.Android");
`AppDomainTest` was just ignored (and had warnings). Updated to just
verify `PlatformNotSupportedException` is thrown.
~~ Test failures ~~
`JsonSerializerTest` requires a feature flag:
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
Otherwise, an exception is thrown:
System.InvalidOperationException : JsonSerializerIsReflectionDisabled
`Java.Interop-Tests` were initially not loaded at all. With the log
message:
03-13 16:13:59.940 5262 5344 W NUnit : Failed to load tests from assembly 'Java.Interop-Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065
If we make `Java.Interop-Tests.dll` a `@(TrimmerRootAssembly)`:
<TrimmerRootAssembly Include="Java.Interop-Tests" RootMode="All" />
Then all the tests cases are preserved and can be run, in the same way
the "main app assembly" is preserved.
`NinePatchTests` failed with:
The drawable created from resource tile should be a NinePatchDrawable.
Expected: not null
But was: null
The only usage of `NinePatchDrawable` was:
Assert.IsNotNull (d as NinePatchDrawable);
`NinePatchDrawable` likely needs its interfaces and constructors
preserved for this test to pass. I added an attribute for just `All`
for the test to pass:
[DynamicDependency (DynamicallyAccessedMemberTypes.All, typeof (NinePatchDrawable))]
`CustomWidgetTests` failed with:
(Binary XML file line #1 in Mono.Android.NET_Tests:layout/uppercase_custom: Binary XML file line #1 in Mono.Android.NET_Tests:layout/uppercase_custom: Error inflating class Mono.Android_Test.Library.CustomTextView)
at Java.Interop.JniEnvironment.InstanceMethods.CallObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(String , IJavaPeerable , JniArgumentValue* )
at Android.Views.LayoutInflater.Inflate(Int32 , ViewGroup )
at Xamarin.Android.RuntimeTests.CustomWidgetTests.<>c.<UpperCaseCustomWidget_ShouldNotThrowInflateException>b__0_0()
at NUnit.Framework.Constraints.VoidInvocationDescriptor.Invoke()
at NUnit.Framework.Constraints.ExceptionInterceptor.Intercept(Object )
--- End of managed Java.Lang.RuntimeException stack trace ---
android.view.InflateException: Binary XML file line #1 in Mono.Android.NET_Tests:layout/uppercase_custom: Binary XML file line #1 in Mono.Android.NET_Tests:layout/uppercase_custom: Error inflating class Mono.Android_Test.Library.CustomTextView
Caused by: android.view.InflateException: Binary XML file line #1 in Mono.Android.NET_Tests:layout/uppercase_custom: Error inflating class Mono.Android_Test.Library.CustomTextView
Caused by: java.lang.ClassNotFoundException: Mono.Android_Test.Library.CustomTextView
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:815)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at crc643df67da7b13bb6b1.TestInstrumentation_1.n_onStart(Native Method)
at crc643df67da7b13bb6b1.TestInstrumentation_1.onStart(TestInstrumentation_1.java:32)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: java.lang.ClassNotFoundException: Didn't find class "Mono.Android_Test.Library.CustomTextView" on path
In this case, `Mono.Android_Test.Library.CustomTextView` was used from
an Android layout, but not used anywhere in managed code.
To fix, I added:
[DynamicDependency (DynamicallyAccessedMemberTypes.All, typeof (Mono.Android_Test.Library.CustomTextView))]
I could have also made `Mono.Android_Test.Library` a `@(TrimmerRootAssembly)`.1 parent 8a5b2a0 commit b521911
File tree
23 files changed
+130
-48
lines changed- external
- src
- Microsoft.Android.Templates
- android-bindinglib
- android-wear
- androidlib
- android
- Xamarin.Android.Build.Tasks
- Microsoft.Android.Sdk/targets
- Resources
- Tests
- Xamarin.Android.Build.Tests
- Xamarin.ProjectTools/Android
- tests
- CodeGen-Binding/Xamarin.Android.JcwGen-Tests
- Mono.Android-Tests
- Android.Graphics
- Android.Widget
- Java.Lang
- Runtime-Microsoft.Android.Sdk
- System
- System.Drawing
- System.Text.Json
- System
- Xamarin.Android.Net
23 files changed
+130
-48
lines changedSubmodule Java.Interop updated from 5bca8ad to 651de42
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
8 | 13 | | |
9 | 14 | | |
10 | 15 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
13 | 20 | | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
8 | 13 | | |
9 | 14 | | |
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| |||
Lines changed: 27 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
29 | 37 | | |
30 | 38 | | |
31 | 39 | | |
| |||
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
62 | 77 | | |
63 | 78 | | |
64 | 79 | | |
| |||
74 | 89 | | |
75 | 90 | | |
76 | 91 | | |
77 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
78 | 101 | | |
79 | 102 | | |
80 | 103 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
235 | 239 | | |
236 | 240 | | |
237 | 241 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
12 | 18 | | |
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
0 commit comments