You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
C# type of Preference trimmed away in Release builds, causing NullReferenceException #9399
In my C# code, I want to get a reference to it in order to initialize:
var test = FindPreference("test") as SwitchPreferenceCompat;
test.PreferenceChange += (_, _) => Toast.MakeText(Context, "toggled", ToastLength.Long).Show();
I can use a subclass TwoStatePreference or just Preference instead the as SwitchPreferenceCompat case. I assume that the C# type of SwitchPreferenceCompat was trimmed away because no direct properties or methods of it was accessed (only of it's subclasses)
No crash when I remove TrimMode=full. I forgot to mention that I build & deploy the app using dotnet publish -t:Run, it should be easy to reproduce with my example project.
I was able to resolve the problem in my project easily by casting to another (sub)class, just wanted to make sure you are aware of that edge case. I absolutely love seeing improvements to the trimmer & app size year after year. My app sizes were reduced by another ~20% after migrating to .NET9. Thank you!!
I've been debugging the trimmer to understand what is happening:
Suppose the only usage of a type such as NinePatchDrawable is d as NinePatchDrawable, same as the issue here.
The trimmer optimizes this completely away:
[Test][TestCaseSource("NinePatchDrawables")]publicvoidDrawableFromRes_ShouldBeTypeNinePatchDrawable(intresId,stringname){Assert.IsNotNull(Application.Context.Resources.GetDrawable(resId),"An image should have been retrieved from resource `"+name+"`.");Assert.IsNotNull(null,"The drawable created from resource `"+name+"` should be a NinePatchDrawable.");}
This optimization pass happens during MarkStep, and MarkJavaObjects happens later. We are trying to "preserve any Java objects" that are left after MarkStep, but MarkStep is removing any usage of NinePatchDrawable.
Unsure what to do here yet, will discuss with our team.
Activity
jonathanpeppers commentedon Oct 14, 2024
The app specifies
TrimMode=full, does the problem go away if you remove this setting? (blank would beTrimMode=partial)tipa commentedon Oct 15, 2024
No crash when I remove
TrimMode=full. I forgot to mention that I build & deploy the app usingdotnet publish -t:Run, it should be easy to reproduce with my example project.I was able to resolve the problem in my project easily by casting to another (sub)class, just wanted to make sure you are aware of that edge case. I absolutely love seeing improvements to the trimmer & app size year after year. My app sizes were reduced by another ~20% after migrating to .NET9. Thank you!!
jonathanpeppers commentedon Oct 15, 2024
This also fixes the sample:
But I'll investigate what we should do here. I suspect the "MarkJavaObjects" trimmer step could need changes here.
jonathanpeppers commentedon Oct 15, 2024
The case is actually similar to a test we have here:
android/tests/Mono.Android-Tests/Android.Graphics/NinePatchTests.cs
Lines 27 to 34 in f09caed
NinePatchDrawable's constructors weren't preserved, and so we had no way to cast to it from Java via the(IntPtr,JniHandleOwnership)constructor.jonathanpeppers commentedon Oct 15, 2024
I've been debugging the trimmer to understand what is happening:
Suppose the only usage of a type such as
NinePatchDrawableisd as NinePatchDrawable, same as the issue here.The trimmer optimizes this completely away:
It's literally inlined to
null!This optimization pass happens during
MarkStep, andMarkJavaObjectshappens later. We are trying to "preserve any Java objects" that are left afterMarkStep, butMarkStepis removing any usage ofNinePatchDrawable.Unsure what to do here yet, will discuss with our team.
[trimming] mark Java objects as "instantiated"
jonathanpeppers commentedon Oct 21, 2024
Another workaround is:
If a type can be created in Java, this optimization can break.
We are making this change, as it appears to affect app size only a minor amount: less than our threshold of 5% per file or 3% overall.
[trimmer] pass `--disable-opt unusedtypechecks` (dotnet#9435)