← All articles

Using AAR Files in Capacitor Android Plugins

Add an AAR distributed outside Maven as a local Gradle module in a Capacitor Android project so users can install it after accepting the license.

Published
Using AAR Files in Capacitor Android Plugins cover image

Modern Android libraries usually come from Maven repositories. Put the package name and version in implementation and Gradle fetches it—very convenient.

dependencies {
    implementation 'com.example:library-name:1.0.0'
}

Some vendors still ship an AAR as a zip and say "add this to your project." Android Studio used to have a UI for adding AAR files via File → New → Import Module, but that UI changed and target modules are harder to pick, so here is the manual approach.

I first considered bundling the AAR inside the plugin repo so users would not configure anything. Many zip-distributed AARs require agreeing to terms and downloading yourself, so this article covers users adding the AAR after installing the plugin.

Assume the AAR file is named ExampleLibrary.aar.

  1. Add the AAR to the Capacitor project
  • android/ExampleLibrary/ExampleLibrary.aar
  • android/ExampleLibrary/build.gradle

ExampleLibrary can be any name. Create a folder, put the AAR and a build.gradle file there.

  1. Add the following to build.gradle
configurations.maybeCreate("default")
artifacts.add("default", file('ExampleLibrary.aar'))

That creates the ExampleLibrary module. Next, wire it into the project. Open android/settings.gradle and add:

gradle
+ include ':ExampleLibrary'
+ project(':ExampleLibrary').projectDir = new File('./ExampleLibrary/')

Now the project and plugins can reference the ExampleLibrary module from ExampleLibrary.aar. Simple.

See you next time.