Appium 『已解决』Activity used to start app doesn't exist or cannot be launched

和曦 · 2015年09月09日 · 最后由 陈恒捷 回复于 2015年09月10日 · 2963 次阅读

解决方法:
用拼接方法指定 APP_PATH,如下:

APP_PATH = File.join(File.dirname(__FILE__),'..','SuerApp9_6.apk')

昨天在写一个 app 自动化脚本时,运到如上问题。
论坛中之前有相关帖子,但是没有解决办法。参https://testerhome.com/topics/1834
我自己也去 StackOverflow 查找,试过将 APP_PATH 设置成绝对路径,请开发将包名更改成 com.changhong.supperapp 后将 appActivity 设置成'.activity.SplashActivity',均未解决。
目前我怀疑是不是因为启动页是一个 Splash 引起的问题,纯属猜测哈。

测试环境:
Win7_32bit
appium_server:1.4.10
appium_lib:7.0.0
jdk:1.8.25

手动执行,通过 Monitor 工具抓取的 Activity 日志:

09-08 21:25:31.482: I/ActivityManager(679): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.changhong.ssc.cookbook cmp=com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity} from pid 32669
09-08 21:25:31.497: W/ActivityManager(679): [LTK] pid, app, 3d=32669, 0, 199913472
09-08 21:25:31.543: I/ActivityManager(679): Start proc com.changhong.ssc.cookbook for activity com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity: pid=499 uid=10083 gids={50083, 3002, 3003, 1015, 1006, 1028}
09-08 21:25:32.430: I/ActivityManager(679): [AppLaunch] Displayed Displayed com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity: +906ms
09-08 21:25:35.204: I/ActivityManager(679): START u0 {cmp=com.changhong.ssc.cookbook/com.changhong.superapp.activity.main.MainActivity (has extras)} from pid 499
09-08 21:25:35.207: W/ActivityManager(679): [LTK] pid, app, 3d=499, 0, 220106752
09-08 21:25:36.414: I/ActivityManager(679): [AppLaunch] Displayed Displayed com.changhong.ssc.cookbook/com.changhong.superapp.activity.main.MainActivity: +1s200ms

Ruby 部分代码如下:

APP_PATH = '../SuerApp9_6.apk'
def desired_caps
  {
    caps: {
      app:                APP_PATH,
      appPackage:         'com.changhong.ssc.cookbook',
      appActivity:        'com.changhong.superapp.activity.SplashActivity',
      appWaitActivity:    'com.changhong.superapp.activity.main.MainActivity', #这个是尝试无果后增加的
      platformName:       'android',
      platformVersion:    '4.4.4',#API:19
      deviceName:         '274b3f06',#MI 3真机
      unicodeKeyboard:    'true',
      resetKeyboard:      'true',
      noSign:             'true', #Skip checking and signing of app with debug keys
      noReset:            'false'
     }
}

Appium-Server 报错的 log 如下:

info: [debug] Device is at API Level 19
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity"
info: [debug] We tried to start an activity that doesn't exist, retrying with . prepended to activity
info: [debug] Getting device API level
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.changhong.ssc.cookbook/.com.changhong.superapp.activity.SplashActivity"
error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
~~~中间log略去
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
info: [debug] Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
    at [object Object].<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules\appium-adb\lib\adb.js:1146:21)
    at [object Object].<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules\appium-adb\lib\adb.js:180:9)
    at ChildProcess.exithandler (child_process.js:656:7)
    at ChildProcess.emit (events.js:98:17)
    at maybeClose (child_process.js:766:16)
    at Process.ChildProcess._handle.onexit (child_process.js:833:5)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)","origValue":"Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity"},"sessionId":null}
info: <-- POST /wd/hub/session 500 30130.327 ms - 346
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 15 条回复 时间 点赞

看起来是有点奇怪,我随便猜猜 appActivity 和 appWaitActivity,都改成 Splash 或者 Main 试试
更方便的尝试方法是直接自己执行 am start 命令,看能不能启动

#1 楼 @eurekasaber
谢谢你的回复。
我也尝试过从 cmd 中直接用 adb 启动,但是报以下错误:

C:\Users\Administrator>adb -s 274b3f06 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity"
Stopping: com.chonghong.ssc.cookbook
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity }
Error type 3
Error: Activity class {com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity} does not exist.

C:\Users\Administrator>adb -s 274b3f06 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.main.MainActivity"
Stopping: com.chonghong.ssc.cookbook
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.main.MainActivity }
Error type 3
Error: Activity class {com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.main.MainActivity} does not exist.
C:\Users\Administrator>adb -s 274b3f06 shell am start -a com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity
Starting: Intent { act=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity }
Error: Activity not started, unable to resolve Intent { act=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity flg=0x10000000 }

C:\Users\Administrator>adb -s 274b3f06 shell am start -a com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.mian.MainActivity
Starting: Intent { act=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.mian.MainActivity }
Error: Activity not started, unable to resolve Intent { act=com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.mian.MainActivity flg=0x10000000 }
com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity

VS

com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.main.MainActivity

建议用 aapt 查看 launchable-activity:

aapt d badging /path/to/your.apk

#3 楼 @watman
什么意思?
appActivity 和 appWaitActivity 两个交换吗?
试过了,错误依旧:(

#4 楼 @laiyuncong8404
我是说从你启动时候的 log 看应该是 SplashActivity, 而不是 MainActivity。
建议你用 aapt 看看 apk 的启动 activity,搜索 launchable-activity:
aapt d badging /path/to/your.apk
或者看看 AndroidManifest.xml 里面的启动 activity,并看看哪些是 exported 能够直接启动的
aapt d xmltree /path/to/your.apk AndroidManifest.xml

#5 楼 @watman
谢谢!
试了以后,似乎证明启动的 Activity 就是 com.changhong.superapp.activity.SplashActivity 无误,
但是脚本运行死活不行啊。

c:\>aapt d badging C:\Appium_Script\SuperApp\SuerApp9_6.apk
package: name='com.changhong.ssc.cookbook' versionCode='3' versionName='V1.0.2'
~~中间略过~~
application: label='SuperApp' icon='res/drawable-mdpi/ic_launcher.png'
launchable-activity: name='com.changhong.superapp.activity.SplashActivity'  label='SuperApp' icon=''

c:\>aapt d xmltree C:\Appium_Script\SuperApp\SuerApp9_6.apk AndroidManifest.xml
    A: package="com.changhong.ssc.cookbook" (Raw: "com.changhong.ssc.cookbook")
~~中间略过~~
      E: activity (line=63)
        A: android:label(0x01010001)=@0x7f070000
        A: android:name(0x01010003)="com.changhong.superapp.activity.SplashActivity" (Raw: "com.changhong.superapp.activity.SplashActivity")
~~中间略过~~        
      E: activity (line=73)
        A: android:label(0x01010001)=@0x7f070000
        A: android:name(0x01010003)="com.changhong.superapp.activity.main.MainActivity" (Raw: "com.changhong.superapp.activity.main.MainActivity")
~~以下略过~~

另外,我 Monkey 测试时,可以启动,
真的有点邪门了。

:Monkey: seed=12867 count=20000
:AllowPackage: com.changhong.ssc.cookbook
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Selecting main activities from category android.intent.category.LAUNCHER
//   - NOT USING main activity com.miui.barcodescanner.activity.CaptureActivity (from package com.miui.barcodescanner)
~~中间略过~~       

//   + Using main activity com.changhong.superapp.activity.SplashActivity (from package com.changhong.ssc.cookbook)
// Selecting main activities from category android.intent.category.MONKEY

~~中间略过~~     

:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivity } in package com.changhong.ssc.cookbook

Sleeping for 1000 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(2.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,2.0)
~~以下略过~~

到现在我已经感觉到没什么能力可以帮你解决问题了。不过如果你愿意试一试,你可以将 appium 的配置里面去掉 “appWaitActivity: 'com.chonghong.superapp.activity.main.MainActivity', # 这个是尝试无果后增加的”。
因为我看到你的 appium-log 显示启动的是 MainActivity 而不是 SplashActivity。我看到你的 am 启动也启动不了,实在是不敢再多作论断,但是你的最后一句应该是-n 而非-a “C:\Users\Administrator>adb -s 274b3f06 shell am start -a com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity”。如果你愿意试一试,可以改成如下命令:

adb -s 274b3f06 shell am start -n com.chonghong.ssc.cookbook/com.chonghong.superapp.activity.SplashActivity

#7 楼 @watman
试过你给的命令,还是报错。
appium-log 中是我贴错,因为 SplashActivity 和 MainActivity 我交替尝试过,现已更正。
虽然问题还没有解决,但是非常感谢您。
给个 app 地址:http://pan.baidu.com/s/1c0Gy9PY

安装了你的 apk 我能够 am 启动,不过我的模拟器不支持 so,启动就 FC 了;装到真机上启动也没任何问题

C:\Users\Administrator\Desktop>%adb% shell am start -n com.changhong.ssc.cookbook/com.changhon
g.superapp.activity.SplashActivity
open: Permission denied
open: Permission denied
Starting: Intent { cmp=com.changhong.ssc.cookbook/com.changhong.superapp.activity.SplashActivi
ty }

会不会是因为你的电脑上面连接了多台设备,而你的目标 “274b3f06” 机器上面并没有安装你的 apk 呢?

和曦 #11 · 2015年09月09日 Author

#10 楼 @watman
只连接了一台。
刚刚我试了下,将 caps 中的 APP_PATH 用拼接方法,结果居然成功了。
之前我试过写绝对路径,比如''c:\superapp\SuerApp9_6.apk'',不行。

APP_PATH = File.join(File.dirname(__FILE__),'..','SuerApp9_6.apk')

再次感谢您的帮助,谢谢!👍 👏

和曦 #12 · 2015年09月09日 Author

#11 楼 @laiyuncong8404
是两个\,自己转义了。

能否把解决方案附在正文末尾并醒目标出?这样也方便后面的人出现类似问题后快速找到解决方案。

和曦 #14 · 2015年09月10日 Author

#13 楼 @chenhengjie123
已在正文开头粗体标出,🌀

#14 楼 @laiyuncong8404 :plus1:

不过好奇怪啊,你不是试过绝对路径不行吗?怎么现在就可以了?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册