如何在已安装的.apk上运行monkeyrunner

时间:2013-03-22 20:28:45

标签: android monkeyrunner

我正在尝试在已经通过monkeyrunner安装的应用程序上运行一些命令。我已经编辑了d.android.com上列出的示例代码,并将其更改为:

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')

# sets a variable with the package's internal name
package = 'com.example.myTestApp'

# sets a variable with the name of an Activity in the package
# activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')

正如您所看到的,我将代码更改为(希望)打开com.example.myTestApp但它不会打开我的应用程序,但它似乎在当前应用程序上运行命令。有什么想法吗?

3 个答案:

答案 0 :(得分:6)

您应该将runComponent中的活动指定为

runComponent = package + "/" + activity

获取可启动活动的名称:

$ aapt dump badging <name>.apk | grep launchable-activity

答案 1 :(得分:4)

我能够使用此方法从Play商店中的任何已安装的apk获取启动活动:

get launchable activity name of package from adb

adb shell pm list packages -f

然后你可以使用adb pull:

adb pull <APK path from previous command> <toYourDesiredLocation>

例如:(adb pull /system/app/MyAPK.apk C:\ someLocation)

然后aapt获取你想要的信息(aapt目前位于〜\ sdk \ build-tools \ android-4.3):

aapt dump badging <FromYourDesiredLocation\pulledfile.apk>

然后查找可启动活动:name ='SomeActivityName'

希望能帮助其他人寻找同样的事情。

答案 2 :(得分:1)

首先检查您的应用是否已安装。

apk_path = device.shell('pm path com.xx.yy')
if apk_path.startswith('package:'):
    print "XXXYY already installed."
else:
    print "XXXYY app is not installed, installing APKs..."
    device.installPackage('D:/path to apk/yourapp.apk')

参考http://antoine-merle.com/introduction-to-the-monkey-runner-tool-2/