Java Generics

Today I discovered a feature of java that I have been using for a while but I didn’t know it existed. Generics allow developers to create generic algorithms that work on collections of different data types but at the same time provide type safety.

Generics are commonly used for data structures. List is an example of a data structure that uses generics:

List<String> list = new ArrayList<String>();

You could have declared your list like this:

List list = new ArrayList();

The difference is that by using generics you make sure that you don’t accidentally try to insert something that is not a string into your list and cause a run time error. By specifying in the list declaration that this is a List of Strings you make sure that you get a compile time error if you try to add something that is not a string to the list.

Read More

Building an Android library with gradle

Android has moved away from ant and adopted gradle as its build system. I’m not very familiar with gradle but there is a feature of the new build system that makes it really appealing to migrate to it. The new gradle build system compiles libraries into an .aar (Android ARchive) which includes it’s resources and assets in a way that can be consumed by the apps that use your library.

This means you no longer need to copy the source code of the library into your project and compile both projects together, now you can just drop the .aar into your libs folder and it will work.

To use gradle with Android you need at least gradle version 1.10. I got the latest version at the time (1.12) and things worked fine for me.

Read More

Using ESLint to enforce JS coding conventions

I spend a decent amount of time reviewing code at work. After a while I get tired of reminding developers that the opening brace for an if statement goes in the same line. JSLint and JSHint do a good job of preventing developers from doing things that would break your code but it doesn’t really allow you to enforce coding conventions the way I wanted.

I was looking for a way to create custom rules for JSHint so I could enforce our team conventions but after some research I found that it is not possible and it is not something we should expect from the project. It seems like Nicholas Zakas wanted the same thing as me and he didn’t find it, so he built it. That is how ESLint was born. It is a relatively new project but I gave it a try and it seems to work very well.

You can use npm to install it globally:

npm i -g eslint
Read More

Creating a library for Android

I already went through the process of creating an application from the command line. This time I am going to show how to create a library project and integrate it into your app.

To create your library project create a folder and execute this command from inside that folder:

android create lib-project \
--name <project_name> \
--target 1 --path . \
--package com.example.whatever

Then you can you ahead and write some code. I created a view that will print a hello message (src/com/ncona/hello/

Read More

Array Pair Sum

This is the first post on a series where I will be resolving some common coding interview questions.

The question

Given an integer array, output all pairs that sum up to a specific value k.

My solution

I like to start by creating a test for my solution:

var arr = [1, 4, 2, 6, 8, 3, 9, 0, 7];
var res = findPairs(arr, 7);
// I expect res to be [[1, 6], [4, 3], [0, 7]]
Read More

Building back stack correctly when coming from a notification on Android

After I added some notifications to my app I noticed that they weren’t behaving the way I wanted them to behave. When I clicked on a notification I landed on one of my internal screens. When I clicked back I was expecting to go to the main screen but instead it went back to the phone home.

After reading a little I discovered that there is a difference between navigating back and navigating up (Read the related links to learn more), and that I needed to properly set the up navigation before I could fix this.

To set the up navigation correctly you need to specify a parent for each of your activities in your manifest:

<activity android:name="Internal" android:label="@string/app_name"
           android:value=".MainActivity" />
Read More

Schedule your Android app to do something periodically

I am writing a little Android app that will send alarms to the user based on certain rules. For this there are certain things that I need:

  • I want to have something that will run periodically and check my DB to see if there are any alarms I should send to the user.
  • I want this to run even if my app is closed or the phone is asleep
  • I want this to start automatically every time the phone is turned on

I did a little research and these are the pieces we need:

  • Services – To have our app do something in the background
  • AlarmManager – To schedule the service to be executed in the future
  • ACTION_BOOT_COMPLETED – This is a broadcast intent that Android sends when it boots

Now, lets start putting things together.

Read More

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:


And install the Google Play services package:


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:

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:

npm install -g grunt-init
Read More