An Android library 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.
Create a library module
- Click File > New > New Module.
- 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.
- Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Add your library as a dependency
Add the library to your project in either of the following ways (if you created the library module within the same project, then it’s already there and you can skip this step):
- Add the compiled AAR (or JAR) file (the library must be already built):
- Click File > New > New Module.
- Click Import .JAR/.AAR Package then click Next.
- Enter the location of the compiled AAR or JAR file then click Finish.
- Import the library module to your project (the library source becomes part of your project):
- Click File > New > Import Module.
- Enter the location of the library module directory then click Finish.
Make sure the library is listed at the top of your settings.gradle
file, as shown here for a library named “my-library-module”:
1 |
include ':app', ':my-library-module' |
Open the app module’s build.gradle
file and add a new line to the dependencies
block as shown in the following snippet:
1 2 3 |
dependencies { compile project(":my-library-module") } |
In this example above, the compile configuration adds the library named my-library-module as a build dependency for the entire app module.
References:-> https://developer.android.com/studio/projects/android-library.html