2

我正在尝试在已通过 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但它没有打开我的应用程序,但它似乎在当前应用程序上运行命令。有任何想法吗?

4

3 回答 3

6

您应该将 Activity 指定runComponent

runComponent = package + "/" + activity

要获取可启动活动的名称:

$ aapt dump badging <name>.apk | grep launchable-activity
于 2013-03-22T20:37:54.537 回答
5

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

从 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>

然后寻找launchable-activity: name='SomeActivityName'

希望能帮助其他人寻找同样的东西。

于 2013-10-10T15:47:19.397 回答
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/

于 2014-02-20T11:13:25.990 回答