Search code examples
c#maui.net-7.0maui-android

Troubleshooting Release Build Error in .NET MAUI Application for Android


I am building a .NET MAUI application using .NET version 7.0.306(x64) and Visual Studio 2022 version 17.6.5. I believe these are the latest versions. Currently, I am focusing on Android.

The Problem: In debug mode, the application works flawlessly. However, the problem occurs when I try to build the app for release and run it on my real physical test device (Samsung). Just at the end of Visual Studio's compiling process, I get the following error message:

Severity    Code    Description Project File    Line    Suppression State
Warning     MSB3073: The command ""C:\Program Files (x86)\Android\android-sdk\platform-tools\adb" -s RFCN30KRB2M uninstall -k "com.company.appname"" exited with code 1.

Visual Studio somehow manages to install the app, but it's in a broken state. I uninstalled the app manually from the device before building it.

What I Have Tried: I have searched for answers online and found an old thread that seems to fit my problem description. However, it's for a Maui Blazor app. MauiIssue

I have tried what the issue mentions as a fix, but it did not resolve the problem.

What does work are the suggestions from this Stack Overflow question:

    <PropertyGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
        <RunAOTCompilation>false</RunAOTCompilation>
        <AndroidLinkMode>None</AndroidLinkMode>
    </PropertyGroup>

However, I have heard that these settings should not be turned off for release mode, as the package can become quite large. I've also heard that AOT (Ahead-of-Time compilation) is really important for iOS.

My Question: Does anybody know what's causing this issue, and if it has been fixed? Which .NET version should I be running if it has already been resolved?

Edit: I just created a new MAUI project from scratch to see if an untouched project would build in release mode from the start. I still get the same error. Am I missing something? Is MAUI ready for production?

The MauiApp1 seems to work, but then again, it doesn't have much logic in it. I tried to build my own app with the linker and AOT turned off, and the main functionality, which uses Azure Cognitive Service, won't work properly. I can't debug either because the debugger won't attach in release mode. Is that how it's supposed to be?

Edit 2: I just updated to Visual Studio 2022 version 17.7 (came out 8 aug, patch thuesday). I`ll give it a try and see if it fixes the issues I have. It did not work.

Edit 3: I installed .NET 8.0.100-preview and Visual Studio version 17.8.0 preview 1, but I had no luck there either. I don't know what to try anymore. I should have tried to build the project in release mode before pouring weeks of development into my app. Is anyone else having this problem, and if so, how did you solve it?

Button style:

<Style TargetType="Button" x:Key="ButtonStyle">
    <Setter Property="FontFamily" Value="OpenSansRegular"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Padding" Value="10,10"/>

    <Setter Property="Margin" Value="5" />
    <Setter Property="MinimumHeightRequest" Value="65"/>
    <Setter Property="MinimumWidthRequest" Value="65"/>
    <Setter Property="BackgroundColor" Value="{StaticResource Primary}" />
    <Setter Property="TextColor" Value="{StaticResource Black}" />
    <Setter Property="CornerRadius" Value="10"/>
    <Setter Property="Shadow">
        <Setter.Value>
            <Shadow Brush="{StaticResource TertiaryBrush}" Offset="10,10" Radius="40" Opacity="0.80"/>
        </Setter.Value>
    </Setter>

    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Disabled">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
                        <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

Solution

  • I can reproduce your problem in my project. And I found the TextColor will change but the BackgroundColor not when I test it in the release mode.

    So I use the Background property instead of the BackgroundColor property and it worked. Such as:

    <Style TargetType="Button" x:Key="ButtonStyle">
        <Setter Property="FontFamily" Value="OpenSansRegular"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Padding" Value="10,10"/>
    
        <Setter Property="Margin" Value="5" />
        <Setter Property="MinimumHeightRequest" Value="65"/>
        <Setter Property="MinimumWidthRequest" Value="65"/>
        <Setter Property="Background" Value="{StaticResource Primary}" />
        <Setter Property="TextColor" Value="{StaticResource Black}" />
        <Setter Property="CornerRadius" Value="10"/>
        <Setter Property="Shadow">
            <Setter.Value>
                <Shadow Brush="{StaticResource TertiaryBrush}" Offset="10,10" Radius="40" Opacity="0.80"/>
            </Setter.Value>
        </Setter>
    
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal" />
                    <VisualState x:Name="Disabled">
                        <VisualState.Setters>
                            <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
                            <Setter Property="Background" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray600}}" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
    

    For more information, you can refer to this known issue about iOS and Android in releasemode does not change background color on button when changing states.