MAUI app not working in release mode but it works perfectly in debug mode
Asked Answered
W

9

10

My MAUI App when running in Debug mode in a emulator is working good but it is not running when I run this in release mode ( does not work either in emulator or a physical device) it crashes.

I am using the Mvvm architecture using a library called CommunityToolkit.Mvvm. I am mentioning this because I do not know if this is causing the problem.

I downloaded an app called Crash Log Viewer in my physical device to inspect the crashes and errors that apps are giving and I got this error:

This information is generated from the Crash Log View application. - https://play.google.com/store/apps/details?id=com.arumcomm.crashlogviewer

Process: com.companyname.ventas_citel
PID: 12230
UID: 10313
Flags: 0x20c8be44
Package: com.companyname.ventas_citel v1 (1.0)
Foreground: No
Process-Runtime: 63820382
Build: samsung/a10sub/a10s:11/RP1A.200720.012/A107MUBU6CVG3:user/release-keys

android.runtime.JavaProxyThrowable: System.FieldAccessException: Field `Microsoft.Maui.Controls.VisualElement:ZIndexProperty' is inaccessible from method `Ventas_Citel.Views.Login.Login:InitializeComponent ()'

   at Ventas_Citel.Views.Login.Login..ctor(LoginViewModel viewModel)
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean )
   at System.Reflection.RuntimeConstructorInfo.DoInvoke(Object , BindingFlags , Binder , Object[] , CultureInfo )
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags , Binder , Object[] , CultureInfo )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite , RuntimeResolverContext , ServiceProviderEngineScope , RuntimeResolverLock )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite , RuntimeResolverContext )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c_DisplayClass2_0.<RealizeService>b_0(ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type , ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type )
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
   at Microsoft.Maui.Controls.ShellContent.<>c_DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b_0()
   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr , IntPtr , IntPtr , IntPtr , IntPtr )
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L , IntPtr , IntPtr , IntPtr , IntPtr , IntPtr )
    at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
    at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:42)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
    at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2777)
    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3020)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
    at android.app.Activity.performStart(Activity.java:8231)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3872)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Seems like the error is in the LoginViewModel so I am sharing you my code files:

MauiProgram

    public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder.UseMauiApp<App>().UseMauiCommunityToolkit();

        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });


        //views
        builder.Services.AddSingleton<Login>();
        builder.Services.AddSingleton<Inventario>();
        builder.Services.AddSingleton<Clientes>();
        builder.Services.AddSingleton<Ventas>();
        builder.Services.AddSingleton<CuentasPorCobrar>();


        //viewModels
        builder.Services.AddSingleton<LoginViewModel>();
        builder.Services.AddSingleton<InventarioViewModel>();
        builder.Services.AddSingleton<ClientesViewModel>();
        builder.Services.AddSingleton<VentasViewModel>();
        builder.Services.AddSingleton<CuentasPorCobrarViewModel>();


        // services
        builder.Services.AddHttpClient<ILoginService, LoginService>();


        return builder.Build();


    }
}

LoginViewModel

public partial class LoginViewModel : BaseViewModel
{
    [ObservableProperty]
    string uuid;

    [ObservableProperty]
    private string _email;

    [ObservableProperty]
    private string _password;

    [ObservableProperty]
    private bool _isEnabled = true;

    private readonly ILoginService _loginService;
    public LoginViewModel(ILoginService loginService)
    {
        ComprobarUUID();
        _loginService = loginService;
        IsEnabled = true;
    }

    private async void ComprobarUUID()
    {
        // .. code
    }

    private async void CargarUUIDPorPrimeraVez()
    {
        // .. code
    }

    [RelayCommand]
    async void Copiar()
    {
        // .. code
    }



}

BaseViewModel

 public partial class BaseViewModel : ObservableObject
    {
        [ObservableProperty]
        private bool _isBusy;

        [ObservableProperty]
        private string _title;
    }

Login.xaml.cs

public partial class Login : ContentPage
{
    public Login(LoginViewModel viewModel)
    {
        InitializeComponent();
        BindingContext = viewModel;
    }
}

I have working so hard in this app and I always been building this app through debug mode but today I had to deliver this app to the team but this did not work.

How do I solve this error? Do you need any more code?

Workout answered 24/11, 2022 at 22:24 Comment(3)
The exception occurs while Login page constructor runs InitializeComponent. That means something in XAML has caused the problem. Given that it works in debug mode, sounds like a Maui bug. If Alexandar's suggestion doesn't fix, try commenting out stuff in login.xaml, to isolate what line causes it. If you find out what line causes, reduce xaml to minimum that causes crash, then add that complete xaml to question. If no one has a fix, then create a new issue at github maui issues.Purpura
@Reyneer, a similar question&answers is here #74557330Cuspidate
if you are finding the problem on iOS physical device it could be related to this: github.com/dotnet/maui/issues/12316Stroll
R
11

Remove zIndex from xaml. and use alternate any approch. i had very bad hours finding out what causing crash in release mode. and this was the fix. can't beleave Maui is in production.

Rigorous answered 29/12, 2022 at 7:31 Comment(0)
K
6

You can try to disable the compiled binding like below:

[XamlCompilation (XamlCompilationOptions.Skip)]
public partial class Login : ContentPage
{
    public Login(LoginViewModel viewModel)
    {
        InitializeComponent();
        BindingContext = viewModel;
    }
}
Kelikeligot answered 25/11, 2022 at 1:16 Comment(1)
this solved my issue.. but .. why? is this a known issue in maui?Graver
R
6

In my case, I was only having trouble in Android Platform. The same: debug build worked perfect, but realease build just crashed at startup. The solution for me was to uncheck AOT Compilation in the project properties, which was by default unchecked for debug but checked for release:

enter image description here

Russia answered 19/5, 2023 at 4:59 Comment(2)
Same problem. Thanks. Haven't found the reason why this is a problem yet.Turoff
This is a lifesaver. In my case the MAUI Android app (.net 8) was having notification issues ("No Channel found for" ...) for Release builds. I've disabled both AOT and "Optimize code" options in the project properties for Release builds (... as they differed from debug settings) and the issue went away.Columbium
K
4

These answers were ultimately helpful, for me it was also an android only thing, a XAML code error. It would crash on a page that had the incorrect StaticResource set for TextColor.

I was using:
TextColor="{StaticResource PrimaryColor}" when it should have been TextColor="{StaticResource Primary}"

Oddly this worked fine in the debugger but showed the wrong color when deployed to my physical phone. While it is debugging it works fine, but once I disconnect my phone then it would crash. Also I tried to use the app without being connected. Obviously it did not work with Release either.

Hope this helps someone else.

Kingsbury answered 18/7, 2023 at 3:31 Comment(0)
T
3

Adding "[XamlCompilation (XamlCompilationOptions.Skip)]" in the MainPage.xaml.cs file right above the class statement and disabling the AOT switches in the project properties worked great for me too.

First part of my MainPage file:

namespace your_namespace

{

    [XamlCompilation(XamlCompilationOptions.Skip)]

    public partial class MainPage : ContentPage

    {

Theatrician answered 5/8, 2023 at 14:58 Comment(0)
H
2

I have same issue. In My Case Xaml Error. Below Code has Error.

<StackLayout Orientation="Horizontal"
            RadioButtonGroup.GroupName="interest" 
            RadioButtonGroup.SelectedValue="{Binding rdinterestTaxRATIO}">
<RadioButton Content="15.4%" 
             Value="15.4"
             RadioButtonGroup.GroupName="interest" 
             Margin="15"
             IsChecked="True"/>
<RadioButton Content="1.4%" 
             Value="1.4"
             RadioButtonGroup.GroupName="interest" 
             Margin="15"
             IsChecked="False"/>
<RadioButton Content="0%" 
             Value="0"
             RadioButtonGroup.GroupName="interest" 
             Margin="15"
             IsChecked="False"/>

Below code everything okay

<StackLayout Orientation="Horizontal"
            RadioButtonGroup.GroupName="interest" 
            RadioButtonGroup.SelectedValue="{Binding rdinterestTaxRATIO}">
<RadioButton Content="15.4%" 
                Value="15.4"
                Margin="15"
                IsChecked="True"/>
<RadioButton Content="1.4%" 
                Value="1.4"
                Margin="15"
                IsChecked="False"/>
<RadioButton Content="0%" 
                Value="0"
                Margin="15"
                IsChecked="False"/>
I just delete duplicated property.
Hagans answered 8/2, 2023 at 6:27 Comment(0)
I
1

For three days I have been chasing a similar problem and everything was pointing to the community toolkit as all I needed to do was comment out observablecollection.add and everything worked. Then I tried different linking and all that stuff. After reading this all I needed to do was eliminate this: Style="{StaticResource CardView}" from my XAML (which I stole from another project).

India answered 10/2 at 20:10 Comment(0)
S
0

Pitch in my 2 cents here, I had yet another issue in my XAML - had recently changed the view model type name but didn't update the page's x:DataType to match.

  1. Added [XamlCompilation(XamlCompilationOptions.Skip)] as advised, verified it skipped over the compilation error
  2. Corrected x:DataType to match updated view model
  3. Removed [XamlCompilation(XamlCompilationOptions.Skip)], verified app worked as expected
Simeon answered 4/9, 2023 at 22:0 Comment(0)
K
0

hey lads I'm having this very issue using a IValueConverter , funny part is that this very same code worked for me on a different Page rather the one I'm having the issue

 public class ColorDarkConverter : IValueConverter
{
     

    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value != null)
        {
            switch (value)
            {
                case "IndianRed":
                    return "DarkRed";
                     
                case "Green":
                    return "DarkGreen";
                 case "Gold":
                    return "DarkGoldenrod";
                 default:
                    return value;
            }
        }
        
        else return value;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        //throw new NotImplementedException();
    }
}

the app stoped crashing after I removed actual converter from my xaml file.

enter image description here

I hope this is helpful to any one

Komara answered 25/12, 2023 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.