Android App Development

Introduction of Gradle in Android

Gradle is a build tool with a focus on whole project automation and support for a multi-language development. It was created in 2008.

It was implemented in Java and it is very popular because of its opensource compatibility.

Before we started using Android Studio, we have used Eclipse for Android app development purpose.

The gradle build system is designed to support complex scenarios while creating Android applications like :

  1. Multi-distribution : The application must be customized for multiple clients
  2. Multi-application : Supporting the creation of multiple APKs for different devices while reusing the code.

While running any applicaiton, it triggers the appropriate Gradle task for for that particular application and starts the applicaiton after building of Gradle.

The Android Studio plug-ins allows Gradle tasks to be initiated and managed from within the Android Studio. The Gradle command-line wrapper can be used to build Android Studio based projects.

Example in RealTime

Example : A module within Android Studio project which triggers an intent to load another module in the project. The first module is dependent on the second module since the application will fail to build if the second module cannot be located and launched at runtime.

This dependency can be declared in the Gradle.build file for the first module so the second module is included in the application build.

Other example of dependencies are libraries and JAR files on which the project depends in order to compile and run.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

Classes & Methods

There are two types of Gradle files in your application : for Project and for particular Module.

build.gradle(Project:MyApplication)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
	  //classpath for gradle tools version
        classpath 'com.android.tools.build:gradle:2.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Module:app)

apply plugin: 'com.android.application'
android {
    //SDK Version for compiling the file
    compileSdkVersion 23
    //Building tools version
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "acadgild.com.myapplication"
         //minimum SDK Version of app
        minSdkVersion 15
	  //Targeted SDK Version of app
        targetSdkVersion 23
         //version code of app
        versionCode 1
         //version name of app
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
//dependencies which are using for compiling libraries and other things used for testing and library support
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}
settings.gradle(Project Setttings)
include ':app'

Shortcut for Update Gradle

If you update any gradle files, it is recommended to sync project with Gradle file option.

Dependecies

Another functionality of Gradle is that of dependencies.

You can add library files and compile them for dependencies. Here the compile of testing number has been given to junit version name as ‘junit:junit:4.12’. You can compile your project applicaiton using v4 and v7 with their verisons.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

Adding Project Dependencies

1. If you want to use user interface to fill out the dependcies, you can choose do the following :

a) Right click on the module(app)

b) Choose Open Module Settings

2. The Dialog Box will open as below:

3. Choose Dependenices Tab from above Tabs.

4. Now to add a dependencies, Simply click on + Button.

5. Let’s click on any dependency you want to add dependency. For example, here I am adding Joda Time dependency.

6. If you click on OK the dependency will be added like below:

dependencies {
 compile fileTree(dir: ‘libs’, include: [‘*.jar’])
 compile ‘com.android.support:appcompat-v7:21.0.3’
 compile ‘joda-time:joda-time:2.3’

Note : Remember to always Sync Project with Gradle Files to kick off the build.

Support libraries : v4 and v7

V4 library : This library is designed to be used with Android 1.6 and higher. It includes the largest set of APIs compared to the other libraries, including support for application components, user interface features, accessibility, data handling, network connectivity, and programming utilities.

App Components :

  1. Fragments
  2. NotificationCompat
  3. LocalBroadcastManager

V7 library : There are several libraries designed to be used with Android 2.1 and higher. These libraries provide specific feature sets and can be included in your application independently from others.

App Components :

  1. ActionBar
  2. AppCompatActivity
  3. AppCompatDialog
  4. ShareActionProvider

Conclusion

For the most part, Android Studio performs application builds in the background without any intervention from the developers. This build process is handled using the Gradle system, an automated build toolkit designed to allow the ways in which projects are build to be configured and managed through a set of build configuration files.

Gradle builds system and configuration files within the context of an Android Studio project.

Keep visiting our site www.acadgild.com for more updates on Android and other technologies.

Hope this blog was informative enough on the Introduction to Gradle in Android.

Android Programming

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close