baskren / Forms9Patch

Simplify image management and text formatting in your Xamarin.Forms apps
http://Forms9Patch.com
Other
128 stars 33 forks source link

[Bug] linker issue #62

Closed TomBruyneel closed 3 years ago

TomBruyneel commented 4 years ago

Description

When setting the linker to sdk AND user assemblies while using form9patch on Android I receive an error

Steps to Reproduce

  1. Use forms9patch in an android project and add the initialization code to the mainactivity
  2. add it to a xaml view as namespace xmlns:fp9="clr-namespace:Forms9Patch;assembly=Forms9Patch"
  3. use a forms9patch label in xaml <fp9:Label ...
  4. set the link mode to sdk and user assemblies

Expected Behavior

The application should start normally

Actual Behavior

The application throws following error:

07-15 15:58:25.289 E/mono-rt ( 3215): [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Forms9Patch.Label' threw an exception. ---> System.Exception: Unable to confirmed initialization. Did you forget to add Forms9Patch.Android.Settings.Initialize() after XamarinForms.Forms.Forms.Init()? 07-15 15:58:25.289 E/mono-rt ( 3215): at Forms9Patch.Settings.ConfirmInitialization () [0x00025] in C:\Users\ben\Development\Xamarin\Forms9Patch\Forms9Patch\Forms9Patch\Models\Settings.cs:104 07-15 15:58:25.289 E/mono-rt ( 3215): at Forms9Patch.Label..cctor () [0x00219] in C:\Users\ben\Development\Xamarin\Forms9Patch\Forms9Patch\Forms9Patch\Elements\Label.cs:252 07-15 15:58:25.289 E/mono-rt ( 3215): --- End of inner exception stack trace --- 07-15 15:58:25.289 E/mono-rt ( 3215): at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr) 07-15 15:58:25.289 E/mono-rt ( 3215): at Sodexo.ForYou.Views.LoginPage.InitializeComponent () [0x00023] in E:\prophets_git\sodexo\sodexo-4you-app\sodexo-4you\Sodexo.ForYou\Sodexo.ForYou\obj\Debug etstandard2.1\Views\LoginPage.xaml.g.cs:26 07-15 15:58:25.289 E/mono-rt ( 3215): at Sodexo.ForYou.Views.LoginPage..ctor (Sodexo.ForYou.ViewModels.LoginViewModel loginViewModel) [0x00008] in E:\prophets_git\sodexo\sodexo-4you-app\sodexo-4you\Sodexo.ForYou\Sodexo.ForYou\Views\LoginPage.xaml.cs:19 07-15 15:58:25.289 E/mono-rt ( 3215): at (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&) 07-15 15:58:25.289 E/mono-rt ( 3215): at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:936 07-15 15:58:25.289 E/mono-rt ( 3215): at DryIoc.ReflectionTools.TryRethrowWithPreservedStackTrace (System.Exception ex) [0x00018] in C:\Users\tom_b.nuget\packages\dryioc\4.2.4\contentFiles\cs etstandard2.0\DryIoc\Container.cs:12950 07-15 15:58:25.289 E/mono-rt ( 3215): at DryIoc.Interpreter.TryInterpretAndUnwrapContainerException (DryIoc.IResolverContext r, FastExpressionCompiler.LightExpression.Expression expr, System.Boolean useFec, System.Object& result) [0x00034] in C:\Users\tom_b.nuget\packages\dryioc\4.2.4\contentFiles\cs etstandard2.0\DryIoc\Container.cs:2758 07-15 15:58:25.289 E/mono-rt ( 3215): at DryIoc.Container.ResolveAndCache (System.Int32 serviceTypeHash, System.Type serviceType, DryIoc.IfUnresolved ifUnresolved) [0x00170] in C:\Users\tom_b.nuget\packages\dryioc\4.2.4\contentFiles\cs etstandard2.0\DryIoc\Container.cs:406 07-15 15:58:25.289 E/mono-rt ( 3215): at DryIoc.Container.DryIoc.IResolver.Resolve (System.Type serviceType, DryIoc.IfUnresolved ifUnresolved) [0x00181] in C:\Users\tom_b.nuget\packages\dryioc\4.2.4\contentFiles\cs etstandard2.0\DryIoc\Container.cs:353 07-15 15:58:25.289 E/mono-rt ( 3215): at DryIoc.Resolver.Resolve[TService] (DryIoc.IResolver resolver, DryIoc.IfUnresolved ifUnresolved) [0x00000] in C:\Users\tom_b.nuget\packages\dryioc\4.2.4\contentFiles\cs etstandard2.0\DryIoc\Container.cs:7583 07-15 15:58:25.289 E/mono-rt ( 3215): at Sodexo.ForYou.App.OnStart () [0x0012b] in E:\prophets_git\sodexo\sodexo-4you-app\sodexo-4you\Sodexo.ForYou\Sodexo.ForYou\App.xaml.cs:132 07-15 15:58:25.289 E/mono-rt ( 3215): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.34(intptr,intptr) 07-15 15:58:25.289 E/mono-rt ( 3215): at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.34(intptr,intptr)

Basic Information

TomBruyneel commented 4 years ago

Just to be clear, I did add the initialization call and in the other link modes it works as expected :)

baskren commented 4 years ago

@TomBruyneel

Yup. This is pretty much expected. I have never had the time to optimize Forms9Patch to survive the linking process. I am curious about your use case ... are you trying to squeeze every last bit of size out of your executable so it packages smaller or is there something else you have in mind?

TomBruyneel commented 4 years ago

@baskren no, that's pretty much it, my apk is kind of big and slow to start up and I was hoping to at least reduce the apk size and assembly count a bit by using full linking

TomBruyneel commented 4 years ago

I forked the project and found a simple solution, setting the preserve attribute on the settings class of all projects solves the problem. You want me to make a PR?

baskren commented 4 years ago

Yes, please.

Sent from my iPhone

On Jul 16, 2020, at 5:27 AM, TomBruyneel notifications@github.com wrote:

 I forked the project and found a simple solution, setting the preserve attribute on the settings class of all projects solves the problem. You want me to make a PR?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

TomBruyneel commented 4 years ago

I made a PR, I'm not claiming this will solve all linker issues, but it did solve mine.

baskren commented 4 years ago

Picking up on dialog in Pull Request:

After the pull request was tested, the following was observed:

For the sake of documenting this, the following tests were broken with Link All (vs Link Framework SDKs only):

User Pages / MFoster Button Binding Sample : No buttons are changing state as a result of clicking the [Toggle State] button; User Pages / MFloyd : Xamarin.Forms.Xaml.XamlParseException, Cannot assign property "ColumnSpacing"; User Pages / SegmentBindingPage: Text and images are missing from shaded cells; User Pages / XAML: F9P Label in Xamarin.ListView: frames in cells are missing background color; User Pages / ChatListPage: Bubbles not sized correctly and contains no text; Code / Picker in popup: Activation exception thrown; ListView Sandbox: Activation exception thrown; There is also a suite of apps I maintain that I use as a second set of tests. It has some really nasty use cases that seem to ferret out a lot of issues. I haven't tested against those as it usually takes a day or so.

And the I added:

I fogot to mention that the above tests were run on iOS. Haven't tried UWP or Android yet. Also, in case it wasn't clear, the tests are in the Demo app: https://github.com/baskren/Forms9Patch/tree/master/Demo

To which @TomBruyneel responded:

Ah yes, as said, I solved my problem to start with. I thought you meant I broke something in sdk only linker mode. But those are interesting results and I'm willing to help you resolve some of them

And I replied:

Yes ... any help you can give would be GREATLY appreciated. I just made a commit to master (I should have branched it) that updates the test cases for Link All. The last two issues I think are related and I'm going to look at them now. If you do have any time to look at the first 5 issues, that would be most helpful!

baskren commented 4 years ago

Commit https://github.com/baskren/Forms9Patch/commit/d340d26cb4f6f71bbf934d822935a831b190f8b0 appears to fix the last two failed test.