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

Java code static analysis with Pmd

Pmd is a tool for running code static analysis for multiple languages. The first thing you need to use it is download it from Pmd’s website. Clicking the download button will download a zip file. Uncompress that zip and you will have all you need.

Running code static analysis

Once you have pmd on your computer you can analyse your code using this command:

1
<path to pmd>/bin/run.sh pmd -d <src folder> -l java -f <reporting format> -R <rules>
Read More

Using Android lint to check for errors in your code

Android’s lint tool allows you to find common issues in your code by running code static analysis against your project. This tool performs some Android specific checks that some other code static analysis tools can’t do for you. The lint tools comes with the Android SDK under tools/lint. In its most simple form you can use this command:

1
lint <Android project folder>

I like this form because I can easily plug it to my CI system:

1
lint <Android project folder> --exitcode
Read More

Detect when Android emulator is ready

I am writing an Android app, so as any good developer I want to have some tests in place that run continuously to make sure my app doesn’t break. The thing about Android is that I need an emulator in order to run my unit tests, so I need a way to start the emulator and detect that it is ready to be used. To do this we need to verify if the boot animation has finished using this command:

1
adb shell getprop init.svc.bootanim

Now, the only thing we need to do is call this command constantly until we get “stopped”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash

# Kill emulator
adb -s emulator-5554 emu kill

# Start the emulator
emulator -avd NexusOne -gpu on -qemu -enable-kvm &

# Don't exit until emulator is loaded
output=''
while [[ ${output:0:7} != 'stopped' ]]; do
  output=`adb shell getprop init.svc.bootanim`
  sleep 1
done
Read More

Android Logcat Unexpected value from nativeGetEnabledTags: 0

Every time I try to use logcat to debug my android App I get this message a lot:

1
W/Trace &nbsp; ( 1264): Unexpected value from nativeGetEnabledTags: 0

This apparently is some kind of bug. This makes it really hard to read the logs I really care about so I am now using this command to filter all those entries:

1
adb logcat | grep -v .*nativeGetEnabledTags.*
Read More