0

I cannot publish my MAUI app

Hello

My MAUI app runs fine in Debug - in the emulator or while deployed on my Phone.

In Release mode, on both my emulator and my phone, after a looooong compile and deploy, it won't even start. The only message I get in the "Error List" in Visual Studio is

MSB3073: The command ""C:\Program Files (x86)\Android\android-sdk\platform-tools\adb" -s emulator-5554 uninstall -k "com.MyCompanyEtc.ExpensesMobile"" exited with code 1.

This is a simplified code

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();

        Routing.RegisterRoute("Login", typeof(View.Login.Login));
        Routing.RegisterRoute("DocumentScan", typeof(DocumentScan));
        .....
        CurrentItem = Login;
    }
}


[QueryProperty(nameof(Login), "Login")]
public partial class Login_VM : Base_VM
{
    public readonly LoginService loginService;

    private readonly IConnectivity connectivity;

    [ObservableProperty]
    private string password;
    
    public Login_VM(LoginService loginService, IConnectivity connectivity)
    {
        try
        {
            this.loginService = loginService;
            this.connectivity = connectivity;

            username = "MyName";
            password = "000";
        }
        catch (Exception ex)
        {
            ErrorHandling.HandleError(ex);
        }
    }

    [RelayCommand]
    private async Task LoginAsync()
    {
        if (IsBusy)
        {
            return;
        }

        try
        {
            if (connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                await Shell.Current.DisplayAlert(AppRes.MsgConnectivity1, AppRes.MsgConnectivity2, "OK"); //No connectivity....etc;
                return;
            }

            IsBusy = true;
            
            LoginStatus loginStatus = LoginService.Login(username, password);
            if (loginStatus == LoginStatus.loginAcceptedAdmin)
            {
                Globals.User ??= new User()
                {
                    Name = username
                };
                ....
                await Shell.Current.GoToAsync($"//{nameof(Pending)}");
            }
            else if (LoginService.Login(username, password) == LoginStatus.loginAcceptedRegularUser)
            {
                Globals.User ??= new User()
                {
                    Name = username
                };
                ....
                await Shell.Current.GoToAsync($"//{nameof(Pending)}");
            }
            else if (LoginService.Login(username, password) == LoginStatus.loginRefused)
            {
                await Shell.Current.DisplayAlert(AppRes.Login, AppRes.LoginWrongUsernameOrPass, "OK"); //Wrong username and/or password !
            }

            Globals.loginStatus = loginStatus;
        }
        catch (Exception ex)
        {
            ErrorHandling.HandleError(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }
}

In the Tools/Android/DeviceLog I have this (snip):

at ExpensesMobile.View.Login.Login..ctor(Login_VM 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 )
02-15 15:24:20.473  pixel_5_-_api_33    Info    5453    MonoDroid   android.runtime.JavaProxyThrowable: System.FieldAccessException: Field `Microsoft.Maui.Controls.VisualElement:ZIndexProperty' is inaccessible from method `ExpensesMobile.View.Login.Login:InitializeComponent ()'
................
02-15 15:24:20.444  pixel_5_-_api_33    Error   5453    AndroidRuntime  
   at ExpensesMobile.View.Login.Login..ctor(Login_VM viewModel)

Thank you Alex

Alex
  • 157
  • 8
  • As per https://stackoverflow.com/questions/74566743/maui-app-not-working-in-release-mode-but-it-works-perfectly-in-debug-mode, I have tried to disable this compiled binding.... but.... no joy ! :-( – Alex Jan 24 '23 at 18:08
  • Do you have exception handling, logging, or crash reporting tools setup? Have you checked the logs? There is a lot of basic debugging you could do – Jason Jan 24 '23 at 18:11
  • Steve, I have checked the DeviceLog on my Android Emulator. The only thing I've found more interesting is this: ...at ExpensesMobile.View.Login.Login..ctor(Login_VM 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 ) etc etc But why does it run in Debug if there's an error in my constructor of my Login viewModel ? – Alex Jan 24 '23 at 18:37
  • 1
    The debug and release configs can have different linker settings – Jason Jan 24 '23 at 18:45
  • You have some "timing" problem or "race condition". Disconnecting the debugger allows JIT-compilation of code, so it runs faster. This may change the order in which two different events occur. On emulator, launch app from emulator's home screen. does it crash there, now that debugger is not attached? Regardless, you'll need to isolate the problem - which is a pain since it doesn't happen with debugger. Wrap contents of LoginPage's constructor in `try .. catch (Exception ex) ...`, and log any exception to a log file. "Divide and conquer": comment out different parts of login code, what crashes? – ToolmakerSteve Jan 24 '23 at 19:47
  • Hello. Some info, after more testing: The RELEASE version won't even start (phone or emulator). The DEBUG version crashes as well if I start it from the phone or from the emulator, it doesn't matter.... It's just that it crashes a bit later in the login process in the emulator than on the phone. The DEBUG version does NOT crash if I start it from VS. So, in other words, my App only works as a DEBUG build if a debugger is attached (if I start it from Visual Studio). Also, I do not have much code in Login, for now it's just dummy code (return true). That's all I have there ! – Alex Jan 25 '23 at 08:46
  • Hello. The error apparently appears on my "login" VM constructor (from the log): ... at ExpensesMobile.View.Login.Login..ctor(Login_VM viewModel) at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean ) etc. My code is just a stubf for now: public Login_VM(LoginService loginService, IConnectivity connectivity) { this.loginService = loginService; this.connectivity = connectivity; username = "Alex"; password = "000"; } No breakpoint can be hit in Release mode. – Alex Feb 14 '23 at 16:31
  • @Jianwei Sun, I have updated my question (see above). Thank you. – Alex Feb 15 '23 at 14:15

0 Answers0