Skip to content

C# type of Preference trimmed away in Release builds, causing NullReferenceException #9399

@tipa

Description

@tipa

Android framework version

net9.0-android

Affected platform version

.NET9 RC2

Description

C# Type seems to be trimmed away, causing problems in Release builds.
The problem did not occur in .NET 8

Steps to Reproduce

In my app I am using a Preference screen, with a SwitchPreferenceCompat specified in the XML.

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
  <PreferenceCategory>
    <SwitchPreferenceCompat app:key="test" app:title="Test"/>
  </PreferenceCategory>
</PreferenceScreen>

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();

Example project to reproduce: New folder.zip

Did you find any workaround?

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)

Relevant log output

No response

Metadata

Metadata

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions