2013-03-22 85 views
2

我試圖在已經通過monkeyrunner安裝的應用程序上運行一些命令。我已經編輯在d.android.com列出的示例代碼和我改成了這樣:如何在已安裝的.apk上運行monkeyrunner

# 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但它不會打開我的應用程序,但它似乎在當前應用程序上運行命令。有任何想法嗎?

回答

6

應指定在runComponent活動作爲

runComponent = package + "/" + activity 

要獲得可發射活動的名稱:

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

如果我只知道包,而不是活動? – EGHDK 2013-03-22 20:44:54

+0

例如,我可以通過從play.google.com中的URL中獲取軟件包名稱來查找手機上另一個應用程序的軟件包名稱。 – EGHDK 2013-03-22 20:46:34

+1

也許你可以檢查logcat消息,試圖打開你的應用程序時顯示的內容。檢查日誌類似「顯示」..我已經看到設置應用程序和一些其他應用程序的情況下的活動名稱。 – Rilwan 2013-03-23 10:42:50

3

我能夠從一個所有已安裝的APK得到發射活動使用此方法播放商店:

get launchable activity name of package from adb

adb shell pm list packages -f 

然後你就可以使用adb拉:

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

例如:(ADB拉/system/app/MyAPK.apk C:\ someLocation)

,然後AAPT得到你的信息想(AAPT目前位於〜\ SDK \構建工具\ Android的4.3):

aapt dump badging <FromYourDesiredLocation\pulledfile.apk> 

然後尋找啓動的活動:名稱= 'SomeActivityName'

希望能幫助別人尋找相同的東西。

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/

相關問題