Adding Local .Aar Files to Gradle Build Using "Flatdirs" Is Not Working

Adding local .aar files to Gradle build using flatDirs is not working

Building upon Josiah's answer, here's how I got it to work.

Following his instructions (under edit) (File -> New-> New Module -> Import .JAR/.AAR) and import your .AAR.

Then in your project build.gradle (not the top level one, the one under 'app') add the following (in the dependencies section):

dependencies {
compile project(':Name-Of-Your-Project')
}

Note Name-Of-Your-Project should match the name of the folder that was added after you imported the AAR file (at the same level as app/.idea under the top most level folder). Or to put it another way...


MyApplication
.idea
app
build.gradle (here's where to add compile project(':ProjectName') to dependency section)
ProjectName (added automatically after importing, matching the name of your aar file)
build
gradle
etc

This worked for me running Android Studio 0.8.0. Don't forget to synchronize gradle (using toolbar button or in File->Synchronize) after you do this.

(Thanks to Josiah for getting me going in the right direction)

(Note: prior to this I tried adding it to the libs folder, trying to manipulate the top level build.gradle and the app level build.gradle, but none of that worked for my aars files--jar's will work fine, but not the aar files)

Adding local .aar files to my gradle build

You put your flatDir block in the wrong repostories block. The repositories block inside buildscript tells Gradle where to find the Android-Gradle plugin, but not the rest of the dependencies. You need to have another top-level repositories block like this:

repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}

I tested this and it works okay on my setup.

Error building Android library: Direct local .aar file dependencies are not supported

I recently encountered the same issue, the fix was to remove the library from libs/ and import it using File -> New -> New Module -> Import .JAR/.AAR Package, then referencing it in the library module build.gradle file:

dependencies {
implementation project(":imported_aar_module")
}

If you are on a newer Android Studio version (4.0.0+), this option is not available. Instead you have to do it manually.

  1. Create a new directory and put the following content into the build.gradle file withing the new directory:
configurations.maybeCreate("default")
artifacts.add("default", file('[nameOfTheAar].aar'))

  1. Place the aar into this new directoy. Next to the build.gradle file.
  2. Add the new created Gradle project to the settings.gradle file:
include(":pathToTheCreatedDirectory")

  1. Include the project in your library where you want to use the aar:
implementation project(":pathToTheCreatedDirectory", configuration = "default")

How to manually include external aar package using Gradle for Android

Please follow below steps to get it working ( I have tested it up to Android Studio 2.2)

Lets say you have kept aar file in libs folder. ( assume file name is cards.aar )

then in app build.gradle specify following and click sync project with Gradle files.
Open Project level build.gradle and add flatDir{dirs 'libs'} like did below

allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}

and now open app level build.grdle file and add .aar file

    dependencies {
implementation(name:'cards', ext:'aar')
}

If everything goes well you will see library entry is made in build -> exploded-aar

Also note that if you are importing a .aar file from another project that has dependencies you'll need to include these in your build.gradle, too.



Related Topics



Leave a reply



Submit