Last year I produced my first native android application. its called measureme and its done ok on googleplay, over 100 downloads and a review of over 4 stars.

it was developed using eclipse and ant, these were the recommended tools at the time and i used them to build release and debug variants and to run unit tests. towards the end of the year android studio 1.0 was officially released and around the same time android 5.0 was released, this prompted me to migrate to the new development toolset of androidstudio and gradle.

when i fired up androidstudio it would import my eclipse project and that did get me 80% of the way to being migrated but as always the remaining 20% needed some fiddling.

build variants

it took some time to work out how to make the application work slightly differently when build for release and debug builds using custom Ant tasks. In android studio the same effect is much easier, in the Gradle script for the app I set the sourceSets like this

sourceSets {
 debug.setRoot('build-types/debug')
 release.setRoot('build-types/release')
}

Then I created this folder structure

 build types

In the assets folder I created a logback.xml config file, one for the debug build and one for the release build and simelally in the java/net/derekwilson/measureme I created a class like this

package net.derekwilson.measureme;

public class MeasureMeBuildConfig {
 /* Whether or not to include logging statements in the application. */
 public final static boolean PRODUCTION = false;
}

In AndroidStudio the files in the project treeview are changed depending upon the build variant selected, debug in this case

build types2

Unit Tests

Getting the unit tests to run was a bit more fiddly and I am indebted to this post on getting the unit tests running from the command line and this post on getting them running from AndroidStudio. After that it was pretty easy to run them from either the command line or AndroidStudio and there is none of the refresh problems that affected Eclipse.

Signing the App

Signing and copying the app had been complex and once again I had used a combination of Ant and shell scripts. It was just as complex using Gradle. This is because I needed to enter a password to sign the release apk but I did not want to put the password into the Gradle script as this is an open source project.

It turns out that I can use the Console when I build from the command line but I needed a slightly different mechanism from within AndroidStudio. I made some slight modifications and ended up with this Gradle script.

import groovy.swing.SwingBuilder
apply plugin: 'com.android.application'

android {
 compileSdkVersion 21
 buildToolsVersion "21.1.2"

 defaultConfig {
  applicationId "net.derekwilson.measureme"
  minSdkVersion 7
  targetSdkVersion 21
 }

 signingConfigs {
  release {
   storeFile file("../../AndroidSupport/deploy/derek.keystore")
   storePassword "not real password"
   keyAlias "measureme"
   keyPassword "not real password"
  }
 }

 gradle.taskGraph.whenReady { taskGraph ->
  if (taskGraph.hasTask(':app:assembleRelease')) {
   def password = ""

   if (System.console() == null) {
    new SwingBuilder().edt {
     dialog(modal: true,
     title: "Enter signing password",
     alwaysOnTop: true,
     resizable: false,
     locationRelativeTo: null,
     pack: true,
     show: true
     )
    {
     vbox {
      label(text: "Enter signing password: ")
      input = passwordField()
      button(defaultButton: true,
        text: 'OK',
        actionPerformed: {
          password = new String(input.password)
          dispose();
        })
      }
     }
    }
   } else {
    password = System.console().readPassword("\nEnter password: ")
    password = new String(password)
   }

   if (password.size() <= 0) {
    throw new InvalidUserDataException("Empty password")
   }

   // set signing config key passwords
   android.signingConfigs.release.storePassword = password
   android.signingConfigs.release.keyPassword = password
  }
 }

The migration did not take that long and I have found AndroidStudio to be great improvement over Eclipse.