Create an Android library (original) (raw)

An Android library is structurally the same as an Android app module. It includes 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 apps:

A library module is useful in the following situations:

In either case, move the files you want to reuse into a library module and then add the library as a dependency for each app module.

This page explains how to create and use an Android library module. For guidance on how to publish a library, seePublish your library

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 dialog that appears, click Android Library, then click Next.
    There's also an option to create a Kotlin or 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 doesn't let you include Android resources or manifest files, which is very useful for code reuse in Android projects. 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 pane. If you don't see the new module folder, make sure the pane is displaying the Android view.

Convert an app module to a library module

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

  1. Open the module-level build.gradle file, if you're using Groovy, or the build.gradle.kts file, if you're using Kotlin script.
  2. Delete the line for the applicationId. Only an Android app module can define this.
  3. Find the `plugins` block at the top of the file that looks like this:

Groovy

plugins {
id 'com.android.application'
}

Kotlin

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

Change it to the following:

Groovy

plugins {
id 'com.android.library'
}

Kotlin

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

The structure of the module remains the same, but it now operates as an Android library. The build creates an AAR file instead of an APK.

When you want to build the AAR file, select the library module in theProject window and clickBuild > Build APK.

Add dependencies with the Project Structure dialog

You can use the Project Structure dialog to add dependencies to your project. The following sections describe how to use the dialog to add dependencies.

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 that you want to add the library.
  3. In the Declared Dependencies tab, click and selectModule Dependency from the menu.
  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 selectimplementation if it applies to all configurations, then click OK.

Android Studio edits your module's build.gradle or build.gradle.kts file to add the dependency, in the following form:

Groovy

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

Kotlin

implementation(project(":example-library"))

Use your library in other projects

The recommended way to share dependencies (JARs and AARs) is with a Maven repository, either hosted on a service, such asMaven Central, or with a directory structure on your local disk. For more information on using Maven repositories, see Remote repositories.

When an Android library is published to a Maven repository, metadata is included so that the dependencies of the library are included in the consuming build. This lets the library be automatically deduplicated if it is used in multiple places.

To use your Android library's code in another app module in a different project, proceed as follows:

  1. Navigate toFile >Project Structure >Dependencies.
  2. In the Declared Dependencies tab, click and selectLibrary Dependency in the menu.
  3. In the Add Library Dependency dialog, use the search box to find the library to add. This form searches the repositories specified in the in the dependencyResolutionManagement { repositories {...}} block in thesettings.gradle or settings.gradle.kts file.
    Add library dependency in the Project Structure  
Dialog
  4. Select the configuration that requires this dependency or selectimplementation if it applies to all configurations, then click OK.

Check your app’s build.gradle or build.gradle.kts file to confirm that a declaration similar to the following appears (depending on the build configuration you've selected):

Groovy

implementation 'com.example:examplelibrary:1.0.0'

Kotlin

implementation("com.example:examplelibrary:1.0.0")

Add your AAR or JAR as a dependency

To use your Android library's code in another app module, proceed as follows:

  1. Navigate to File >Project Structure >Dependencies.
  2. In the Declared Dependencies tab, click and select Jar Dependency in the menu.
  3. In the Add Jar/Aar Dependency dialog, enter the path to your AAR or JAR file, then select the configuration to which the dependency applies. If the library should be available to all configurations, select theimplementation configuration.
    Add AAR dependency in the Project Structure  
Dialog
    Check your app’s build.gradle or build.gradle.kts file to confirm that a declaration similar to the following appears (depending on the build configuration you've selected):

Groovy

implementation files('my_path/my_lib.aar')

Kotlin

implementation(files("my_path/my_lib.aar"))

To import a dependency on the Gradle build running outside of Android Studio, add a path to the dependency in your app’s build.gradle or build.gradle.kts file. For example:

Groovy

dependencies { implementation fileTree(dir: "libs", include: [".jar", ".aar"]) }

Kotlin

dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to listOf(".jar", ".aar")))) }

For more about adding Gradle dependencies, seeAdd build dependencies.

Declare a public resource

Resources include all files in your project’s res/ directory, such as images. All resources in a library default to public. To make all resources implicitly private, you must define at least one specific attribute as public.

To declare a public resource, add a <public> declaration to your library’s public.xml file. If you haven't added public resources before, you need to create the public.xml file in theres/values/ directory of your library.

The following example code creates two public string resources with the names mylib_app_name and mylib_public_string:

To prevent users of your library from accessing resources intended only for internal use, use this automatic private designation mechanism by declaring one or more public resources. Alternately, you can make all resources private by adding an empty <public /> tag. This marks nothing as public and makes all resources private.

Any resources that you want to remain visible to developers using your library should be made public.

Implicitly making attributes private prevents users of your library from receiving code completion suggestions from internal library resources and lets users rename or remove private resources without breaking clients of your library. Private resources are filtered out of code completion, and the lint tool warns you when you try to reference a private resource.

When building a library, the Android Gradle plugin gets the public resource definitions and extracts them into the public.txt file, which is then packaged inside the AAR file.

Development considerations for library modules

As you develop your library modules and dependent apps, be aware of the following behaviors and limitations.

Groovy

android {
defaultConfig {
consumerProguardFiles 'lib-proguard-rules.txt'
}
...
}

Kotlin

android {
defaultConfig {
consumerProguardFiles("lib-proguard-rules.txt")
}
...
}
However, if your library module is a part of a multi-module build that compiles into an APK and doesn't generate an AAR, run code shrinking on only the app module that consumes the library. To learn more about ProGuard rules and their usage, readShrink, obfuscate, and optimize your app.

Anatomy of an AAR file

The file extension for an AAR file is .aar, and the Maven artifact type isaar as well. The file itself is a ZIP file. The only mandatory entry is/AndroidManifest.xml.

An AAR file can also include one or more of the following optional entries: