Xamarin.Android TextView doesn't raise change events when "Link SDK Assemblies" is selected

168 views Asked by At

I'm working on a project in Xamarin with MvvmCross, trying to use the linker to get the app size down for release on Android and iOS (no Forms.)

On Android, when I selected "Link SDK Assemblies Only", the project builds and runs with no crashes or error messages. But none of the EditText controls responds correctly when their text changes, their data bindings don't get updated, and their Changed event handlers don't get called.

When I select "Don't Link", everything works fine. Bindings are updated and event handlers are called.

I have looked at the Xamarin Linker documentation, and I'm aware of how to ensure various assemblies, types and methods are preserved. My problem is I simply don't know what needs to be preserved, nor do I have any idea how to find out. I've tried tools like bitdiffer with no success.

Can anyone help me figure out what I need to preserve if anything?

2

There are 2 answers

0
Trevor Balcom On BEST ANSWER

Add a LinkerPleaseInclude.cs file to your Android project containing the following code:

public class LinkerPleaseInclude
{
    public void Include(TextView text)
    {
        text.AfterTextChanged += (sender, args) => text.Text = "" + text.Text;
        text.Hint = "" + text.Hint;
    }
}

EditText is inheriting from TextView so this will take care of your problem by making the linker think you reference the TextView.AfterTextChanged event. You can find a reference Android LinkerPleaseInclude.cs file here.

0
tequila slammer On

This behavior is often seen if the LinkerPleaseInclude file is missing a reference to your EditText. The reason is that the linker strips everything not referenced/ used from your app. With a reference in the LinkerPleaseInclude file the linker will notice the usage and keeps the event handler.