Lesson Wednesday

Java programmers all over the world have created tools that aren't included in the standard Java library. These tools provide additional functionality, or ways to make the development process easier. Similar to the way we import additional packages from Java into our projects, we can also import entire collections of tools!


When we write a program that requires an outside tool or library, that outside resource is known as a dependency, because your program depends on it to work. This term is used in many programming languages, not just Java. A project can have anywhere from a one dependency, to hundreds, even thousands!

Gradle and build.gradle

But how do we "get" a dependency into our project? And once it's there, how do we use it? Thankfully, there are tools to help with this. One of the most common is called Gradle. We've been using Gradle since we started using IntelliJ.

Gradle is a program that manages a project's dependencies. More specifically, it's an open source build automation tool. This sounds complicated, but it's actually fairly simple to use! Gradle works like this: You provide a list of dependencies your project needs. Then Gradle locates, downloads, and installs them into your project. It will also compile our projects for us. Simple as that!

Take a look at any IntelliJ project you have available, and you'll see a file called build.gradle. Open it, and you'll see relevant information to your project. There isn't much in here at the moment, but as our programs grow in complexity, we'll be adding more code here.

Here is a build.gradle from our Car Dealership app:

group 'car-dealership'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {

dependencies {
   testCompile group: 'junit', name: 'junit', version: '4.12'

Let's walk through this file together:

  • apply plugin: Plugins are prebuilt Gradle extensions that provide access to more options. The java plugin gives Gradle access to tools it requires to build Java projects. application provides access to tools that allow it to run Java applications.

  • 1.0-SNAPSHOT is the version of your application. This is correct, since this is our very first iteration of this application.

  • repositories {} tells Gradle where to find dependencies your application uses. mavenCentral() is a giant public repository that contains both dependencies we will be using today. We can view the contents of mavenCentral at The Central Repository website.

  • dependencies {} are the specific packages required by your application. The syntax for adding a dependency is: group: 'groupID', name: 'packageName', version: 'version'. In the example file above, we've already included the JUnit package as a dependency because we'll need it to begin writing and running automated tests.

Here is a much more in-depth build.gradle from an Android project:

You don't have to use Gradle from inside of IntelliJ, but can use it standalone via the command line instead.

Almost all Java developers rely on Gradle to manage their projects, even if they use alternatives to IntelliJ, such as Eclipse (a different IDE) or even Android Studio. They may also use a similar tool called Maven, or a combination of both for different tasks.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    useLibrary 'org.apache.http.legacy'

    repositories {

    defaultConfig {
        applicationId "com.epicodus.myrestaurants"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.robolectric:shadows-support-v4:3.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'

    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'