MAUI Android应用模拟器运行正常,发布APK后启动崩溃求助
发布签名APK后Android应用闪屏崩溃问题
我使用Visual Studio Community Edition 2022开发Android应用,在内置Pixel模拟器或通过USB连接的真机上运行时一切正常,但生成签名发布APK并安装到同一设备或其他Android手机后,应用在闪屏后立即崩溃。
已尝试的解决方案
在.csproj文件中添加以下属性:
<PublishTrimmed>False</PublishTrimmed> <AndroidLinkMode>None</AndroidLinkMode> <AndroidDexTool>d8</AndroidDexTool> <PropertyGroup> <AndroidPackageFormat>apk</AndroidPackageFormat> </PropertyGroup> <PropertyGroup> <AndroidEnableAssemblyCompression>false</AndroidEnableAssemblyCompression> </PropertyGroup>
但问题仍未解决。
相关代码文件
MauiProgram.cs
using CommunityToolkit.Maui; namespace V2ROffline; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UseMauiCommunityToolkit() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); builder.Services.AddSingleton<MainPage>(); builder.Services.AddTransient<StockTakeWithoutFile>(); return builder.Build(); } }
App.xaml.cs
namespace V2ROffline; public partial class App : Application { public App() { InitializeComponent(); MainPage = new AppShell(); } }
App.xaml
<?xml version = "1.0" encoding = "UTF-8" ?> <Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:V2ROffline" x:Class="V2ROffline.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/Styles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
AppShell.xaml.cs
namespace V2ROffline; public partial class AppShell : Shell { public AppShell() { InitializeComponent(); Routing.RegisterRoute(nameof(StockTakeWithoutFile), typeof(StockTakeWithoutFile)); } }
AppShell.xaml
<?xml version="1.0" encoding="UTF-8" ?> <Shell x:Class="V2ROffline.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:V2ROffline" Shell.FlyoutBehavior="Disabled"> <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> </Shell>
MainPage.xaml.cs
namespace V2ROffline; using System.Text.RegularExpressions; using V2ROffline.lib; public partial class MainPage : ContentPage { int count = 0; public MainPage() { InitializeComponent(); /** Following code for checkWritePermission is not called I believe app is crashing even before InitializeComponent() **/ Task task = checkWritePermission(); this.Title = Vars.TITLE + "-" + Vars.VERSION; TxtEmpCode.Focus(); } private async Task checkWritePermission() { PermissionStatus status = await Permissions.RequestAsync<Permissions.StorageWrite>(); status = await Permissions.RequestAsync<Permissions.StorageWrite>(); } private void OnLoginClicked(object sender, EventArgs e) { string siteCode = TxtSiteCode.Text.Trim(); string empCode = TxtEmpCode.Text.Trim(); Regex regexSite = new Regex("^[a-zA-Z]{2}[0-9]{2}"); Regex regexEmp = new Regex("^[a-zA-Z0-9]"); if (!(empCode.Length >= 5 && empCode.Length <= 7 && regexEmp.IsMatch(empCode))) { DisplayAlert("Error", "Please enter valid Employee Code", "OK"); TxtEmpCode.Focus(); return; } if (!(siteCode.Length == 4 && regexSite.IsMatch(siteCode))) { DisplayAlert("Error", "Please enter valid Site Code", "OK"); TxtSiteCode.Focus(); return; } siteCode = siteCode.ToUpper(); empCode = empCode.ToUpper(); if (RbtnWithFile.IsChecked) { } else if (RbtnWithoutFile.IsChecked) { Task task = openStockTakeWithoutFile(empCode, siteCode); } else { DisplayAlert("Error", "Please select the mode of Stock Take", "OK"); RbtnWithFile.Focus(); return; } } async Task openStockTakeWithoutFile(string empCode, string siteCode) { var navigationParameter = new Dictionary<string, object> { { "EmpCode", empCode},{ "SiteCode", siteCode} }; await Shell.Current.GoToAsync(nameof(StockTakeWithoutFile), navigationParameter); } private void OnExitClicked(object sender, EventArgs e) { App.Current.Quit(); } }
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" x:Class="V2ROffline.MainPage"> <ContentPage.Resources> <Style x:Key="InvalidEntryStyle" TargetType="Entry"> <Setter Property="TextColor" Value="Red" /> </Style> <Style x:Key="ValidEntryStyle" TargetType="Entry"> <Setter Property="TextColor" Value="Green" /> </Style> </ContentPage.Resources> <Grid Margin="50,50" x:Name="GridList" VerticalOptions="Center"> <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions Padding="10,0"> <ColumnDefinition Width="120"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Text="Employee Code" FontAttributes="Bold" HeightRequest="20" HorizontalOptions="Start" /> <Border Stroke="#505050" StrokeThickness="1" Background="#FFFFFF" Padding="10,0" Grid.Row="0" Grid.Column="1" HeightRequest="40" WidthRequest="170" HorizontalOptions="Start"> <Border.StrokeShape> <RoundRectangle CornerRadius="5,5,5,5" /> </Border.StrokeShape> <Entry x:Name="TxtEmpCode" Placeholder="Enter employee code" TextTransform="Uppercase" MaxLength="7" > <Entry.Behaviors> <toolkit:TextValidationBehavior InvalidStyle="{StaticResource InvalidEntryStyle}" ValidStyle="{StaticResource ValidEntryStyle}" Flags="ValidateOnValueChanged" MinimumLength="5" MaximumLength="7" /> </Entry.Behaviors> </Entry> </Border> <Label Grid.Row="1" Grid.Column="0" Text="Site Code" FontAttributes="Bold" HeightRequest="20" HorizontalOptions="Start" /> <Border Stroke="#505050" StrokeThickness="1" Background="#FFFFFF" Padding="10,0" Grid.Row="1" Grid.Column="1" HeightRequest="40" WidthRequest="170" HorizontalOptions="Start"> <Border.StrokeShape> <RoundRectangle CornerRadius="5,5,5,5" /> </Border.StrokeShape> <Entry x:Name="TxtSiteCode" Placeholder="Enter site code here" TextTransform="Uppercase" MaxLength="4" > <Entry.Behaviors> <toolkit:TextValidationBehavior InvalidStyle="{StaticResource InvalidEntryStyle}" ValidStyle="{StaticResource ValidEntryStyle}" Flags="ValidateOnValueChanged" MinimumLength="4" MaximumLength="4" /> </Entry.Behaviors> </Entry> </Border> <RadioButton x:Name="RbtnWithFile" RadioButtonGroup.GroupName="StockTake" HeightRequest="30" Content="Stock take with file" Grid.Row="2" Grid.ColumnSpan="2" /> <RadioButton x:Name="RbtnWithoutFile" RadioButtonGroup.GroupName="StockTake" HeightRequest="30" Content="Stock take without file" Grid.Row="3" Grid.ColumnSpan="2" /> <Button Grid.Row="4" Grid.Column="0" x:Name="BtnExit" Margin="0,10,0,0" Text="Exit" WidthRequest="120" Clicked="OnExitClicked" HorizontalOptions="Center" /> <Button Grid.Row="4" Grid.Column="1" x:Name="BtnLogin" Margin="0,10,0,0" Text="Login" WidthRequest="120" Clicked="OnLoginClicked" HorizontalOptions="Center" /> </Grid> </ContentPage>
问题排查怀疑
我怀疑应用在InitializeComponent()执行前就已经崩溃,因为checkWritePermission()代码完全没有被调用。
内容的提问来源于stack exchange,提问作者maddy23285
相关产品推荐
相关产品推荐

