Android MonkeyRunner exception - android

I'm trying to run MonkeyRunner Android test tool from Eclipse (4.2).
I installed Eclipse PyDev plugin, as well as Jython 2.5.1.
My PyDev interpreter is set to Jython / 2.5.
I created a Jython project in Eclipse with a simple jython file:
# 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()
Now when I launch it from Eclipse (project > run as JythonProject), I'me getting the following exception:
Traceback (most recent call last):
File "C:\svnrepository\trunk\JythonProject\src\main.py", line 10, in <module>
device = MonkeyRunner.waitForConnection()
at com.android.monkeyrunner.MonkeyRunner.waitForConnection(MonkeyRunner.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
Any idea what can be wrong?

There is a post of Diego Torres Milano how to configure PyDev to use it with monkeyrunner. I followed his steps and in my case everything works fine.
In brief, at first you need to change your Android installation. You need to rename your monkeyrunner tool to something monkeyrunner-original. Then you need to write in the same folder shell script (name it monkeyrunner) that will invoke monkeyrunner-original in the specific manner.
Script for Linux:
# /bin/bash
if [ "$1" = '-u' ]
then
shift
fi
exec /opt/android-sdk-linux_86/tools/monkeyrunner-original "$#"
Script for Windows:
REM
#echo off if("%1")==("-u") shift "C:\Program Files\Android\android-sdk\tools"/monkeyrunner-original %1 %2 %3 %4 %5 %6 %7 %8
Then in Eclipse, you should define new Python interpreter (not Jython) and point it to the created script.

Related

Native Code coverage with android soong build system

I'm trying to generate code coverage report for my native components with AOSP source code using soong build system.
I have extended aosp vhal but unit test cases are same as in below link.
http://androidxref.com/8.1.0_r33/xref/hardware/interfaces/automotive/vehicle/2.0/default/tests/
Tried adding below to cc_test, cc_binary in Android.bp
native_coverage : true,
cflags: [
"-g",
"-O0",
"-fprofile-arcs",
"-ftest-coverage",
],
ldflags : [
"-fprofile-arcs",
"-ftest-coverage",
],
Native binary unit-tests-coverage is generated in out/target/product but I can't find gcno intermediates for this.
Running below command gives me *.gcda files for each test files.
adb shell \
GCOV_PREFIX=/data/local/tmp \
GCOV_PREFIX_STRIP=`echo $ANDROID_BUILD_TOP | grep -o / | wc -l` \
/data/local/tmp/unit-tests-coverage
I have tried below links but not sure how to proceed :(
http://logan.tw/posts/2015/04/28/check-code-coverage-with-clang-and-lcov/
https://android.googlesource.com/platform/bionic.git/+/master-soong
https://android.googlesource.com/platform/build/soong/+/581341d%5E%21/
https://android.googlesource.com/platform/external/e2fsprogs/+/fedfb27%5E%21/
https://android.googlesource.com/platform/development/+/master/scripts/acov#23
http://androidxref.com/9.0.0_r3/xref/bionic/README.md#293
I'm not sure if google's vts framework can be used here to generate native code coverage.
https://codelabs.developers.google.com/codelabs/android-vts-8/#6
"gcnodir" is generated but not sure how to make use of it.
/coverage/data/nativetest64/vehicle-unit-tests-coverage/unit-tests-coverage.gcnodir
Posting answer to my question for other users on SO.
Install coverage tool :
sudo apt-get install lcov (This should install lcov-1.12)
sudo apt-get install gcc-4.6 (Clang generates .gcno approximately equal to gcc 4.2 that aren't compatible
with gcov-4.8. Installing gcc-4.6 to get gcov-4.6 and invoking lcov with '--gcov-tool /usr/bin/gcov-4.6')
Download LLVM 3.8 for llvm-cov to work : http://releases.llvm.org/download.html
All native unit test cases i.e instrumented binary needs to be executed on target. To build and emit clang's instrumentation based profiling. Example: http://androidxref.com/9.0.0_r3/xref/hardware/interfaces/automotive/vehicle/2.0/default/Android.bp#82 (Renamed to vehicle-manager-unit-test for shorter name)
export NATIVE_COVERAGE=true
Add native_coverage: true to test module in Android.bp
Go to: module-name/test
Use mm or make command to build native binary
Ex: For hardware/interfaces/automotive/vehicle/2.0/default/tests/ :
mma or make vehicle-manager-unit-test -j32
Copy coverage enabled instrumented binary to target
adb push out/target/product/product_name/data/nativetest64/vendor/vehicle-manager-unit-test /data/nativetest64/vehicle-manager-unit-test
adb shell chmod +x /data/nativetest64/vehicle-manager-unit-test
Run test cases and generate .gcda files
adb shell \
GCOV_PREFIX=/data/local/tmp \
GCOV_PREFIX_STRIP=echo $ANDROID_BUILD_TOP | grep -o / | wc -l \
/data/nativetest64/vehicle-manager-unit-test
adb shell find -iname *.gcda
adb pull /data/local/tmp/proc/self/cwd/out/soong/.intermediates/hardware/interfaces/automotive/vehicle/2.0/default/vehicle-manager-unit-test/android_x86_64_silvermont_vendor_cov/obj/hardware/interfaces/automotive/vehicle/2.0/default/tests/ .(Destination folder)
Extract GCNO files from GCNODIR (archive file generated at
out/overage/data/nativetest64/vendor/vehicle-manager-unit-test ) to
same folder with GCDA files
llvm-cov gcov -f -b *.gcda (https://llvm.org/docs/CommandGuide/llvm-cov.html )
lcov --directory . --base-directory . --gcov-tool /usr/bin/gcov-4.6 --capture -o cov.info (http://ltp.sourceforge.net/coverage/lcov.php)
genhtml cov.info -o output
Here's the script which wraps all these commands:
https://gist.github.com/pankajgangwar/f070b8b54e83543f8e3638dcd2cae1b8
here it is explaned how to generate coverage reports, which do require GTest:
these flags enable the generation of test coverage: -fprofile-arcs -ftest-coverage
then one has to use gcov: gcov main_test.cpp
which's output then can be passed on to lcov (for reference):
$ lcov --coverage --directory . --output-file main_coverage.info
from which one can generate an lcov coverage report in HTML format:
$ genhtml main_coverage.info --output-directory out
these .gcda files in .gcnodir are gcov data files. gcov also has an output option --json-format, which might come handy when wanting to consume the coverage data with a web-service.
one of the examples from the links you've provided can be used to generate it for a whole project:
Collect the code coverage results:
$ lcov --directory . \
--base-directory . \
--gcov-tool gcov.sh \
--capture -o cov.info
Generate HTML files:
$ genhtml cov.info -o output
where the only difference is, that the wrapper script would need to be adjusted to call gcov. probably one could even omit the wrapper passed with option --gcov-tool, since it should be directly called.
since one can only prepare the coverage report by adding the compiler flags, the gcov and lcov commands should be setup as post-build script, so that they would automatically generate the report.

Invalid syntax zipfile(apkAbsPath) in line 202 ninjadroid.py

I cloned ninjadroid from https://github.com/rovellipaolo/NinjaDroid recently and followed the instructions for decompiling apk files. As instructed I have installed android sdk and python, but when I tried running the command in my command prompt (python ninjadroid.py -t myPackage.apk), it throws the below error
File "ninjadroid.py", line 202
with ZipFile(apkAbsPath) as z:
Mmm... that's strange.
Is 'myPackage.apk' an existing file (in your computer)? Is it a real APK package? From the error, I would say you are trying to reverse a non-APK file.
Unfortunately the "is-APK" check in the current, available version is a lazy check - done pretty badly. I completely reimplemented it in the new version I am working on.
UPDATE
With the newest NinjaDroid version (i.e. 2.0):
$ python ninjadroid.py this_is_a_non_existing_file
>> NinjaDroid: [ERROR] The target file (i.e. 'this_is_a_non_existing_file') must be an existing, readable file!
$ touch this_is_an_existing_file_but_not_an_apk_package.txt
$ python ninjadroid.py this_is_an_existing_file_but_not_an_apk_package.txt
>> NinjaDroid: [ERROR] The target file (i.e. 'this_is_an_existing_file_but_not_an_apk_package.txt') must be an APK package!
$ python ninjadroid.py test/data/Example.apk
{
"app_name": "Example",
...
}

How to touch/press a button/View using AndroidViewClient?

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.

android monkey runner scripts

I was trying to run a a sample python program using monkey runner but unfortunately throwing an error of this type :
Can't open specified script file
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)
Exception in thread "main" java.lang.NullPointerException
so any one can guide me how to resolve this and how to use monkey runner to execute this type of things
I've found that making the path to the script absolute helped monkeyrunner.
I am invoking the runner from a Python script where a helper class has a startMonkey method:
def startMonkey(self, monkeyScript):
command = [ self.env + '\\tools\\monkeyrunner.bat' ]
command.append( os.path.abspath( monkeyScript ) )
return startProcess( command )
This seems to be working for me.
Script file should be a full path file name try below monkeyrunner c:\test_script\first.py
So go to the folder \ sdk \ tools
and press shift and right click to open command prompt and type monkeyrunner c:\test_script\python_file_name.py

How to download google source code for android

As you know, there is a list of several hundred projects in https://android.googlesource.com/. I'd like to download them all in windows machine. According to Google's document,
To install, initialize, and configure Repo:
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
To clone the entire platform, install repo, and run:
mkdir mydroid
cd mydroid
repo init -u https://android.googlesource.com/platform/manifest
repo sync
In my machine, however, I cannot "repo init" in Git Bash because it says it does not have python. I have python installed but git bash does not recognize it. Note that I set the python directory to the system path too. If anybody can give a tip, I would appreciate it. Thanks
UPDATE: I believe it's problem with new version of Git Bash for Windows. System path is not applied to Git Bash at all - I could easily test if system path worked with command prompt. Anyway, I tried this instead and it actually ran with error of course.
/c/python27/python.exe ../bin/repo init -u https://android.googlesource.com/platform/manifest
The error message is
$ /c/python27/python.exe ../bin/repo init -u https://android.googlesource.com/platform/manifest
Traceback (most recent call last):
File "../bin/repo", line 91, in <module>
import readline
ImportError: No module named readline
OK. I passed this error by installing pyreadline in windows:
easy_install pyreadline
If you got an error, you must install setuptools from
http://pypi.python.org/pypi/setuptools#files
And finally ran the command again to get this:
$ repo init -u https://android.googlesource.com/platform/manifest
fatal: unable to start d:\mywork\dev\GoogleAndroid\working_dir\.repo\repo/main.py
fatal: [Errno 8] Exec format error
With one click, download the latest code as .tar.gz file, from here
https://android.googlesource.com/platform/frameworks/base/+archive/master.tar.gz, the android could be found under core folder
Edit
Alternative here:
http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/
Just select the version then a download options within.
If you consider, as an example, this other program "sympy" which also needs git bash and python, it is only a matter to add python to your PATH prior to launching the git bash session.
Install Python from:
http://python.org/download/
by downloading the "Python 2.7 Windows installer" (or Python 2.6 or 2.5) and running it.
Add python directory to your system environment path variable
(My Computer -> Advanced -> Environment Variables -> Path -> Edit).
Note that the repo script itself must be in the path, as mentioned in the Version Control page of android:
Repo is a repository management tool that we built on top of Git. Repo unifies the many Git repositories when necessary, does the uploads to our revision control system, and automates parts of the Android development workflow.
Repo is not meant to replace Git, only to make it easier to work with Git in the context of Android.
The repo command is an executable Python script that you can put anywhere in your path.
This answer explains how to fix this error:
fatal: unable to start c:\path\.repo\repo/main.py
fatal: [Errno 8] Exec format error
Summary: I finally used the python packaged by Cygwin.
Details: Below is the full story.
The tip from the repo bug tracking is to add '/c/app/Python27/python ':
line 136 in v1.20
REPO_MAIN = '/c/app/Python27/python ' + S_repo + '/main.py'
line 735 in v1.20 (beginning of function main)
wrapper_path = '/c/app/Python27/python ' + os.path.abspath(__file__)
But we get the error TypeError: coercing to Unicode: need string or buffer, NoneType found
Therefore I reverted these changes above and performed the other changes below (on version 1.20):
line 136, replaced single slash by double back-slash:
REPO_MAIN = S_repo + '\\main.py'
line 766, added python absolute path as first element of me:
me = ['C:\\app\\Python27\\python.exe', repo_main,
'--repo-dir=%s' % rel_repo_dir,
'--wrapper-version=%s' % ver_str,
'--wrapper-path=%s' % wrapper_path,
'--']
line 776, replaced os.execv(repo_main, me) by
os.execv('C:\\app\\Python27\\python.exe', me)
However we get still an error:
$ Traceback (most recent call last):
File "c:\path\.repo\repo\main.py", line 39, in <module>
from subcmds.version import Version
File "c:\path\.repo\repo\subcmds\__init__.py", line 36, in <module>
['%s' % name])
File "c:\path\.repo\repo\subcmds\forall.py", line 17, in <module>
import fcntl
ImportError: No module named fcntl
The Python v2.7 fcntl documentation says fcntl is available for platform Unix only.
I finally reverted again all changes in repo script and installed Cygwin including its python and git packages: it succeeded as a charm.
But, as the symlinks simulated by Cygwin are not recognized by the MSysGit, we have to use the Cygwin git. And GUIs on top of git are not fully compliant with Cygwin git...
(see also my other post)
Edit:
Cygwin can use native NTFS symlinks (just set CYGWIN=winsymlinks:native and be Admin). Therefore MSysGit can be used and any other GUI based on it :-)

Categories

Resources