最近在 Android R(Android 11)系统上执行以前写的Espresso UI(AndroidX)单元测试用例的时候,发现在 联想拯救者电竞手机Pro(128GB) 系统版本 12.5.225上,如果测试用例申请了通讯录写入权限,如下:
1 |
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> |
那么测试用例执行的时候,会报告如下错误:
1 |
Test running failed:Process crashed. |
详细的logcat日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
D/Zygote: Forked child process 10664 I/ActivityManager: Start proc 10664:xx.test/u0a274 for added application xx.test I/AutoRun: AutoRunServices: ProcessStateHandler: sendKillAppRequest: [uid = 10274, pid = 10664, package name = xx.test mOtherPids = null, mStartupTime = 0, mDelay = 0, mCount = 0] I/adbd: jdwp connection from 10664 D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar I/yboard.biz.tes: The ClassLoaderContext is a special shared library. D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar I/Perf: Connecting to perf service. D/NetworkSecurityConfig: No Network Security Config specified, using platform default D/NetworkSecurityConfig: No Network Security Config specified, using platform default I/MonitoringInstr: Instrumentation started! I/AndroidJUnitRunner: Waiting for debugger to connect... I/System.out: Sending WAIT chunk I/ActivityManager: Killing 10664:xx.test/u0a274 (adj 0): permissions revoked I/Zygote: Process 10664 exited due to signal 9 (Killed) I/libprocessgroup: Successfully killed process cgroup uid 10274 pid 10664 in 44ms |
目前测试发现,即使通过GrantPermissionRule授予权限,也是没用的,代码根本来不及执行,应用就一下被系统杀死了。
目前只能是不测试这部分功能。
1 2 |
@Rule public final GrantPermissionRule rule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_CONTACTS); |
在OPPO/VIVO/小米等手机上,需要手工开启 后台弹出界面权限 才能执行单元测试。有个思路是通过UI Automator来自动开启这个权限,暂时还需要测试一下。
参考链接
- No Network Security Config specified, using platform default
- No Network Security Config specified, using platform default - Android Log
- Android revoke permission at start of each test
- Permissions updates in Android 11
- Android revoke permission at start of each test
- Android Marshmallow: Test permissions with Espresso?
- AndroidTestMockPermissionUtils
- Android 权限适配 从此第三方系统新增的权限无法判断状态的问题得到解决! 如MIUI自启动, 后台弹出界面权限等
- 实战|Android后台启动Activity实践之路
- Espresso和UIAutomator - 完美的结合
- UI Automator