
Importing AAR in Kotlin build.gradle.kts
Introduction
This article provides a detailed guide on how to create a new module in a Kotlin project to handle AAR (Android Archive) libraries. It covers the steps to set up the module, delete unnecessary files, configure the Gradle files (setting.gradle.kts and build.gradle.kts) to import the AAR dependencies, and compares two different approaches to module management.
Steps to Create a New Module for AAR Libraries
Step 1:
- Create a new library module, for example,
:libmodule in your Kotlin project.
Step 2:
- ForeIn the newly created
:libmodule, delete the/srcfolder. Since we dont need this/srcfolder or create the source code in this folder.
Step 3:
- Create a new folder named
sdkor whatever folder name you like for the aar SDK file and make it inside the:libmodule. Place the AAR files that you want to reference in this folder.
Step 4:
- Copy the
:libmodule’s originalbuild.gradle.ktsfile into thissdkfolder and add the following commands:
configurations.maybeCreate("default")
artifacts.add("default", file("sdk-release.aar"))
Step 5:
Open the
setting.gradle.ktsfile in your project. Find the line that includes the:libmodule, which should look like this:include(":lib")Modify this line to include the “sdk” subfolder:
include(":lib:sdk")
In the
build.gradle.ktsfile of the module where you want to use the AAR libraries, add the following dependencies:implementation(project(":lib:sdk"))
Alternative Approach
If creating a new module seems cumbersome, you can create a new folder directly in the project’s root directory and configure it in the setting.gradle.kts file.