I'm using python(v3.6.0) and tkinter is working fine.
Now I'm trying to add tkinter to my python script to work with android monkeyrunner tool. When I try to execute execute below command:
C:\Python36\myScript>monkeyrunner C:\Python36\myScript\installApp.py
command, then the following error message is appeared:
[main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
[main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\Python36\myScript\installApp.py", line 2, in <module>
import tkinter as tk
ImportError: No module named tkinter
I have already added my Python library file path C:\Python36\Lib, in my windows environment variable, but still ImportError: No module name tkinter error occured.
The problem is that monkeyrunner is based on jython. You can use AndroidViewClient/culebra which is almost a drop-in replacement, based on python and also uses Tkinter for the UI (see culebra).
Related
I've found a nice plugin for lldb that allows watching memory regions as images: https://github.com/carlodalmutto/ImageWatchLLDB
As I can see, this plugin was developed for Xcode and not for Android.
However, it is written in Python and therefore should be cross-platform.
I've found a directory c:\Users\username\.lldb\ on my hard drive. Its size is about 1 Gb.
I've created following files: c:\Users\username\.lldbinit, c:\Users\username\lldbinit, c:\Users\username\.lldb\lldbinit, c:\Users\username\.lldb\.lldbinit, c:\Users\username\.lldb\init
All of them have the similar content:
script print "script print Hi from lldb"
echo "echo Hi"
Unfortunately I don't see any output.
I can open LLDB console and issue these commands:
(lldb) script print "script print Hi from lldb"
script print Hi from lldb
(lldb) echo "Hi"
error: 'echo' is not a valid command.
error: Unrecognized command 'echo'.
This means that LLDB understands script print command.
Installation directory for Android Studio contains the subdirectory bin\lldb containing python scripts with pretty printers (file jstring_reader.py, gdb\printing.py, etc)
So, it seems possible to extend LLDB from recent NDK.
The question is: how can I add my custom plugins?
UPDATE. After some hacking I've found that LLDB commands, starting with the "script" string, are actually python commands:
(lldb) script print sys.executable
C:\Android\sdk\lldb\3.1\bin\LLDBFrontend.exe
(lldb) script print sys.version
2.7.10 (default, Feb 24 2016, 14:25:13) [MSC v.1900 64 bit (AMD64)]
(lldb) script print sys.path
['C:/Android/sdk/lldb/3.1/bin', 'C:/Android/Android Studio/bin/lldb/shared/jobject_printers', 'C:/Android/sdk/lldb/3.1/lib/site-packages', 'C:\\Android\\sdk\\lldb\\3.1\\bin\\python27.zip', 'C:\\Android\\sdk\\lldb\\3.1\\DLLs', 'C:\\Android\\sdk\\lldb\\3.1\\lib', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\plat-win', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\lib-tk', 'C:\\Android\\sdk\\lldb\\3.1\\bin', 'C:\\Android\\sdk\\lldb\\3.1', 'C:\\Android\\sdk\\lldb\\3.1\\lib\\site-packages', '.', 'C:\\Android\\Android Studio\\bin\\lldb\\shared\\stl_printers', 'C:\\Android\\sdk\\ndk-bundle\\prebuilt\\windows-x86_64\\share\\pretty-printers\\libstdcxx\\gcc-4.9']
(lldb) script import pip
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named pip
The first idea of installing missing modules with pip has failed.
I update my question: Are there any official guide on writing python plugins for lldb from NDK?
UPDATE 2. Have tried creating Anaconda virtual environment with the same python version and adding it to sys.path. Also failed.
(lldb) script os.environ['PATH']+= r'C:\Anaconda\envs\ndkpy;'
(lldb) script os.environ['PATH']+= r'C:\Anaconda\envs\ndkpy\Library\bin;'
(lldb) script import numpy as np
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda\envs\ndkpy\Lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: A dynamic link library (DLL) initialization routine failed.
UPDATE 3. Have found C:\Android\Sdk\lldb\3.1\lib\lib-tk. Tried importing Tkinter, but still no luck:
(lldb) script import Tkinter
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Android\sdk\lldb\3.1\lib\lib-tk\Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
Scheme1:
Just run command script import C:\\lldb_plugins\\chisel\\fbchisellldb.py or command script import C:\\lldb_plugins\\LLDB\\lldb_commands\\dslldb.py on your lldb console in Android Studio.
Scheme2:
Create file C:\Users\username\.lldbinit and put the following content to this file:
command script import C:\\lldb_plugins\\chisel\\fbchisellldb.py
command script import C:\\lldb_plugins\\LLDB\\lldb_commands\\dslldb.py
And then, run reload_lldbinit on your lldb console in Android Studio.
I want to run some apps on android with QPython3. So I tried but got permission error.
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
import subprocess
yt = subprocess.Popen('/system/app/YouTube/YouTube.apk')
yt.wait()
print("done")
The result is following :
(snip)
"..../subprocess.py" line 1356, in _execute_child
OSError : [Errno 13] Permissio denied
Help me please...
I'm pretty sure you cannot execute an .apk directly. You would use subprocess to run Unix commands like ls or other "real" ELF executables.
I have generated the template using command $ culebra -U -o mytest.py and using for com.csr.csrmeshdemo:id/powerSwitch to turn on and off the switch.
It worked fine for first time but after restarting system for another issue I am unable to run my python script. getting error like this:
*root#parameshwar:~/Desktop/monkey/adt-bundle-linux-x86_64-20131030/sdk/CSR_script# adb devices
List of devices attached
T00940ZEIM device
root#parameshwar:~/Desktop/monkey/adt-bundle-linux-x86_64-20131030/sdk/CSR_script# python t6.py
E
======================================================================
ERROR: testSomething (__main__.CulebraTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "t6.py", line 44, in testSomething
self.vc.dump(window='-1')
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 3270, in dump
self.setViewsFromUiAutomatorDump(received)
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 2927, in setViewsFromUiAutomatorDump
self.__parseTreeFromUiAutomatorDump(received)
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 3109, in __parseTreeFromUiAutomatorDump
raise ValueError("received does not contain valid XML data")
ValueError: received does not contain valid XML data
----------------------------------------------------------------------
Ran 1 test in 2.508s
FAILED (errors=1)*
Please help me with this..
Latest AndroidViewClient/culebra 11.5.7 adds more verbose information about the exception and would let you trace the root cause easily.
I finally managed to successfully build a Nexus 10 image (full_manta-userdebug) with Google Mobile Services installed but unfortunately, I'm unable to use my MonkeyRunner script that uses AndroidViewClient. It crashes out of the script with the following:
/system/bin/sh: uiautomator: not found
130628 14:12:41.242:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130628 14:12:41.242:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "/home/allen/projects/cts-scripts/4.2/cts-setup.py", line 361, in <module>
main()
File "/home/allen/projects/cts-scripts/4.2/cts-setup.py", line 339, in main
vc = ViewClient(device, serialno)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1188, in __init__
self.dump()
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1780, in dump
self.setViewsFromUiAutomatorDump(received)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1530, in setViewsFromUiAutomatorDump
self.__parseTreeFromUiAutomatorDump(received)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1688, in _ViewClient__parseTreeFromUiAutomatorDump
self.root = parser.Parse(receivedXml)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 988, in Parse
parserStatus = parser.Parse(uiautomatorxml, 1)
File "/home/allen/android/android-sdks/tools/lib/jython-standalone-2.5.4-rc1.jar/Lib/xml/parsers/expat.py", line 212, in Parse
xml.parsers.expat.ExpatError: Content is not allowed in prolog.
Do I need to install the ViewServer for this to work or did I forget to include the uiautomator backend when I built the image?
Try uiautomator from command line to find out if it's there or not:
$ adb shell uiautomator
if your output is similar to
Usage: uiautomator <subcommand> [options]
Available subcommands:
help: displays help message
...
then you have uiautomator installed. Otherwise if the output is
/system/bin/sh: uiautomator: not found
then you forgot to include it in the image.
You don't need ViewServer if UiAutomator is used as AndroidViewClient back-end.
I have an app, for which i wanna run Monkeyrunner (using Android ViewClient)
I am trying to define my view as
Vc = ViewClient(device, serialno)
vc.dump
touchProject = vc.findViewByIdorRaise('id/projectNewGallery')
touchProject.touch()
But I am getting error : ->"com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='id/projectNewGallery' in tree with root=ROOT"
How can i set root ? like this ?
touchProject = vc.findViewByIdorRaise('id/projectNewGallery','id/projectMain')
?
///////////////////////////////////////////////////////////////////////////////////
I am trying to use culebra tool, But I am getting following error.
$ java -jar androidviewclient-2.3.16.jar culebra
ERROR: monkeyrunner was not found and Windows 7 does not support shebang in scripts. Aborting.
I also tried this way (Non-Shebang OS -> Windows 7 )
$ /cygdrive/c/android-sdk/tools/monkeyrunner.bat -plugin /cygdrive/c/Android_Resources_Hassan/MonkeyRunner_KIneMaster/AndroidViewClient-master/AndroidViewClient/bin/androidviewclient-2.3.22.jar culebra myscript.py
Plugin file doesn't exist
Usage: monkeyrunner [options] SCRIPT_FILE
-s MonkeyServer IP Address.
-p MonkeyServer TCP Port.
-v MonkeyServer Logging level (ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF)
Ok i was able to run it Windows CMD :
C:\android-sdk\tools>monkeyrunner -plugin C:\Android_Resources_Hassan\MonkeyRunner_KIneMaster\androidviewclient-2.3.24.jar test3_py.py
but I got following error again :
130619 14:41:15.725:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130619 14:41:15.725:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\android-sdk\tools\test3_py.py", line 71, in <module>
touchProject = vc.findViewByIdOrRaise('id/projectNewGallery')
File "C:\Android_Resources_Hassan\MonkeyRunner_KIneMaster\AndroidViewClient- master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1919, in findViewById
OrRaise raise ViewNotFoundException("ID", viewId, root)
com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='id/projectNewGallery' in tree with root=ROOT
my script file --------------------
#! /usr/bin/env monkeyrunner
'''
Copyright (C) 2012 Diego Torres Milano
Created on Feb 3, 2012
#author: diego
'''
import re
import sys
import os
# this must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails
try:
ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
except KeyError:
print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" % __file__
sys.exit(1)
sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src')
from com.dtmilano.android.viewclient import ViewClient
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
#starting script
print "start"
# Connects to the current device, returning a MonkeyDevice object
device, serialno = ViewClient.connectToDeviceOrExit()
#device connected
print "connection started"
# sets a variable with the package's internal name
package = 'com.example.app.myApp'
# sets a variable with the name of an Activity in the package
activity = 'com.example.mainapp.MainActivity'
# sets the name of the component to start
runComponent = package + '/' + activity
# Runs the component
device.startActivity(component=runComponent)
#device set to sleep for 4 seconds
print "wait for 4 seconds"
# first screen shot event
MonkeyRunner.sleep(4);
vc = ViewClient(device, serialno)
vc.dump()
root = vc.getRoot()
#touchProject = vc.findViewWithTextOrRaise('', root)
touchProject = vc.findViewByIdOrRaise('projectNewGallery')
touchProject.touch()
# wait for screenshot to save
MonkeyRunner.sleep(2);
# Takes a screenshot
result1 = device.takeSnapshot()
result1.writeToFile('/myPath/shot1.png','png')
# wait for 3 seconds
MonkeyRunner.sleep(3);
is ID same as defined in XML android:id="+id/newProjectGallery" ?
////////////////////// AFTER adding vc.traverse() in code///////////////////
I am getting following error
130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\android-sdk\tools\test.py", line 71, in <module>
ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
File "C:\Android_Resources_Hassan\MonkeyRunner\AndroidViewClient- master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1687, in traverse
print >>stream, "%s%s" % (indent, s)
LookupError: unknown encoding 'ms949'
Moreover I ran dump-simple.py code also. But I am getting again the same error :
130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\android-sdk\tools\dump-simple.py", line 30, in <module>
ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
File "C:\Android_Resources_Hassan\MonkeyRunner\AndroidViewClient- master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1687, in traverse
print >>stream, "%s%s" % (indent, s)
LookupError: unknown encoding 'ms949'
what is causing it ?
Please help me ~
Thanks
I see a couple of errors in your snippet (check this corrected version):
vc = ViewClient(device, serialno)
vc.dump()
touchProject = vc.findViewByIdOrRaise('id/projectNewGallery')
touchProject.touch()
to simplify the process, you can just use culebra to generate the correct script template that you can, later on, adapt to your needs:
When the device screen contains the desired Views:
$ culebra -VC -o myscript.py
edit myscript.py to add the call to the touch() method and run
$ myscript.py
The use of verbose comments (-C) simplifies the identification of Views if IDs are not available.
Update
You were almost there in your first example, I guess the problem should be fixed by my corrected snippet before. The code run, the only problem may be you are expecting IDs that are not there.
Remember, there are no IDs if the back-end is UiAutomator, which is the default back-end for API >= 16.
The java runner expects monkeyrunner to be in the PATH.
It seems /cygdrive/c/Android_Resources_Hassan/MonkeyRunner_KIneMaster/AndroidViewClient-master/AndroidViewClient/bin/androidviewclient-2.3.22.jar does not exist. At least this is what monkeyrunner thinks. Perhaps you should use \ in Windows paths.