Android Tutorial, Part 5: Gradle, Dependencies, SDK, and Libraries

Hi all! This is the fifth part of this tutorial series to teach newbies to develop apps for android using its native language Java and the official Android IDE Android Studio. You are free to use and share, and spread this tutorial with or without permission as long as you would link and give attribution to the source material.
If you haven’t seen the previous lessons, please read that first, see my wall.

In the previous lesson, we’ve learned about Intents and made a “link” from our SplashScreenActivity to our DashboardActivity. In this tutorial, we’re going to learn about gradle, how to import dependencies and what are libraries.

Before we start, let’s create another git branch again. Let’s go back to the master branch first then create another branch. You can go to a branch by using the git command checkout. You can do this in Android Studio by clicking the git branches tab on the bottom right corner of Android Studio.



Then create a new branch: VCS > Git > Branches > New Branch.

Let’s name it feature/firebase-integration

We’re switching branches for different features. You might have noticed that your dashboard activity is gone now. Don’t worry, we’ll get back to it later.

So... back to the topic.
What is Gradle? I think this Stack Overflow thread explains it better.

Short Answer
Gradle is a build system.

Long Answer
Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse.

You can do this on the command line, but you have to learn what each tool (dx, aapt) does in the SDK. Eclipse saved us all from these low level but important, fundamental details by giving us their own build system.

Now, have you ever wondered why the res folder is in the same directory as your src folder?
This is where the build system enters the picture. The build system automatically takes all the source files (.java or .xml), then applies the appropriate tool (e.g. takes java class files and converts them to dex files), and groups all of them into one compressed file, our beloved APK.

This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is \src folder) or resources files (in Eclipse it is \res folder).

Now, in order to automate all these tasks, there has to be a script; you can write your own build system using shell scripting in linux or batch files syntax in windows. Got it?

Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM based build system, what that means is that you can write your own script in Java, which Android Studio makes use of.

One cool thing about gradle is that it is a plugin based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR for Java) from sources then you can write a complete plugin in Java or Groovy, and distribute it to rest of world.

Why did Google use it?
Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little to no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.

You must have seen build.gradle file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you write System.out.println("Hello Gradle!"); then it will print on your console.

What can you do in a build script?
A simple example is that you have to copy some files from one directory to another before the actual build process happens. A Gradle build script can do this.



What’s a Software Development Kit (SDK)?
SDK is a set of libraries that work together to provide functionality to accomplish a specific task. On our case we’re using Firebase SDK to provide convenience methods so we can communicate with the Firebase Backend.

So what is a library? This is the definition of a physical library (the one with books): A library is a collection of sources of information and similar resources, made accessible to a defined community for reference or borrowing. - Wikipedia -

I think the definition of the physical library also fits for the definition of the software development library. It is a set of codes that we can borrow to be used on our own software.

Now that we’ve learned about Gradle, SDK, and Libraries. Let’s apply it on our project.
Expand Gradle Scripts from our project tab on the left side of the screen and open our app module gradle script, build.gradle (Module: app)



According to Firebase’s documentation, we should add the Firebase SDK to our dependencies:
Code:
compile 'com.firebase:firebase-client-android:2.5.2+'

But according Google’s recommendation, we should avoid using ‘+’ in our gradle scripts so we’re gonna use this instead:

Code:
compile 'com.firebase:firebase-client-android:2.5.2'

Our dependencies should look like this:
Code:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'

    compile 'com.firebase:firebase-client-android:2.5.2'
}

What if there’s a newer version? Then let’s check if there’s a newer version on
// cannot post links (facebook suddenly doesn't accept links)

Then add this to the android configuration:
Code:
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}

Your android config should look like this:
Code:
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "xyz.teamcatalyst.ratemyisp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}

Now that we’ve added firebase to our dependencies let’s sync our project.


You can press the Blue encircled one or the red encircled one, it will result to the same thing.
Commit everything and push to our current branch, enter the commit message “Add firebase dependencies to gradle”

So now that we’ve learned about Gradle, Dependencies, SDK, and Libraries, and added Firebase SDK to our dependencies, we can now use it to develop our App.

You have just learned how to import libraries and SDK. To learn more, checkout the next tutorial.
 

Top Bottom