Recently I got time to do a full migration from project done with eclipse to Android studio, since now seems much more stable perhaps is a bit more slow compiling than eclipse+ADT, it has a much nicer editor and a much better refactor tools.

First steps to migrate to android studio is basically make it compile with gradle. Gradle is just another dependency-analizer tool like maven with steroids, basically you can create functions instead of only have a xml DSL.

First make sure you got the gradle wrapper on the root of your project, that means having a gradlew executable and a gradle folder. and put it into your git, every developer who wants to build the app will not need to have installed gradle with the gradle-wrapper will be able to perform all the tasks. * Make sure to download the latest gradle wrapper, I had really bad headaches for using a old one

Next create a empty file named settings.gradle to include all the subprojects, my project was quite big so I had two shared libraries and a main app.

include ':App', ':library1', 'library2'

also create a local.properties file with the sdk.dir property.

sdk.dir=/Applications/Android Studio.app/sdk

First we are going to configure the first library that is the most easy one,  so we can explain the basics of the build.gradle file

buildscript {  
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:0.8.+'
  }
}

apply plugin: 'android-library'

repositories {  
  mavenCentral()
}

android {  
  compileSdkVersion 19
  buildToolsVersion "19.0.0"

  defaultConfig{
    minSdkVersion 8
  }

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      resources.srcDirs = ['src']
      res.srcDirs = ['res']
      assets.srcDirs = ['assets']
    }
  }

}

dependencies {  
  compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
}

Starting from the beginning, we must set which plugins are going to be use for building this artificat, and also on which repositories we can found it. In this library we are going to use only the google android plugin for build, exactly the version 0.8 or greater (but not 0.9). If you like you could use the simply the + version to use always the latest version.

Next we say which plugins we are going to use, generally to build an android library use the 'android-library' or if you are building an app simply use the 'android' plugin.

Next we set were gradle must search for the dependencies for the artifact don't get mess with the first repository tag. The first one was for the plugins you need to build the artifact and this one is for the artifact dependencies itself.

The key point is the sourceSet tag, where you define where each folder of the project are, you will see a lot of build.gradle that not define this section, mainly is because this projects are started from scracth with Android studio which have a different folder layout than eclipse projects and the default values are correct. If you come from a eclipse project odds are that you need the same values that in this example.

Finally the last step is just to declare the dependencies of the library, just like you do in a pom.xml.

The second library is a bit more complex, basically is a shippable library (as a jar)  and use the first library as a dependency.

    dependencies {  
      compile project(':library1')
    }
  }

  task jar(type: Jar) {
      from android.sourceSets.main.java
  }

See how we indicate that library1 is a dependency of this library, also we have added a new jar task to build the jar.

 

buildscript{  
  repositories{
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.7.+'
    classpath 'org.holoeverywhere:plugin:0.2.+' 
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
  }
}

apply plugin: 'holoeverywhere-app' // order matter!!!  
apply plugin: 'android'  
apply plugin: 'android-apt'

repositories {  
  mavenCentral()
}
def AAVersion = "3.0"

android {  
  compileSdkVersion 19
  buildToolsVersion "19.0.0"

  defaultConfig 
  {
    minSdkVersion 8
    targetSdkVersion 19
  }
  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      resources.srcDirs = ['src']
      res.srcDirs = ['res']
      assets.srcDirs = ['assets']
    }
  }
}

dependencies {  
  compile project(':library')
  compile 'net.simonvt.menudrawer:menudrawer:3.0.5'
  compile 'de.greenrobot:eventbus:2.2.0'
  compile 'com.koushikdutta.ion:ion:1.2.4'

  apt 'com.googlecode.androidannotations:androidannotations:2.7'
  compile 'com.googlecode.androidannotations:androidannotations-api:2.7'

  compile 'com.android.support:support-v4:19.0.1'
}

holoeverywhere {  
  // Addons for library
  addons {
    preferences
  }
  library {
    version = 'latest' // you can use 'latest', 'snapshot' or specific version
  }
}

apt {  
  arguments {
    androidManifestFile variant.processResources.manifestFile 
    resourcePackageName 'net.coscolla.awesomeapp'
   logConsoleAppender true
  }
}

On the app I was using much of magic, I really love the AndroidAnnotations library to write more readable code with less lines, on eclipse you use it with simply configuring a new annotations processing. With gradle we use the apt plugin to process the annotations.

On the dependency we define which annotations library apt must use, and also define some variable for apt at the end of the build.gradle file.

Now that everything is nicely ocnfigured you should be able to execute

./gradlew assembleDebug

to create a debug apk. To see a list of all the gradle task just execute

./gradlew tasks

Hope it is useful for everyone, sure it will be for me and all the project I must migrate slowly

I will be completing this guide as I learn more about gradle, next steps will be make the apk signing for release and process with proguard.