Android 如何在已安装的.apk上运行monkeyrunner
我试图在已经通过monkeyrunner安装的应用程序上运行一些命令。我编辑了d.android.com上列出的示例代码,并将其更改为:Android 如何在已安装的.apk上运行monkeyrunner,android,monkeyrunner,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 o
# 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')
如您所见,我将代码更改为(希望是)open
com.example.myTestApp
,但它没有打开我的应用程序,但它似乎在当前应用程序上运行命令。有什么想法吗?您应该在runComponent
中将活动指定为
runComponent = package + "/" + activity
要获取可启动活动的名称,请执行以下操作:
$ aapt dump badging <name>.apk | grep launchable-activity
$aapt dump-bading.apk | grep可启动活动
我可以使用以下方法从play store中的任何已安装apk获取启动活动:
然后可以使用adb pull:
adb pull <APK path from previous command> <toYourDesiredLocation>
adb拉力
例如:(adb pull/system/app/MyAPK.apk C:\someLocation)
然后使用aapt获取所需信息(aapt当前位于~\sdk\build tools\android-4.3中):
aapt转储标志
然后查找可启动的活动:name='SomeActivityName'
希望这能帮助其他人寻找同样的东西。首先检查您的应用程序是否已安装
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')
请参阅如果我只知道软件包而不知道活动,该怎么办?例如,我可以通过从play.google.com中的URL抓取软件包名称,在手机上找到另一个应用程序的软件包名称。您可以在尝试打开应用程序时查看logcat消息中显示的内容。查看类似“显示”的日志…我在设置应用程序和其他一些应用程序中看到了活动名称。
aapt dump badging <FromYourDesiredLocation\pulledfile.apk>
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')