Tutorial #6 - Adding a Library

We will want to use some third-party libraries in our project, to ease development of the app:

Right now, we will just focus on arranging for our project to be able to use the libraries. Later in the book, we will actually put the libraries to use.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository:

Step #1: Getting Rid of Existing Cruft

If you look at the app/build.gradle file, you will see that we were given some dependencies automatically, when the project was created:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
}

(from EmPubLite-AndroidStudio/T5-Layout/EmPubLite/app/build.gradle)

We are not going to use any of those:

However, not only did the new-project wizard generate these dependencies for us, but it also code-generated some do-nothing test code that depends upon these dependencies. So, we will leave those two test dependencies alone, as it is simpler to ignore them than it is to clean that part up.

But go ahead and delete the compile fileTree statement, leaving you with


dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
}

You may get a yellow banner at the top of the editor, indicating that a “project sync” is requested. Ignore that for the moment, as we will be making more changes to this file.

Step #2: Requesting New Dependencies

Many of the dependencies we are going to set up now are available from JCenter, and our project is already set up to pull from there. However, the CWAC-Security library is not, and so we will need to teach Gradle how to find that library.

To do that, add the following code to your app/build.gradle file, above the dependencies closure:

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}

(from EmPubLite-AndroidStudio/T6-Library/EmPubLite/app/build.gradle)

Then, add seven more lines to the dependencies closure, identifying the libraries that we need:

dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.commonsware.cwac:security:0.8.0'
    compile 'com.android.support:support-v13:24.2.1'
    compile 'io.karim:materialtabs:2.0.5'
}

(from EmPubLite-AndroidStudio/T6-Library/EmPubLite/app/build.gradle)

At this point, your app/build.gradle file should look something like:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.commonsware.empublite"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}

dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.commonsware.cwac:security:0.8.0'
    compile 'com.android.support:support-v13:24.2.1'
    compile 'io.karim:materialtabs:2.0.5'
}

(from EmPubLite-AndroidStudio/T6-Library/EmPubLite/app/build.gradle)

If that “project sync” yellow banner is at the top of the editor, click the “Sync Now” link in that banner to synchronize the *.iml files with the changes you made to this build.gradle file. If you do not, choose Tools > Android > Sync Project with Gradle Files to force that resync.

In Our Next Episode…

… we will configure the action bar on our tutorial project