Setting up Google Analytics for Android

Setting up Google Analytics for a website is as simple as adding a snippet of JS on each of your pages. I was expecting the same for Android apps, but it seems like you need to follow a few steps to get this to work.

Setting up Google Play services SDK

The Google Analytics library for Android needs to communicate with Google Play API, for this reason, we need to install the SDK. Run this command:

1
android

And install the Google Play services package:

google_play_services

Read More

Android UI Automation using Espresso

I asked around my Android developers which was the best framework out there for Android automation and the most convincing answer was Espresso, so I decided to give it a try. Espresso is designed to be used in environments where the developers write their own tests (which I think should be everywhere), and promises a concise and beautiful syntax.

Set up

To use espresso you have to set up a test project. I decided I was going to place all my automation tests under tests/automation so I created that folder, moved into it and ran this command:

1
2
3
4
android create test-project \
-n MyProjectAutomation \
-p . \
-m ../../
Read More

Creating a grunt plugin

I have been using grunt for a while but some of my tasks started getting a little ugly because I started abusing grunt-shell. Because of this I decided to create a grunt plugin for something that I need for most of my JS projects, unit tests.

To run unit test I use VenusJS test runner for which there wasn’t a grunt plugin. So I decided to create one. Grunt documentation has some information on creating plugins but I felt there were some things missing.

This is the process I followed to create my plugin:

Install grunt-init by running:

1
npm install -g grunt-init
Read More

Make vim Command-T ignore certain files

Since I have been working with Java I found annoying every time I used Command-T to look for a file it showed me not only the source code file, but also the .class file. Looking at the documentation I found that there is a way to have Command-T ignore certain files. Just add this to your .vimrc

1
set wildignore+=*.class
Read More

Hash tables

Hash tables are a very important data structure that can be used for many things. They are really fast so they are a good fit for almost anything where they can be used. One example of when a hash table is useful is an associative array, for example:

1
2
3
4
5
var a = {};
a['hello'] = 'hola';
a['bye'] = 'adios';

console.log(a['hello']); // Prints hola

The beauty of hash tables is that searching for the value of a[‘hello’] (ideally)takes the same time no matter how many values the associative array has. In JavaScript we have this functionality with plain objects, but I needed to understand a little more about it’s implementation so I will explain how you could create your own hash table using arrays.

Read More

Configure syntastic to work fine with Android projects

Synstastic is a syntax checker for many programming languages, including Java. The problem that I was having is that for my Android project it wasn’t helping me at all because it couldn’t find any of the Android libraries and almost every line showed as an error.

The reason for this is that syntastic uses javac in the background to look at the file and find out if there are any errors. It does a good job for classes in the java standard library, but it doesn’t know where to find the Android SDK so it throws errors for every line that makes use of it. To fix this we need to add the java SDK to our path.

There are two ways of doing this. The first one will only last for the length of the vim session and will go away when you quit. Use this vim command:

1
:SyntasticJavacEditClasspath
Read More

How to inspect your Android sqlite DB

I am developing an Android app that makes use of an SQLite database. Every now and then I want to see what is the state of my app’s database to make sure things are being stored the way I expect. To do this you need to connect to your emulator using an adb shell.

Make sure your emulator is running and run this command to get a terminal to the emulator:

1
adb shell

You will be presented with a prompt similar to this one:

1
root@android:/ #
Read More

Customizing PMD rules

PMD allows you to perform code static analysis for your project, but sometimes the default doesn’t fit the way you decided to write code. The good thing is that you can customize the rules you want to use to fit your preferences.

To customize the rules you will need to create an xml file with this structure:

1
2
3
4
5
6
7
<?xml version="1.0"?>
<ruleset name="Custom ruleset"
   xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>Rules for my project</description>
</ruleset>
Read More

Creating a Robolectric project from scratch with ant

I’m still trying to figure out this Android unit testing madness and this time I was finally able to create a Robolectric project without any IDE. Now that I know the steps it doesn’t seem so hard, but it was a long process trying to gather all the information necessary to make this work.

The first thing we need is to create a folder inside our android project. I’ll call mine tests. This is going to be the folder where we will include everything related to our Robolectric tests. The next step is to create a build.xml file so we can build our project with ant:

Read More

Consuming a REST service from Java (Android)

The time finally came when I need to consume a service from my Android app. As I expected this is not as easy as with JavaScript. Strict types, threads and craziness come all into play for this simple task.

The first thing I learned about making a request to a REST service was to use the apache library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public String makeRequest(url) {
  // HttpRequestBase is the parent of HttpGet, HttpPost, HttpPut
  // and HttpDelete
  HttpRequestBase request = new HttpGet(url);
  // The BasicResponseHandler returns the response as a String
  ResponseHandler<String> handler = new BasicResponseHandler();

  String result = "";
  try {
    HttpClient httpclient = new DefaultHttpClient();
    // Pass the handler and request to httpclient
    result = httpclient.execute(request, handler);
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }

  return result;
}
Read More