Create an Android library

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. Unlike JAR files, AAR files offer the following functionality for Android applications:

  • AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
  • AAR files can contain C/C++ libraries for use by the app module's C/C++ code.

A library module is useful in the following situations:

  • When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
  • When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.

In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module. This page teaches you how to do both.

Create a library module

To create a new library module in your project, proceed as follows:

  1. Click File > New > New Module.
  2. In the Create New Module window that appears, click Android Library, then click Next.

    There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects— especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.

  3. Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

Once the Gradle project sync completes, the library module appears in the Project panel on the left. If you don't see the new module folder, make sure it's displaying the Android view.

Convert an app module to a library module

If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:

  1. Open the module-level build.gradle file.
  2. Delete the line for the applicationId. Only an Android app module can define this.
  3. At the top of the file, you should see the following:

    Groovy

    implementation project(path: ':example-library')
    

    Kotlin

      plugins {
          id("com.android.application")
      }
      

    Groovy

      apply plugin: 'com.android.application'

    Change it to the following:

    Groovy

      apply plugin: 'com.android.library'
      

    Kotlin

      plugins {
          id("com.android.library")
      }
      
  4. Save the file and click File > Sync Project with Gradle Files.

That's it. The entire structure of the module remains the same, but it now operates as an Android library and the build will now create an AAR file instead of an APK.

When you want to build the AAR file, select the library module in the Project window and then click Build > Build APK.

Adding dependencies with the Project Structure Dialog

Use your library from within the same project

To use your new Android library's code in another app or library module within the same project, add a project-level dependency:

  1. Navigate to File > Project Structure > Dependencies.
  2. Select the Module in which you’ll use the library.
  3. In the Declared Dependencies tab, click and select Module Dependency in the dropdown.

  4. In the Add Module Dependency dialog, select your library module.

    Add module dependency in the Project Structure Dialog

  5. Select the configuration that requires this dependency, or select "implementation" if it applies to all configurations, and click OK.

Studio will edit your modules’s build.gradle file to add the dependency of the form: