Working with the Android ecosystem, you want to have tools to make your development life easier.
I’ve decided to write weekly development blogs on some topic that I either tackled that week at work or something I’ve been working on. I don’t write much since graduating college, and that’s a big reason for doing this blog. This will also be a good opportunity to reflect on my own progress with software engineering, and if I could help anyone out, that’s just bonus points.
I spend a lot of my time with the Android SDK and all the fun that comes with it. Android development can be difficult because of its nature: open source platform forked by many manufacturers and other businesses, along with numerous devices with different hardware and API versions. A good development environment can ease the pain that one will endure at times in your app development.
Step 1: Make sure your SDK installation is up to date as much as possible.
Why not automate that?
Here’s a little script that can do that for you:
The most important line in this bash script is
android update sdk --no-ui
, as this is the binary
that will update your sdk installation.
Unfortunately, this will require you to accept the licenses for all the new updates
so we’ll use the expect utility to automate our “yes” responses for each prompt.
Run this in, say, cron, and enjoy your automatic updates.
Step 2: Automate your testing
The app I develop at work is supported on over 5000 devices (from api level 2.2 to 4.4.2, as of the time of this writing). Testing new code and refactors on different devices/emulators/VMs takes time, so having a good test suite/continuous integration can facilitate development.
I will not get into how to go about writing/using tests for your app, there are many good tutorials. For reference, check the Android developer site here.
For the app I work on, I use the Robotium framework for its ease of use, minimal development time, and quick execution. Remember I have a big range of devices properties to test against! Check out robotium.
Using Robotium, I have a nice collection of essential UI/integration tests, and unit tests to ensure the experience is consistent from one device to the next, from one user to the next.
Despite the urge to buy a bunch of devices, I have to settle with emulators and virtual machines to span the properties of devices I normally could not physically get my hands on (like API version, screen size, etc.). I have a significant amount of virtual device configurations, as the app can be supported on almost every kind of device.
Ideally I would love to automate this too! Get a coffee and have all the tests run on every emulator and device, find the failing tests and get to work fixing any issues.
So I wrote a bash script to do that for me, partially because I liked the idea and mostly because I want to get better at shell scripting. It’s not perfect, but it serves its intended purpose. Feel free to fork it and make it work for you, if so desired.
It’s 200 lines of fun, so let me explain the gist of it.
Basic usage
This is relatively self-explanatory. Given the package name of your app, (e.g. com.example.myapp), your tests are located in package_name/test.
You can run all the test cases or one particular test case, using the -a or -t param flags.
By default, the tests will run on all physical devices you have connected to your computer, interfacing through ADB (the android debug bridge).
If you want to run the test(s) on your emulators also, use the -e flag.
Dependencies
The android and adb binaries must be in your PATH environment variable. We use the android binary to install your app’s APK on each device, as they may not have it, and adb to run your tests on each device.
In part 2, I’ll explain the rest of the code.