Kitchen Hacker

RSS

Posts tagged with "android SDK"

Simple & Painless Way To Do Android Database Development

The kicker……on an unrooted phone.

In order to work with the Android database in DDMS or on the cmd line, you need to have root access to your phone. Up until now, doing any kind of work in the database realm of Android has been extremely painful for me because I cant access the DB on a running device, but I can access it on an emulator running my application. Anyone who has done any android work knows that dealing with the emulator is a whole world of slowness, spinning wheels, and pain (the details of which I wont get into here).

So I have been toying with the idea of rooting my Nexus S in order to gain this root access. However, there are a lot of reasons why you might not want to root your phone, so I also spent a lot of time searching for an alternative. Finally I was at the Android Developer Labs (Thanks @androiddev), where I had a chance to talk to some fellow android developers. I explained my problem and one of the developers told me that he and his partner wrote a script that would dump the SQLlite DB to the SD card on startup of the app. So I decided to run with it.

Some things to note aobut how and why I wrote this:

  1. The SD card contents are not sandboxed to your app, so I only allow this ability in debug (read:dev) builds. 
  2. I did not want to dump the DB on every start up, moreover I wanted the ability to take a snapshot anytime.
  3. I have been trying to find innovative ways to put dev functions into our app.

I wrote three parts to make this work. First I created a *DEBUG* section of our settings menu. A <PreferenceCategory> that sits at the top of our <PreferenceScreen> and when I inflate the Preference screen I check our Constants class to see if the app is a debug build, if it is not, I immediately dispose of the debug category. Now I have a home for any functions that I want to have and test. For instance, sometimes you might want to test firing a service at any given point in your app lifecycle, this is now a good place to host that from.

Second, I wrote a small AsyncTask that runs on a background thread to grab the path to the current database, generally found at /data/data/database/{packageName}/databases/{databaseName} and copy that file over to the SD card using a couple of helper classes and functions.

Third I wrote a small DBHelper function and a small FileIOUtility class that create a copy File of the current database (checks to see if it even exists first), and then writes that exact file to an app location on the SD card. For my purposes I’m using the same location where our ImageManager stores any local photos we have downloaded. or /mnt/sdcard/{appName}

And that’s it really, now I can have my application running on my test device, unrooted, but still dump the db so that I can explore it using some DB tool or just the sqllite cmd line tool in the SDk. A simple problem that was a pain to deal with before.