How to Add a Library Project to Android Studio

How to Add a Library Project to Android Studio

6 June 2024 Stephan Petzl Leave a comment Tech-Help

Adding a library project to Android Studio can sometimes be a bit confusing, especially for those who are transitioning from the old ADT Eclipse-based bundle. This guide will provide a step-by-step process to help you integrate external libraries into your Android Studio project seamlessly.

Step-by-Step Guide

1. Create a New Project

First, create a new project using the Android Studio creator. For this example, we’ll name our project “HelloWorld”. The initial project structure will look like this:

HelloWorld/
  app/
    - build.gradle  // local Gradle configuration (for app only)
    ...
  - build.gradle // Global Gradle configuration (for whole project)
  - settings.gradle
  - gradle.properties
  ...
  

2. Organize Your Libraries

In the root directory (HelloWorld/), create a new folder named /libs to keep your external libraries organized. This step is optional but helps maintain a cleaner project structure.

3. Add the Library

Copy your library into the newly created /libs folder. For this example, we’ll use the PagerSlidingTabStrip library. After copying, your project structure should look like this:

HelloWorld/
  app/
    - build.gradle  // Local Gradle configuration (for app only)
    ...
  libs/
    PagerSlidingTabStrip/
      - build.gradle // Local Gradle configuration (for library only)
  - build.gradle // Global Gradle configuration (for whole project)
  - settings.gradle
  - gradle.properties
  ...
  

4. Edit settings.gradle

Include your library in the settings.gradle file. If you use a custom path, define the project directory for your library:

include ':app', ':PagerSlidingTabStrip'
project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
  

If you encounter a “Default Configuration” error, use this instead:

include ':app'
include ':libs:PagerSlidingTabStrip'
  

5. Modify app/build.gradle

Add your library project as a dependency in the app/build.gradle file:

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:21.0.3'
  implementation project(":PagerSlidingTabStrip")
}
  

If you followed the alternative step 4, use this configuration instead:

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:21.0.3'
  implementation project(":libs:PagerSlidingTabStrip")
}
  

6. Create build.gradle for the Library (if needed)

If your library project doesn’t have a build.gradle file, create one manually. Here’s an example:

apply plugin: 'com.android.library'

dependencies {
  implementation 'com.android.support:support-v4:21.0.3'
}

android {
  compileSdkVersion 21
  buildToolsVersion "21.1.2"

  defaultConfig {
    minSdkVersion 14
    targetSdkVersion 21
  }

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

7. Synchronize the Project

Finally, click the “Synchronize the project with the Gradle” icon in Android Studio. Your library should now be available in your project.

Additional Resources

For more detailed information, you can refer to our documentation on Virtual Test Devices and Running Test Batches.

Streamlining Your Workflow with Repeato

Integrating libraries can be time-consuming, but tools like Repeato can help streamline your development process. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps efficiently. With features based on computer vision and AI, Repeato enables developers to focus on creating great products while simplifying the test automation process.

Repeato is particularly beneficial for mobile developers, allowing them to delegate test automation tasks to non-technical colleagues or QA teams. This can significantly reduce the time spent on creating and maintaining tests, ensuring your project remains on track.

Like this article? there’s more where that came from!