Category Archives: Android

“Re-imaging” a Device – or flashing a Rom

Had a problem where I was added¬† a phone that someone had tried to install ubuntu (Linux flavor) on it, it failed and they just put it back on the shelf int he lab ūüė¶

Took me a couple tries to figure out how to put a usable version of Android back on the device.

What I went through:

  1. Figure out what model phone I had – had to pop the case and read behind the battery – since didn’t tell me the carrier, fortunately IT had that info
  2. Then I needed to find the right Rom (image) for the device and download the zip file for it to my computer (first image failed)
  3. Then I copied the image to the SDCARD of the phone:
    • adb push bigxie_maguro_JRN84D_v2-signed.zip /sdcard/bigxie_maguro_JRN84D_v2-signed.zip
  4. Then on the phone I “Flash Zip from SD Card”
  5. And choose that zip above, it took  a few minutes to unpack the zip file and boot Рbut it worked.

Command line I also used the commands “adb reboot” and “adb reboot recovery” to keep booting my phone

For the Roms, I found the images here:  http://www.clockworkmod.com/rommanager

For Additional Flashing/imaging instructions:  Flashing Guide Рhttp://forum.xda-developers.com/wiki/index.php?title=Flashing_Guide_-_Android

 

Advertisements

Monkeyrunner links & Other automation info

Some links for Monkeyrunner

From the Android site (though it took tweaking to make this work for me)

monkeyrunner | Android Developers –¬†http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html

 

Additional Documentation from Android

Monkey Device – other program options, like reboot –¬†http://developer.android.com/guide/developing/tools/MonkeyDevice.html
Monkey Image РPixel and Image stuff Р http://developer.android.com/guide/developing/tools/MonkeyImage.html
Monkey Runner – has some static utility methods.¬†–¬†http://developer.android.com/guide/developing/tools/MonkeyRunner.html

 

Some Other UI Automation Ideas

Android UI Automation Basics –¬†http://goravsingal.hubpages.com/hub/Android-UI-Automation-Basics

 

Python

Python/Jython Downloads –¬†http://www.python.org/download/

Alternatives to¬†Eclipse (which isn’t my favorite beast)

I have been using Notepad++ to write my early learning scripts (though this isn’t an IDE)

Netbeans another IDE

 

Other tools

Robotium – (I felt this needed a lot of developer support in order to me to work with it and I am not a Java person) –¬†http://code.google.com/p/robotium/¬†– and it integrates with¬†Cucumber¬†but that requires Ruby

 

Paid tools (e.g not free)

TestDroid –¬†http://testdroid.com/

Updating the Hostfile for testing on the android

For a new test I had to update the hostfile on my droid to point to a new environment, this task turned out be to be tougher than it should be – considering a droid is basically a computer.

The hostfile is protected and in order to be able to modify it you need to be able to “root” the phone ¬†(android version of iPhone’s jailbrake, basically break into the phone to override defaults). ¬†This was easier said than done, I found many¬†references¬†to z4root. ¬†Many of the links didn’t work, or went to a site siezed by the US government, or my local AV software would prevent the download due to it triggering virus alerts. ¬†When I finally got the file, I found the that the OS that came installed default on my phone (Huawei U8500) had been fixed and I couldn’t use z4root.

So in the end I ended up using the¬†Emulator¬†that is installed with the android SDK (note you do not need eclipse for this, I don’t even have eclipse installed). ¬†I ran the¬†AVD Manager, and created a virtual device that matched the specs of my test Android Huawei U8500 (I called the device huawei_u8500). ¬†Then I ran the emulator (launching it command line) with that single device

emulator -wipe-dataavd huawei_u8500

-wipe-date has the emulator start with a clean image.

Once it was running (takes a few minutes to launch, knowing the hot keys to naviagate helps, since I the emulator lacks a back button.

Android Emulator: Keyboard Commands (http://developer.android.com/guide/developing/tools/emulator.html)

Then came updating the host file, one thing you need to do is remount the device image as writable, then pull the file, update it, and copy it back.  Fortunately that is actually easy compared to everything else.

remount the device image as writable

adb remount

Pull the host file from the device

adb pull /system/etc/hosts c:\Android\hosts

You can put the file anywhere and call it anything you want, I called it “hosts” and put it in my android directory

Next open the file in your favorite test editor (I use notepad++, but notepad is fine).  Add in the host file entries you need, for me this was the change:

127.0.0.1     localhost  (this was the original entry)
10.16.1.11  www.newsite.com  (what I added for my test)

Copy the edited hosts file back to your “device”

adb push c:\Android\hosts /system/etc

Now you are done and ready to test, every time in the emulator you go to “www,newsite.com” it will now be directed to 10.16.1.11.

 

 

Monkeyrunner API Reference

Update:  I always get errors when I try and use monkeyrunner.sleep(xx) Рhowever I can import time, and use time.sleep(xx) without a problem

 

API Reference 05-24-2012 (adding the date for when and if it changes in the future) – no formatting this is how it is dumped to the file when doing “text”

MonkeyRunner help

com.android.monkeyrunner.MonkeyRunner.alert
Display an alert dialog to the process running the current script.  The dialog is modal, so the script stops until the user dismisses the dialog.

Args:
message – The message to display in the dialog.
title – The dialog’s title. The default value is ‘Alert’.
okTitle – The text to use in the dialog button. The default value is ‘OK’.
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.broadcastIntent
Sends a broadcast intent to the device.

Args:
uri – The URI for the Intent.
action – The action for the Intent.
data – The data URI for the Intent
mimetype – The mime type for the Intent.
categories – An iterable of category names for the Intent.
extras – A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
component – The component of the Intent.
flags – An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.(see android.content.Context.sendBroadcast(Intent))
Returns: returns nothing.

com.android.monkeyrunner.MonkeyRunner.choice
Display a choice dialog that allows the user to select a single item from a list of items.

Args:
message – The prompt message to display in the dialog.
choices – An iterable Python type containing a list of choices to display
title – The dialog’s title. The default is ‘Input’
Returns: The 0-based numeric offset of the selected item in the iterable.

com.android.monkeyrunner.MonkeyImage.convertToBytes
Converts the MonkeyImage into a particular format and returns the result as a String. Use this to get access to the rawpixels in a particular format. String output is for better performance.

Args:
format – The destination format (for example, ‘png’ for Portable Network Graphics format). The default is png.
Returns: The resulting image as a String.

com.android.monkeyrunner.MonkeyDevice.drag
Simulates dragging (touch, hold, and move) on the device screen.

Args:
start – The starting point for the drag (a tuple (x,y) in pixels)
end – The end point for the drag (a tuple (x,y) in pixels
duration – Duration of the drag in seconds (default is 1.0 seconds)
steps – The number of steps to take when interpolating points. (default is 10)
Returns: returns nothing.

com.android.monkeyrunner.MonkeyView.getAccessibilityIds
Returns the accessibility ids of the current view

Returns: The accessibility ids of the view as a list of ints

com.android.monkeyrunner.MonkeyRect.getCenter
Returns a two item list that contains the x and y value of the center of the rectangle

Returns: The center coordinates as a two item list of integers

com.android.monkeyrunner.MonkeyView.getChecked
Get the checked status of the view

Returns: A boolean value for whether the item is checked or not

com.android.monkeyrunner.MonkeyView.getChildren
Returns the children of the current view

Returns: The children of the view as a list of MonkeyView objects

com.android.monkeyrunner.MonkeyView.getEnabled
Returns the enabled status of the view

Returns: The enabled status of the view as a boolean

com.android.monkeyrunner.MonkeyView.getFocused
Returns the focused status of the view

Returns: The focused status of the view as a boolean

com.android.monkeyrunner.MonkeyRect.getHeight
Returns the height of the rectangle

Returns: The height of the rectangle as an integer

com.android.monkeyrunner.MonkeyDevice.getHierarchyViewer
Get the HierarchyViewer object for the device.

Returns: A HierarchyViewer object

com.android.monkeyrunner.MonkeyView.getLocation
Returns the location of the view in the form of a MonkeyRect

Returns: The location of the view as a MonkeyRect object

com.android.monkeyrunner.MonkeyView.getParent
Returns the parent of the current view

Returns: The parent of the view as a MonkeyView object

com.android.monkeyrunner.MonkeyDevice.getProperty
Given the name of a variable on the device, returns the variable’s value

Args:
key – The name of the variable. The available names are listed in http://developer.android.com/guide/topics/testing/monkeyrunner.html.
Returns: The variable’s value

com.android.monkeyrunner.MonkeyDevice.getPropertyList
Retrieve the properties that can be queried

Returns: returns nothing.

com.android.monkeyrunner.MonkeyImage.getRawPixel
Get a single ARGB (alpha, red, green, blue) pixel at location x,y. The arguments x and y are 0-based, expressed in pixel dimensions. X increases to the right, and Y increases towards the bottom. This method returns a tuple.

Args:
x – the x offset of the pixel
y – the y offset of the pixel
Returns: A tuple of (A, R, G, B) for the pixel. Each item in the tuple has the range 0-255.

com.android.monkeyrunner.MonkeyImage.getRawPixelInt
Get a single ARGB (alpha, red, green, blue) pixel at location x,y. The arguments x and y are 0-based, expressed in pixel dimensions. X increases to the right, and Y increases towards the bottom. This method returns an Integer.

Args:
x – the x offset of the pixel
y – the y offset of the pixel
Returns: An unsigned integer pixel for x,y. The 8 high-order bits are A, followedby 8 bits for R, 8 for G, and 8 for B.

com.android.monkeyrunner.MonkeyDevice.getRootView
Obtains current root view

Returns: The root view object

com.android.monkeyrunner.MonkeyView.getSelected
Returns the selected status of the view

Returns: The selected status of the view as a boolean

com.android.monkeyrunner.MonkeyImage.getSubImage
Copy a rectangular region of the image.

Args:
rect – A tuple (x, y, w, h) describing the region to copy. x and y specify upper lefthand corner of the region. w is the width of the region in pixels, and h is its height.
Returns: a MonkeyImage object representing the copied region.

com.android.monkeyrunner.MonkeyDevice.getSystemProperty
Synonym for getProperty()

Args:
key – The name of the system variable.
Returns: The variable’s value.

com.android.monkeyrunner.MonkeyView.getText
Returns the text contained by the view

Returns: The text contained in the view

com.android.monkeyrunner.MonkeyDevice.getViewByAccessibilityIds
Obtains the view with the specified accessibility ids.

Args:
windowId – The window id of the view to retrieve.
accessibility id – The accessibility id of the view to retrieve.
Returns: The view object with the specified id.

com.android.monkeyrunner.MonkeyDevice.getViewById
Obtains the view with the specified id.

Args:
id – The id of the view to retrieve.
Returns: The view object with the specified id.

com.android.monkeyrunner.MonkeyView.getViewClass
Returns the class name of the view

Returns: The class name of the view as a string

com.android.monkeyrunner.MonkeyDevice.getViewIdList
Retrieve the view ids for the current application

Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.getViewsByText
Obtains a list of views that contain the specified text.

Args:
text – The text to search for
Returns: A list of view objects that contain the specified text.

com.android.monkeyrunner.MonkeyRect.getWidth
Returns the width of the rectangle

Returns: The width of the rectangle as an integer

com.android.monkeyrunner.MonkeyRunner.help
Format and display the API reference for MonkeyRunner.

Args:
format – The desired format for the output, either ‘text’ for plain text or ‘html’ for HTML markup.
Returns: A string containing the help text in the desired format.

com.android.monkeyrunner.MonkeyRunner.input
Display a dialog that accepts input. The dialog is ,modal, so the script stops until the user clicks one of the two dialog buttons. To enter a value, the user enters the value and clicks the ‘OK’ button. To quit the dialog without entering a value, the user clicks the ‘Cancel’ button. Use the supplied arguments for this method to customize the text for these buttons.

Args:
message – The prompt message to display in the dialog.
initialValue – The initial value to supply to the user. The default is an empty string)
title – The dialog’s title. The default is ‘Input’
okTitle – The text to use in the dialog’s confirmation button. The default is ‘OK’.The text to use in the dialog’s ‘cancel’ button. The default is ‘Cancel’.
cancelTitle –
Returns: The test entered by the user, or None if the user canceled the input;

com.android.monkeyrunner.MonkeyDevice.installPackage
Installs the specified Android package (.apk file) onto the device. If the package already exists on the device, it is replaced.

Args:
path – The package’s path and filename on the host filesystem.
Returns: True if the install succeeded

com.android.monkeyrunner.MonkeyDevice.instrument
Run the specified package with instrumentation and return the output it generates. Use this to run a test package using InstrumentationTestRunner.

Args:
className – The class to run with instrumentation. The format is packagename/classname. Use packagename to specify the Android package to run, and classname to specify the class to run within that package. For test packages, this is usually testpackagename/InstrumentationTestRunner
args – A map of strings to objects containing the arguments to pass to this instrumentation (default value is None).
Returns: A map of strings to objects for the output from the package. For a test package, contains a single key-value pair: the key is ‘stream’ and the value is a string containing the test output.

com.android.monkeyrunner.MonkeyRunner.loadImageFromFile
Loads a MonkeyImage from a file.

Args:
path РThe path to the file to load.  This file path is in terms of the computer running MonkeyRunner and not a path on the Android Device.
Returns: A new MonkeyImage representing the specified file

com.android.monkeyrunner.MonkeyDevice.press
Send a key event to the specified key

Args:
name – the keycode of the key to press (see android.view.KeyEvent)
type – touch event type as returned by TouchPressType(). To simulate typing a key, send DOWN_AND_UP
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.reboot
Reboots the specified device into a specified bootloader.

Args:
into – the bootloader to reboot into: bootloader, recovery, or None
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.removePackage
Deletes the specified package from the device, including its associated data and cache.

Args:
package – The name of the package to delete.
Returns: True if remove succeeded

com.android.monkeyrunner.MonkeyImage.sameAs
Compare this MonkeyImage object to aother MonkeyImage object.

Args:
other – The other MonkeyImage object.
percent – A float in the range 0.0 to 1.0, indicating the percentage of pixels that need to be the same for the method to return ‘true’. Defaults to 1.0.
Returns: boolean ‘true’ if the two objects contain the same image.

com.android.monkeyrunner.MonkeyView.setFocused
Sets the focused status of the view

Args:
focused – The boolean value to set focused to
Returns: returns nothing.

com.android.monkeyrunner.MonkeyView.setSelected
Sets the selected status of the view

Args:
selected – The boolean value to set selected to
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.shell
Executes an adb shell command and returns the result, if any.

Args:
cmd – The adb shell command to execute.
Returns: The output from the command.

com.android.monkeyrunner.MonkeyRunner.sleep
Pause the currently running program for the specified number of seconds.

Args:
seconds – The number of seconds to pause.
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.startActivity
Starts an Activity on the device by sending an Intent constructed from the specified parameters.

Args:
uri – The URI for the Intent.
action – The action for the Intent.
data – The data URI for the Intent
mimetype – The mime type for the Intent.
categories – A Python iterable containing the category names for the Intent.
extras – A dictionary of extras to add to the Intent. Types of these extras are inferred from the python types of the values.
component – The component of the Intent.
flags – An iterable of flags for the Intent.All arguments are optional. The default value for each argument is null.(see android.content.Intent)
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.takeSnapshot
Gets the device’s screen buffer, yielding a screen capture of the entire display.

Returns: A MonkeyImage object (a bitmap wrapper)

com.android.monkeyrunner.MonkeyDevice.touch
Sends a touch event at the specified location

Args:
x – x coordinate in pixels
y – y coordinate in pixels
type – touch event type as returned by TouchPressType()
Returns: returns nothing.

com.android.monkeyrunner.MonkeyDevice.type
Types the specified string on the keyboard. This is equivalent to calling press(keycode,DOWN_AND_UP) for each character in the string.

Args:
message – The string to send to the keyboard.
Returns: returns nothing.

com.android.monkeyrunner.MonkeyRunner.waitForConnection
Waits for the workstation to connect to the device.

Args:
timeout – The timeout in seconds to wait. The default is to wait indefinitely.
deviceId – A regular expression that specifies the device name. See the documentation for ‘adb’ in the Developer Guide to learn more about device names.
Returns: A ChimpDevice object representing the connected device.

com.android.monkeyrunner.MonkeyDevice.wake
Wake up the screen on the device

Returns: returns nothing.

com.android.monkeyrunner.MonkeyImage.writeToFile
Write the MonkeyImage to a file.  If no format is specified, this method guesses the output format based on the extension of the provided file extension. If it is unable to guess the format, it uses PNG.

Args:
path – The output filename, optionally including its path
format – The destination format (for example, ‘png’ for¬† Portable Network Graphics format.
Returns: boolean true if writing succeeded.

Monkey Runner (help.py and API Referrence)

Running on Windows 7

So trying something more robust now for test automation for Android (while trying to avoid 3rd party apps)¬† This time Monkey Runner (not the same as Monkey) – something also included in the Android SDK. This takes us into a more robust scripting language Python (which I haven’t played with much before, but there is always google)

Monkey Runner

One problem I ran into is generating the API Reference.

Per the Android Website I should run this:

monkeyrunner help.py <format> <outfile>

Which translates to this

monkeyrunner help.py text help.txt

Help.txt just being the name of the file where I wanted to dump the information.

First problem, I didn’t have help.py.

I finally found it online here: http://source-android.frandroid.com/sdk/monkeyrunner/scripts/help.py

Second Problem

But when I tried to run it, I kept saying:¬† “oops”

After some trouble shooting, I found that it kept trying to “create” the directory that “monkeyrunner” was already installed in an running. – Lame

So I ended up changing this line in help.py

os.makedirs(dirname)

To:

fo = open(saveto_file, “w”)

I also printed out the name and status of the file (though not needed) and changed a copy other lines from using “fp” to “fo”

#Print statements not need but I added them for me
print ‘Name of the file: ‘, fo.name
print ‘Closed or not¬†¬† : ‘, fo.closed
print ‘Opening mode¬†¬†¬† : ‘, fo.mode
print ‘Softspace flag¬† : ‘, fo.softspace

# Changed fp to fo
fo = open(saveto_path, ‘w’)
fo.write(output)
fo.close()

And I included the full path in the file name (though there is probably a better way to do that if you are a python guru.

monkeyrunner help.py text C:\MyPath\help.txt

But it worked and I got the API reference, while I will also post and I will upload a copy of the help.py file

Get a file listing from the Android Device (sdcard)

You can push or pull files from the device, but to get a file listing just shell to the device (for me I needed a listing of what I had pushed to the sdcard, since it has been a while since I worked with a particular device)

adb shell <enter>

prompt changes to $

cd sdcard <enter>

then ls <enter>

results:

ls
LOST.DIR
Android
DCIM
download
media
upload_avatar.jpg
login_5304301725.txt
testMonkey.txt

To nuke (delete/remove) a file rm <filename>

Note:

Tab completion doesn’t work (at least on my windows machine with just the Android SDK installed)

And there is confirmation when you hit enter the file is gone

Then you can get out of the shell by typing exit

Key names array malformed

Tried running my login script

  • adb shell monkey -p com.company.app -v -v -v -f /sdcard/login.txt 1

 

Got back this:

:Monkey: seed=0 count=1
:AllowPackage: com.mpowerlabs.coin
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
** Error: Key names array malformed (internal error).

This only happens on my Huawei U8500 running 2.2.2 – the same script runs fine my low end comet running 2.2 – still trying to find the reason

Screen Captures on the Droid (screen shots)

There is an app to do this, but it can also be done with one of the tools included in the Android SDK

  1. First you need to make sure your system can talk to your device, the easiest way is to open up a prompt and run
    • adb devices
    • Mine returns:
      • List of devices attached
        999DBA1586BE    device
    • So I know my device is connected
  2. Under tools there is a file is a file called: ddms.bat
  3. Run this (double click in windows works fine)
  4. It will launch Dalvik Debug monitor
  5. Once running РOn the menu go to Devices (Screen Capture) and you will see the screen of the android and the app running.  Just refresh as you go and click save for each screen you want.
  6. Detailed setup information can be found here: ¬†“How to Capture¬†Screen shots¬†with your Android Mobile Phone” (Make Use of Write Up)

Here are a few other ways to do screen shots (though I haven’t tried them all – from a Make Use of article)

AShot aka Android Screen Capture

AShot is a free, open-source way to take screenshots of your Android device from your computer. It requires the Android SDK installed and properly configured. AShot offers a variety of advantages over the Android SDK screenshot feature, including near-real-time streaming of your Android’s screen to the Ashot application on your computer. You won’t have to keep clicking the Refresh button, as in the SDK. For more information, check out ourAShot walkthrough.

Other options can be found here:

6 ways to take Screenshots on your Android (Make Use of)

How to Copy and Paste on Android Phones

I keep forgetting how to do this, since I test on the iPhone more

It’s a little unwieldy on Android phones, but copying and pasting text gets easier with practice. Either press and hold down the copy you wish to select, copy, cut, or paste; or push and hold down the trackpad button (if available). When a contextual menu appears, select the proper action (select, copy, cut, or paste).

Installing New Packages

Servers are up and running and builds are starting to be delivered to QA to test with, so now I have to install them on the Android.

At first this seemed simple

adb install (package).apk

adb install myapp.apk

Of course I was wrong, I got an error, package was already installed – I knew this but I was hoping it would just over write it, I was wrong.

adb install myapp.apk
1366 KB/s (1443285 bytes in 1.031s)
pkg: /data/local/tmp/myapp.apk
Failure [INSTALL_FAILED_ALREADY_EXISTS]

So I tried uninstalling it

adb uninstall myapp.apk
Failure

And not a very helpful error.¬† But I figured that it wasn’t really installed with that package name (a developer had installed the package and handed the phone to me the 1st time)

So Next I thought I would try listing all the installed packages

adb shell pm list packages

I got this list back (partial)

package:com.tmobile.selfhelp
package:com.google.android.location
package:com.android.soundrecorder
package:com.android.voicedialer
package:com.android.defcontainer
package:com.android.launcher
package:com.example.android.notepad
package:com.android.vending.updater
package:com.android.contacts
package:com.android.inputmethod.latin
package:com.huawei.mmitest2
package:com.android.phone
package:com.google.android.partnersetup
package:com.android.calculator2
package:com.my.app.android  <Рmy app
package:com.android.htmlviewer
package:com.google.android.voicesearch
package:com.android.providers.calendar
package:com.android.bluetooth
package:com.android.calendar
package:com.google.android.feedback
package:com.android.browser
package:com.android.music
package:com.google.android.talk

So I tried uninstalling that package name

adb uninstall com.mm.app.android
Success

Cool, now I can install the new package (app)

adb install myapp.apk
1366 KB/s (1443285 bytes in 1.031s)
pkg: /data/local/tmp/myapp.apk
Success   <- Awesome

Of course all this was after I got my Windows XP machine to talk to my Android, which required Driver updates and such, I will post most about that later.