Android Project Structure-Part 2

On the previous post we briefly discussed about project structure. Now let’s look into Gradle Scripts .

 

Gradle:
Gradle is a build system where you write script in groovy language to get a work done such as compiling module in your application.Which means it take cares all the work for building an .apk file like your bike engine. The latest version of the gradle is 2.13.

Project_Sturture

In the above image you can see that there are two gradle files.

  1. build.gradle(Project: TestApplication)
  2. build.gradle(Module: app)

The first one’s source code looks something like this :

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        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
}

There’s something called jcenter()  inside of repositories which is a central repository of almost all third party lib. From there you fetch the lib to your project. In eclipse days we always have to download  .jar files from somewhere and include it to our projects. Now Google made our work easier you will just specify the name of the lib it will fetch it and includes for you. The including part is in second gradle file.

It looks something like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "test.gtm.com.sample"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

apply plugin: ‘com.android.application’  :  Which specifies that your module is an android project or library module. If it is lib it should be : apply plugin: ‘com.android.library’

And you specify the compileSdkVersion: which specifies on which version of sdk you want to compile the code. And buildToolsVersion: which specifies on which version of compilers you want to use.

Gradle build will be unsuccessful if you’re trying to build without compileSdkVersion and  buildToolsVersion. 

defaultConfig {
applicationId “test.gtm.com.sample” // Is your applicationId used for generating different flavours of build. 
minSdkVersion 11 
targetSdkVersion 23
versionCode 1
versionName “1.0”
}

So applicationId is very important to ship your application to the playstore.And it should be unique.
dependencies {
compile ‘com.android.support:appcompat-v7:23.4.0’
compile ‘com.google.code.gson:gson:2.2.4’
compile ‘com.squareup.picasso:picasso:2.3.2’
}

Dependencies: 

You write a single line of code which delivers you a lib which is written by geeks.How simple is that :), beauty of using android studio and gradle.

compile ‘com.squareup.picasso:picasso:2.3.2’

The above line fetchs the picasso lib for you from jcenter to your lonccal working system.Also you can include your own modules here.

This is all about gradle for now !

Sources :
https://developer.android.com/studio/build/index.html
http://stackoverflow.com/a/24828666
http://gradle.org/

“Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart “- Steve Jobs

Happy coding !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s