Unable to connect to Android device from Jenkins
Question: So I used to call a MonkeyRunner script from within Jenkins to connect to a
USB Android device and run some automated tests, however MonkeyRunner itself
was rather unstable so I switched to the fantastic AndroidViewClient and
ported my test script over to use this pure Python API.

The new Python script works fine when invoked independently from the shell
however the goal is to call this script from Jenkins as a post build step.

The problem I have is the initial connection to the USB Android device. Script
below is a snippet from the actual test script - this is the part that tests
if a USB device is present and obtains it's serial number before connecting to
the Android device. This works fine from the ubuntu shell however fails to
connect when invoked from a Jenkins 'Execute Shell'.

    
    
    #! /usr/bin/env python
    
    # Import Class Files
    
    import re
    import sys
    import os
    import time
    import commands
    import signal
    import subprocess
    import codecs
    
    ubuntuHome = os.getenv('HOME')
    sdkRootDefault = ubuntuHome + '/dev_env/ADT/sdk'
    sdkRoot = os.getenv('ANDROID_SDK_ROOT',sdkRootDefault)
    platformTools = sdkRoot + '/platform-tools'
    
    # Find the attached devices
    adbcmd = platformTools + "/./adb devices |grep -v attached |grep device |head -n 1 | cut  -f1"
    p = subprocess.Popen(adbcmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    serialnoIn = (p.stdout.readline()).strip()
    
    # No devices found then exit
    if len(serialnoIn) == 0 or serialnoIn is None:
        print ("ERROR: No devices found")
        sys.exit(1)
    
    print "INFO: Trying Connection to " + serialnoIn + "..." 
    

From Ubuntu Shell the output is:

> INFO: Trying Connection to 3a005473...

From Jenkins 'Execute Shell' build step

> ERROR: No devices found

Interestingly when this same snippet was called from within the original
MonkeyRunner script it worked fine. So I am thinking MonkeyRunner does some
initial setup my Python script isn't doing? I am not enough of a Python expert
to know how to configure USB devices. Any help is appreciated.


Answer: Okay the answer was simple really - it helps if jenkins can make use of the
sdk tools and AndroidViewClient paths!

> sudo -Rm u:jenkins:rwx

...fixed it

