Importing libraries into Android Studio

Not all Android libraries out there provide a Maven dependency to include it into your project. Sometimes you will just have to resort to the original method of just including the source code in your project.

For this example, I will be importing the SlidingMenu library for Android by Jeremy Feinstein.

First of all, allow me to illustrate the folder structure of your entire project:

importlibrary

To achieve this structure, you will have to create library folder in the root of your project, and import the code library you want to include into it as a module. Note that if the code library you are trying to import doesn’t already have a build.gradle file, you may need to import it into Eclipse first and generate the necessary build.gradle file (as of writing this post, Android Studio is unable to generate build.gradle files for eclipse projects).

Next, based on the naming conventions I established in the image above ensure you have the following:

In your APP’s build.gradle file make sure you have:

dependencies {
    // Your other dependencies go here
    compile project(':libraries:SlidingMenu')
}

This tells Gradle that your App has a dependency on SlidingMenu.

In SLIDING MENU’s build.gradle file make sure it has the following:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}

apply plugin: 'android-library'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']

            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
}

This tells Gradle how to compile and build the Sliding Menu module. We’re telling it that Sliding Menu depends on the Android V4 Support library, the SDK version target to build against, and finally, the folders paths where the source code, resources, and manifest files are located.

Your PROJECT’s settings.gradle file should look like this:

include ":libraries:SlidingMenu", ':App'

This tells Gradle the modules it should build for your entire project, and the order to do it in. Obviously we want to build Sliding Menu before we build our app, because Sliding Menu is a dependency and it expects it  to be already compiled.

In android studio press the Tools -> Android -> Sync Project with Gradle Files button, then rebuild your project. If all went well you should be able to import the com.jeremyfeinstein.slidingmenu.lib.SlidingMenu library into your app’s source files.

2 Comments

  1. Android Studio | Pearltrees · May 20, 2014 Reply

    […] Importing libraries into Android Studio | Crowbar Solutions […]

  2. Fab · March 28, 2021 Reply

    Thanks. BTW, if the library doesn’t have an android compliant structure and has the sources in the “src” directory, one can use another source set by putting this in the build.gradle of the library. This also tells another location for the AndroidManifest.xml

    sourceSets {
    main {
    manifest.srcFile ‘AndroidManifest.xml’
    java.srcDirs = [‘src’]
    res.srcDirs = [‘src’]
    }
    }

    Moreover, if the project doesn’t have an AndroidManifest.xml, one might create a very simple one like this one (replace the package name to the on of the library):

Leave a Reply