解决 appium 报错 java.lang.SecurityException - Permission Denial- starting Intent 的问题

1. 问题描述

在使用 appium 时,出现以下问题:

1
2
3
4
5
6
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.dreamfish.com.autocalc/.MainActivity } from null (pid=9251, uid=2000) not exported from uid 10053
at com.android.server.am.ActivityStackSupervisor.checkStartAnyActivityPermission(ActivityStackSupervisor.java:1779)
at com.android.server.am.ActivityStarter.startActivity(ActivityStarter.java:725)
at com.android.server.am.ActivityStarter.startActivity(ActivityStarter.java:551)
at com.android.server.am.ActivityStarter.startActivityMayWait(ActivityStarter.java:1156)
at com.android.server.am.ActivityStarter.execute(ActivityStarter.java:490)

2. 问题原因

出现这样的错误一定是因为当前的 appActivity 设置的不是 app 首次进入的 appActivity.

3. 解决方法

(1)首先获取该 App 的包名

1
2
3
➜  ~ adb shell dumpsys window | grep mFocusedApp
mFocusedApp=Token{2e61d6 ActivityRecord{a39b1f1 u0 com.dreamfish.com.autocalc/.MainActivity t32}}
mFocusedApp=AppWindowToken{7130c57 token=Token{2e61d6 ActivityRecord{a39b1f1 u0 com.dreamfish.com.autocalc/.MainActivity t32}}}

(2)通过 monkey 命令找到 cmp= 后面的 activity,本列中的 activity.LauncherActivity

1
2
3
4
5
6
7
8
➜  ~ adb shell monkey -p com.dreamfish.com.autocalc -v 1 | grep cmp=
args: [-p, com.dreamfish.com.autocalc, -v, 1]
arg: "-p"
arg: "com.dreamfish.com.autocalc"
arg: "-v"
arg: "1"
data="com.dreamfish.com.autocalc"
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.dreamfish.com.autocalc/.LauncherActivity } in package com.dreamfish.com.autocalc

4. 相关参考

代码参考于以下博客,自己做了相关调整:

https://blog.csdn.net/For_if_while/article/details/122946460