Last Updated: September 16, 2020
·
59.42K
· itseranga

Migrate existing android project to gradle

Android with ant

  • Before introducing Android Studio, android apps build with ant(in eclipse and Idea)

  • I have existing android application which build with ant, in rest of this tip I'm gonna demonstrate how to migrate the application in to Gradle

  • I have used IntelliJ Idea for ant based project and gonna use Android Studio with Gradle android project

Existing Idea project

  • Following is my exiting project(which build with ant)

Picture

  • Project name is SenZors. It contains src, res, lib, asserts etc

Gradle based project structure

  • Gradle based project looks like below
└── project/
    └── module/
        └── build.gradle/
    └── build.gradle
    └── settings.gradle
  • Main project contains build.gradle and settings.gradle file

  • Each module contains separate build.gradle file

Project vs Module

  • A project is a complete android app. Projects can consist of one or more modules.

  • Module is a component of the project that can build, test, or debug independently

  • There are three types of modules

  1. Java library modules
  2. Android library modules
  3. Android application modules

settings.gradle file

  • This file tells gradle about which modules to build, example from SenZors
include ':senz'
  • This specifies to include senz module when building our SenZors project

Top level build.gradle file

  • It is the top level gradle build file, which can use to define configuration options common to all sub modules

  • I'l give more details about this build.gradle file content later

Module's build.gradle file

  • Each module contains separate build.gradle file, so the module can build independently

  • It can define android specific settings, dependency settings etc..

  • I'l discuss more details about this file content later

Gradle application module structure

  • My main application module name is senz
└── senz/
    └── build/
    └── libs/
    └── src/
        └── main/
            └── java/
            └── res/
            └── asserts/
    └── build.gradle
  • When migrating existing ant project to gradle we need move ant project content according to the gradle project structure

Migrating SenZors to gradle

  • Main project is SenZors, I'm use senz as the main application module

Define project structure

  • Following are the steps for creating project structure
1. Create new application module directory `senz` in `SenZors`
mkdir -p SenZors/senz/src/main/java

2. Move source codes to senz
mv SenZors/src/com SenZors/senz/src/main/java

3. Move res and asserts
mv SenZors/res SenZors/senz/src/main
mv SenZors/asserts SenZors/senz/src/main  

4. Move libs
mv SenZors/libs SenZors/senz/

5. Move AndroidManifest 
mv SenZors/AndroidManifest.xml SenZors/senz/src/main

6. Create gradle build  files
touch SenZors/build.gradle
touch SenZors/settings.gradle
touch SenZors/senz/build.gradle

Define settings.gradle

  • Define which modules to build in settings.gradle file(we have only one application module senz)
include ':senz'

Top level build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

# Gradle wrapper task
task wrapper(type: Wrapper) {
    gradleVersion = '1.12'
}
  • There are two main parts in here

buildscript settings

  • In here buildscript { ... } configures the code driving the build

  • It declares that it uses the Maven Central repository, and that there is a classpath dependency on a Maven artifact

  • This artifact is the library that contains the Android plugin for Gradle in version 0.9.+

Gradle wrapper task

  • The Gradle Wrapper is the preferred way of starting a Gradle build

  • Gradle wrapper consists a batch script for Windows support and a shell script for support on OS-X and Linux

  • These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system(this is very helpful on continuos integration with jenkins)

  • In above gradle we define gradle wrapper with gradle version 1.12 (gradleVersion is the version you'd like the project to use)

  • Now execute following command from command line

gradle wrapper
  • This command will generates some more files in the root of your project
└── SenZors
    └── gradlew
    └── gradlew.bat
    └── gradle
        └── wrapper
            └── gradle-wrapper.jar
            └── gradle-wrapper.properties

     ...

    └── build.gradle
    └── settings.gradle
  • The first time you run the wrapper for a specified version of Gradle, it downloads and caches the Gradle binaries for that version

  • These Gradle Wrapper files are designed to be committed to source control.

  • Then anyone can build the project without having to first install and configure a specific version of Gradle

  • Now you can use ./gradlew build to build the project

  • More info about Gradle wrapper -http://www.gradle.org/docs/current/userguide/gradle_wrapper.html

senz module build.gradle file

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard false
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

        debug {
            debuggable true
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:5.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
  • Few interesting thing in here

apply plugin: 'android'

  • It applies the Android plugin for Gradle to this build

Android specific build option

  • android {...} configures all the Android-specific build options

  • It specifies SDK versions, release/debug build configurations etc..

Dependencies

  • The dependencies element is outside and after the android element

  • This element declares the dependencies for this module

Sync the project with Android Studio

  • Open Android Studio and import the SenZors project. It will sync gradle android project

  • Make sure to tick use default gradle wrapper option in Android Studio preferences

Picture

  • Finalised project directory structure would be looks like below

Picture

Build from command line

# build project from command line
./gradlew build

# list all available gradle tasks 
./gradlew tasks

References

  1. http://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/
  2. http://developer.android.com/sdk/installing/studio-build.html
  3. http://spring.io/guides/gs/gradle-android/
  4. http://gmariotti.blogspot.it/2013/10/common-tips-about-gradle.html
  5. http://tools.android.com/tech-docs/new-build-system/user-guide

3 Responses
Add your response

Thanks, this post really helped me pursuing my goal of migrating adt source to gradle..
Really appreciate your style of braking things down...

over 1 year ago ·

Nice post.. i've tried many articles but failed. can i discuss it with you please ? thanks

over 1 year ago ·

Hey, Excellent work. I must appreciate your work the content is really awesome clear and concise. you are really doing a great work, carry on the hard work.

over 1 year ago ·