Android Protip: Copy a SQLite database

Disclaimer: NO Root access required

Sometimes you need to export your app’s SQLite database to your computer for further analysis. Seeing that there isn’t a ‘cp’ command, this is how you’d go about doing it:

adb shell ‘run-as cat /data/data/ > /sdcard/mydb.sql’

Leave a comment

Posted by on August 14, 2013 in Android


Tags: , ,

Top 10 Performance Techniques for Mobile Web Apps

Christophe Coenraets (Adobe technical evangelist) did a great talk on Top 10 Performance Techniques for PhoneGap Mobile Web Apps (link)

I thought I’d quickly outline the techniques discussed in his talk

#10 Don’t generate the UI at the server-side
– Create the UI in javascript at the client-side
– Insert views into- or remove from the DOM

#9 Don’t wait for the data to display the UI
– Perception management
– People will wait for data, not for UI

#8.1 Cache static data
– LocalStorage / SQLite

#8.2 Cache Dynamic Data
– Present cached data first, then
– update UI when requested data arrives

#8.3 Cache JS query selectors
– assign selectors to avoid traversing the DOM repeatedly
– Coding convention tip: prefix ‘$’ to selector variables e.g ‘var $backButton’

#8.4 Cache Precompiled templates

#7 Use CSS transitions with hardware acceleration
– (CSS) transform: translate3d()
– micro-library:

#6 Avoid ‘Click-Event 300ms’ Delay
– micro-library: FastClick :

#5 Use CSS Sprites
– ala Glyphicons

#4 Limit use of shadows and gradients

#3 Avoid reflow (re-layouts)
– Build UI off-DOM then apply

#2 Do you need that framework?

#1 Test. Test. Test
– Test with production data
– Test with many devices
– test with bad networks

Leave a comment

Posted by on June 4, 2013 in HTML5, Mobile, PhoneGap


Tags: , , , ,

Pro-Tip : ADB via Wifi

I recently worked on an Android app that used the device’s only USB I/O port to control another piece of hardware.  This requirement eliminated my ability to use LogCat or debug the application…until I discovered that you can use ADB over wifi.

This takes just a few steps:

1. Ensure that your Android device is online and connected to the ADB servier ($ adb devices)

2. Switch to TCP/IP port '$ adb tcpip 5555'

3. Ensure that your device is connected to the same network as your dev machine i.e you can ping it.

4. Connect to your Android device over the network ($ adb connect <Device IP address> ) eg: '$ adb connect'

5. Unplug your Android device from the USB

To reset your ADB server to USB, you can:

1. kill/start your ADB server, or

2. use '$ adb usb'.
Leave a comment

Posted by on May 27, 2013 in Android


Tags: ,

Pro-Tip: LogCat Regex Filtering

One day, You may need to filter the LogCat output. So, here are some quick examples:

Filter tag ‘FOO’ or ‘MainActivity:

Filter ‘Hello World’ in the log text:
text:^(?!.*(Hello World)).*$

You can combine filter by simply concatenating filter expressions:
tag:^(?!.*(FOO)).*$ text:^(?!.*(BAR)).*$

Leave a comment

Posted by on May 12, 2013 in Android, Uncategorized


Tags: , , ,

Unit Testing for Android – Mockito

Unit testing in general is one of the after thoughts for many during the development process. I was recently reminded how important unit testing is and that it should seriously be considered when estimating time/effort during the project planning/estimation day(s) in your Agile process (you are Agile, right?).

I see unit testing being used in two ways: first, for algorithm/logic validation and secondly, for behavior validations (BDD – Behavior Driven Development).

In this post, I’m just going to illustrate how to use Mockito to validate algorithms/logic at unit(method)-level on an Android application.

The Ingredients

The following libraries are required in your test project’s classpath:

  • Mockito (as of this post mockito-1.9.5)  – link
  • DexMaker (dexmaker-1.0 and dexmaker-mockito-1.0) – link
As of 1.9.5, Mockito supports the Delvik VM by using DexMaker to generate
mock classes dynamically, allowing the unit test to run on your android 

The Preparation

Test Subject

Suppose our app stores data in a SQLite database while offline. Once the app is connected, it can sync with the server.

public void syncData( final ConnectionManager connectionManager )
    final NetworkInfo networkInfo = connectionManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI );
    if ( networkInfo.isAvailable() && networkInfo.isConnected() ) {
        //...sync data

Test Case Structure

When using Mockito, I find it useful to follow the Setup/Exercise/Verify pattern:

  • Setup – Setup the stub/mock objects and their behavior
  • Exercise – invoke the method(s) to tests
  • Verify – Verify that the stubs/mock objects were interacted with as expected
public class TestApplication extends AndroidTestCase {
public void testSyncData() {
    // Setup
    final ConnectivityManager connectivityManager = Mockito.mock( ConnectivityManager.class );
    final NetworkInfo networkInfo = Mockito.mock( NetworkInfo.class );
    Mockito.when( connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI ).thenReturn( networkInfo );
    Mockito.when( networkInfo.isAvailable() ).thenReturn( true );
    Mockito.when( networkInfo.isConnected() ).thenReturn( true );
    // Exercise
    syncData( connectivityManager );
    // Verify
    Mockito.verify( connectivityManager ).getNetworkInfo();
    Mockito.verify( networkInfo ).isAvailable();
    Mockito.verify( networkInfo ).isConnected();

The Execution

The final step is the run the testcase as a Android JUnit Test.  You’ll notice that a ‘Test’ APK is built and installed on your device and presents a non-destructive way of testing applicable behavior on specific devices.

Once you work more with the Mockito API, you’ll see just how powerful this tool is and how you’ll benefit from better application code structure, architecture and reliability.

In a future post, I’ll discuss how Robolectric will speed up our testing cycles.


Posted by on April 24, 2013 in Android


Tags: , , ,

Git Protip: Copy a file from another branch

It occasionally happens that I’ve fixed a bug in BRANCH_A and while BRANCH_A is waiting to be merged, I create a new branch (BRANCH_B) for further development/bugfixing, but wait!  I really need the bugfix in BRANCH_A in order to continue in BRANCH_B.

This can easily be solved using the following git command (from your working branch, BRANCH_B):

git checkout BRANCH_A myfile.txt
Leave a comment

Posted by on April 22, 2013 in Git


Tags: ,

Android : C2DM and the TokToki -push-messaging service

C2DM (Android Cloud 2 Device Message) was announced at Google IO 2010.  The feature was introduced to relieve the applications from polling cloud resources e.g email.

Fortunately, C2DM has an API, so I thought I’d create a platform off the C2DM infrastructure. This involved developing a back-end application server (I’m running on Google App Engine) and an Android client library that Android developers can utilities to enhance their application (Android 2.2+ required) with push messaging.

TheTok-Toki service (named after the Tok-Tokkie beetle, indigenous to South Africa, known for tapping on the ground to communicate to other beetles). is hosted here.


Leave a comment

Posted by on November 23, 2011 in Android


Tags: , ,


Get every new post delivered to your Inbox.

Join 38 other followers