Adding ActionBarSherlock as a Library into Android Studio

Using maven (similar to NuGet for those of you familiar with the .NET world) and gradle together makes it possible to include libraries in your project without having to manually download the source code and dump it into your project folder. Aside from making it easier to update to newer versions of those libraries as they are released, the imported files don’t clutter up your project folder structure and source repository as much as the complete library source. The downside is that this approach is not appropriate if you intend to modify the source code of the library.

    1. Open your Android SDK Manager and make sure you have the latest version of the Android SDK Platform and Build Tools installed (version 19.0.0 as of this post).
    2. Create or import your project into Android Studio. Now look for the build.gradle file. There is one located in the root of your project, ignore this one. There should be a build.gradle file located in the main module within your project (where your application’s source and resources are).
    3. Modify the build.gradle file you found to include the following, if it already doesn’t:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }
}

That should be it. On your first build you should see the folder: /libraries/ActionBarSherlock/actionbarsherlock/ appear in the root of your project. Any references to ActionBarSherlock should then resolve themselves in your code.

The following steps are also relevant to other libraries available through maven, simply modify the “dependencies” section of the build.gradle.

Share on FacebookShare on Google+Share on RedditTweet about this on TwitterShare on StumbleUponEmail this to someone

Leave a Reply