我们正在使用 Azure DevOps 在内部部署我们的 Xamarin android 应用程序。该应用程序从 VS2019 顺利构建、部署和运行。我们还可以使用 Android Package Signing 选项对我们的 apk 进行签名。
从 Azure DevOps 构建、签名和放置 .apk 工作正常,但我们遇到了挂起和崩溃(分别)。如果我们修复挂起,就会发生崩溃。
在检查“Zipalign”选项之前,我们的应用会挂在 SplashActivity 上。检查“Zipalign”选项后,我们的应用似乎从 SplashActivity 切换到 MainActivity,但随后崩溃并显示“应用一直在停止”。从这里尝试什么?不知道如何调试问题,因为从 VS 部署调试或发布时不会发生这种情况。
编辑1
尝试实施错误处理以捕获错误,但似乎没有受到打击。
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
AppDomain.CurrentDomain.UnhandledException
+= CurrentDomainOnUnhandledException;
System.Threading.Tasks.TaskScheduler.UnobservedTaskException
+= TaskSchedulerOnUnobservedTaskException;
Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser
+= OnAndroidEnvironmentUnhandledExceptionRaiser;
global::Xamarin.Forms.Forms.Init(this, bundle);
DisplayCrashReport();
LoadApplication(new App());
}
#region Error handling
public static void TaskSchedulerOnUnobservedTaskException(object sender,
System.Threading.Tasks.UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
{
var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
LogUnhandledException(newExc);
}
public static void CurrentDomainOnUnhandledException(object sender,
UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
LogUnhandledException(newExc);
}
private void OnAndroidEnvironmentUnhandledExceptionRaiser(object sender,
Android.Runtime.RaiseThrowableEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("OnAndroidEnvironmentUnhandledExceptionRaiser", unhandledExceptionEventArgs.Exception);
LogUnhandledException(newExc);
}
internal static void LogUnhandledException(System.Exception exception)
{
try
{
const string errorFileName = "Fatal.log";
var libraryPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal); // iOS: Environment.SpecialFolder.Resources
var errorFilePath = System.IO.Path.Combine(libraryPath, errorFileName);
var errorMessage = System.String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
System.DateTime.Now, exception.ToString());
System.IO.File.WriteAllText(errorFilePath, errorMessage);
// Log to Android Device Logging.
Android.Util.Log.Error("Crash Report", errorMessage);
}
catch
{
// just suppress any error logging exceptions
}
}
/// <summary>
// If there is an unhandled exception, the exception information is diplayed
// on screen the next time the app is started
/// </summary>
[System.Diagnostics.Conditional("DEBUG")]
public void DisplayCrashReport()
{
const string errorFilename = "Fatal.log";
var libraryPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
var errorFilePath = System.IO.Path.Combine(libraryPath, errorFilename);
if (!System.IO.File.Exists(errorFilePath))
{
return;
}
var errorText = System.IO.File.ReadAllText(errorFilePath);
new Android.App.AlertDialog.Builder(this)
.SetPositiveButton("Clear", (sender, args) =>
{
System.IO.File.Delete(errorFilePath);
})
.SetNegativeButton("Close", (sender, args) =>
{
// User pressed Close.
})
.SetMessage(errorText)
.SetTitle("Crash Report! MainActivity")
.Show();
}
#endregion