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

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