在MAUI最初发布的时候就曾创建过几个模板项目进行体验过,没遇到什么坑。由于最近需要开发针对餐饮行业的收银机(安卓系统)开发一款应用,这种收银机一般配置不咋滴,系统版本和性能也肯定比不上我们自己使用的手机。在做技术选型时首先想到了MAUI,备选Flutter,React Native。都是大厂维护的跨平台应用框架,在使用MAUI框架时遇到新建的模板应用居然启动时直接闪退,最终也是解决了这个闪退问题,遂分享下这一经历。
create a project
Demonstrate the process of creating a project. All processes are default to the IDE and no modifications are made.
Create a new MAUI template project
用 VS 新建MAUI模板项目,如下

项目名也默认为MauiApp1

connected devices
- Connect the target Android device via USB
- The target device turns on developer mode, and then turns on usb debugging (self
BaiduBing/Google) - Switching usb debugging mode on your mobile phone will generally cause charging only, file transfer... Simply and rudely switch between various options when VS lists your device.

commissioning items
Complete the device connection in the previous step and start the project in direct debug mode

Wait for a while, and you can see that the application has been installed on the device. It should be opened automatically. After waiting for a long time, there is no movement, and new content is no longer output in the VS output window.

手动点击设备上的安装好的MauiApp1应用,然后刚看到启动页面一个大大的.NET 标志,随后来了个 Maui1已停止运行

Solve flashback issues
以前也做过使用android studio开发过原生安卓应用,一般这种问题都能在 IDE 有错误输出,可以通过错误信息找到闪退原因。
回顾刚才这个问题,不知道去哪里查看日志,这该怎么去看闪退的原因呢,要是VS能像android studio那样可以查看详细的日志就好了,目前我还不知道是否有地方能看详细的 debug 日志。我选择了一种比较通用的排查错误的方式:adb工具。
有关adb不做介绍,读者如有疑问自行百度必应/谷歌,你只需要知道他是用来调试安卓应用的一个强大工具即可。
下面的流程需要你将adb目录添加到环境变量 PATH 中,方可全局使用adb命令。
frequently used commands
The adb command checks and lists the package names of all apps installed on the phone:
adb shell pm list packages
List all package names for the system application:
adb shell pm list packages -s
List the names of third-party application packages other than system applications:
adb shell pm list packages -3
Guess the keywords that may be included in a package:
adb shell dumpsys activity | findstr mFocusedActivity
Clear application data and cache
adb shell pm clear 应用包名
view the log
adb logcat
- V: Detailed (lowest priority)
- D: Debugging
- I: Information
- W: Warning
- E: Mistake
- F: Serious
- S: Silence (highest priority, no content has been output)
Find the journal we want to read
Make sure adb recognizes your device
$ adb devices
List of devices attached
1234567890ABCDEF device
Find out the name of our package
$ adb shell pm list packages -3
....
package:com.landi.print.service
package:com.companyname.mauiapp1
....
包名为:com.companyname.mauiapp1
- 使用
logcat
直接运行adb logcat能看到设备的所有日志,会对我们的排查造成干扰,我们只需要查看package:com.companyname.mauiapp1的日志,可以使用grep进行过滤,这个在windows的命令行工具都不支持,我使用的是GitBash的shell命令行工具,可以使用这一功能。
adb logcat | grep com.companyname.mauiapp1
这样就只会输出mauiapp1的日志了。
执行上面的命令后,点击mauiapp1应用图标启动应用,得到我们应用启动到崩溃的所有日志如下:
06-16 10:21:11.953 1424 1424 D Launcher2.2.10: flow not clicked com.companyname.mauiapp1crc64e632a077a20c694c.MainActivity
06-16 10:21:11.953 1424 1424 D Launcher2.2.10: flow click desktop com.companyname.mauiapp1crc64e632a077a20c694c.MainActivity
06-16 10:21:11.953 424 466 I ActivityManager: START u0 {act=android.intent.action.MAIN flg=0x10200000 cmp=com.companyname.mauiapp1/crc64e632a077a20c694c.MainActivity} from uid 10072
06-16 10:21:11.958 424 466 E ActivityManager: getPackageFerformanceMode--ComponentInfo{com.companyname.mauiapp1/crc64e632a077a20c694c.MainActivity}----com.companyname.mauiapp1
06-16 10:21:11.967 424 1456 E ActivityManager: getPackageFerformanceMode--ComponentInfo{com.companyname.mauiapp1/crc64e632a077a20c694c.MainActivity}----com.companyname.mauiapp1
06-16 10:21:11.987 424 1456 I ActivityManager: Start proc 19415:com.companyname.mauiapp1/u0a97 for activity com.companyname.mauiapp1/crc64e632a077a20c694c.MainActivity
06-16 10:21:12.173 19415 19415 D debug-app-helper: Checking if libmonodroid was unpacked to /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonodroid.so
06-16 10:21:12.173 19415 19415 D debug-app-helper: Native libs extracted to /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm, assuming application/android:extractNativeLibs == true
06-16 10:21:12.173 19415 19415 D debug-app-helper: Added filesystem DSO lookup location: /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm
06-16 10:21:12.173 19415 19415 W debug-app-helper: Using runtime path: /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm
06-16 10:21:12.173 19415 19415 W debug-app-helper: checking directory: `/data/user/0/com.companyname.mauiapp1/files/.__override__/lib`
06-16 10:21:12.173 19415 19415 W debug-app-helper: directory does not exist: `/data/user/0/com.companyname.mauiapp1/files/.__override__/lib`
06-16 10:21:12.173 19415 19415 W debug-app-helper: Checking whether Mono runtime exists at: /data/user/0/com.companyname.mauiapp1/files/.__override__/libmonosgen-2.0.so
06-16 10:21:12.173 19415 19415 W debug-app-helper: Checking whether Mono runtime exists at: /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonosgen-2.0.so
06-16 10:21:12.173 19415 19415 I debug-app-helper: Mono runtime found at: /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonosgen-2.0.so
06-16 10:21:12.192 19415 19415 W monodroid: Creating public update directory: `/data/user/0/com.companyname.mauiapp1/files/.__override__`
06-16 10:21:12.198 19415 19415 F monodroid: No assemblies found in '/data/user/0/com.companyname.mauiapp1/files/.__override__' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...
06-16 10:21:12.275 19433 19433 F DEBUG : pid: 19415, tid: 19415, name: nyname.mauiapp1 >>> com.companyname.mauiapp1 <<<
06-16 10:21:12.284 19433 19433 F DEBUG : Abort message: 'No assemblies found in '/data/user/0/com.companyname.mauiapp1/files/.__override__' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...'
06-16 10:21:12.288 19433 19433 F DEBUG : #01 pc 0001b08b /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::create_domain(_JNIEnv*, xamarin::android::jstring_array_wrapper&, bool, bool)+282)
06-16 10:21:12.288 19433 19433 F DEBUG : #02 pc 0001c08f /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::create_and_initialize_domain(_JNIEnv*, _jclass*, xamarin::android::jstring_array_wrapper&, xamarin::android::jstring_array_wrapper&, _jobjectArray*, xamarin::android::jstring_array_wrapper&, _jobject*, bool, bool, bool)+26)
06-16 10:21:12.288 19433 19433 F DEBUG : #03 pc 0001d2c5 /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::Java_mono_android_Runtime_initInternal(_JNIEnv*, _jclass*, _jstring*, _jobjectArray*, _jstring*, _jobjectArray*, _jobject*, _jobjectArray*, int, unsigned char, unsigned char)+4020)
06-16 10:21:12.288 19433 19433 F DEBUG : #04 pc 0001d55f /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/lib/arm/libmonodroid.so (Java_mono_android_Runtime_initInternal+50)
06-16 10:21:12.288 19433 19433 F DEBUG : #05 pc 0005282f /data/app/com.companyname.mauiapp1-Wpq5srmqUiNM5498jRmH8Q==/oat/arm/base.odex (offset 0x2e000)
06-16 10:21:12.905 424 19434 W ActivityManager: Force finishing activity com.companyname.mauiapp1/crc64e632a077a20c694c.MainActivity
06-16 10:21:12.916 424 451 I ActivityManager: Showing crash dialog for package com.companyname.mauiapp1 u0
06-16 10:21:12.976 424 877 I ActivityManager: Process com.companyname.mauiapp1 (pid 19415) has died: fore TOP
我们只关注日志级别为F,E的即可:
The following error message explains why the program hangs
06-16 10:21:12.198 19415 19415 F monodroid: No assemblies found in '/data/user/0/com.companyname.mauiapp1/files/.__override__' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...
06-16 10:21:12.275 19433 19433 F DEBUG : pid: 19415, tid: 19415, name: nyname.mauiapp1 >>> com.companyname.mauiapp1 <<<
06-16 10:21:12.284 19433 19433 F DEBUG : Abort message: 'No assemblies found in '/data/user/0/com.companyname.mauiapp1/files/.__override__' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...'
Next, we can use one of our important skills as programmers:BaiduGoogle, whether we can search for the correct answer depends on luck.
看来笔者有点东西,谷歌到了一个类似的案例:https://stackoverflow.com/questions/42336546/xamarin-android-application-crashed-after-clear-data-in-settings
有兴趣的去深究下,这里xamarin的解决办法是关闭 Use Fast Deployment
Modify project configuration

经过仔细查看属性配置文件,找到这个配置与stackoverflow上说的关闭 Use Fast Deployment极其相似,应该就是它了,关闭它再次使用VS以 debug 模式启动项目。
这次经过稍微漫长的过程,也执行到Found device: 1234567890ABCDEF不动了

Opening the app manually has no effect.
卸载Mauiapp1重试
Although the previous change had no effect, I firmly believed that this should be the case, so uninstall the app and try again to eliminate interference factors.

Found device: 1234567890ABCDEF之后不在卡住不动了,随后我的设备上也安装并自动打开了Mauiapp1,并且没有闪退!