Import .AAR/.JAR in Android Studio

Import .AAR/.JAR in Android Studio

Manually Importing .AAR/.JAR Packages in Android Studio: A Step-by-Step Guide

ยท

3 min read

In Android development, integrating external libraries can significantly enhance the functionality and efficiency of our application. One common method to include external dependencies is by using AAR/JAR (Android Archive) packages.

While Android Studio offers seamless integration for many libraries through Gradle, there are scenarios where we might need to manually import AAR/JAR packages, especially in newer versions of Android Studio where the "Import .JAR/.AAR Package" option is not available.

In this article, we'll explore the step-by-step process of manually importing AAR/JAR packages into our Android project.

Background:

The traditional approach of placing AAR/JAR files in the libs directory and referencing them in the build.gradle file has been deprecated in newer versions of Android Studio.

Instead, developers now need to import AAR/JAR packages manually or utilize the "Import .JAR/.AAR Package" option if available.

Old Approach:

  1. Prepare the AAR/JAR Package:

Before starting, ensure we have the AAR/JAR package you wish to include in our project. This could be obtained from various sources such as third-party libraries or our own modules.

  1. Create a New Module:

In Android Studio, create a new module for the AAR/JAR package:

  • Go to File -> New -> New Module.

  • Choose Import .JAR/.AAR Package.

  • Navigate to the location of your AAR/JAR package and select it.

  • Reference the Module in build.gradle

  1. Once the module is imported, we need to reference it in our project's build.gradle file:
dependencies {
  implementation project(":imported_aar_module")
}

Replace "imported_aar_module" with the name of the module we imported. This step is essential for integrating the functionality provided by the AAR/JAR package into our project.

New Approach (100% Working):

If the "Import .JAR/.AAR Package" option is not available or we prefer a manual approach, follow these steps:

  1. Create a New Directory: Start by creating a new directory within our project where we'll store the AAR/JAR package.

  2. AddGradleConfiguration: Inside the new directory, create a build.gradle file with the following content:

     configurations.maybeCreate("default")
     artifacts.add("default", file('[nameOfTheAar].aar'))
    

    Replace [nameOfTheAar] with the name of our AAR/JAR file.

  3. Place AAR Package: Move the AAR/JAR package into the newly created directory alongside the build.gradle file.

  4. Modifysettings.gradle: Include the newly created Gradle project in our settings.gradle file:

     include(":pathToTheCreatedDirectory")
    

    Replace "pathToTheCreatedDirectory" with the actual path to our newly created directory.

  5. Reference inbuild.gradle: Finally, include the project in our library module's build.gradle file where we want to use the AAR/JAR package:

     implementation project(":pathToTheCreatedDirectory", configuration = "default")
    

    Replace "pathToTheCreatedDirectory" with the actual path to our newly created directory.

Conclusion:

Manually importing AAR/JAR packages into your Android project provides a solution for scenarios where the traditional method is deprecated or unavailable. By following the steps outlined in this article, we can seamlessly integrate external dependencies, extending the functionality of our Android applications.

Remember to regularly check for updates and changes in Android Studio's features and documentation to ensure we're using the most efficient and recommended practices for our Android development projects.


That's it for today. Happy Coding...

ย