[android] Adding local .aar files to Gradle build using "flatDirs" is not working

I'm aware of this question: Adding local .aar files to my gradle build but the solution does not work for me.

I tried adding this statement to the top level of my build.gradle file:

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

I've also put the slidingmenu.aar file into /libs and referenced it in the dependencies section: compile 'com.slidingmenu.lib:slidingmenu:1.0.0@aar' but it did not work at all.

I tried compile files('libs/slidingmenu.aar') as well but with no luck.

What am I missing? Any ideas?

P.S. Android Studio 0.8.2

The answer is


Add below in app gradle file implementation project(path: ':project name')


This solution is working with Android Studio 4.0.1.

Apart from creating a new module as suggested in above solution, you can try this solution.

If you have multiple modules in your application and want to add aar to just one of the module then this solution come handy.

In your root project build.gradle

add

repositories {
flatDir {
    dirs 'libs'
}}

Then in the module where you want to add the .aar file locally. simply add below lines of code.

dependencies {
api fileTree(include: ['*.aar'], dir: 'libs')
implementation files('libs/<yourAarName>.aar')

}

Happy Coding :)


Update : As @amram99 mentioned, the issue has been fixed as of the release of Android Studio v1.3.

Tested and verified with below specifications

  • Android Studio v1.3
  • gradle plugin v1.2.3
  • Gradle v2.4

What works now

  • Now you can import a local aar file via the File>New>New Module>Import .JAR/.AAR Package option in Android Studio v1.3

  • However the below answer holds true and effective irrespective of the Android Studio changes as this is based of gradle scripting.


Old Answer : In a recent update the people at android broke the inclusion of local aar files via the Android Studio's add new module menu option. Check the Issue listed here. Irrespective of anything that goes in and out of IDE's feature list , the below method works when it comes to working with local aar files.(Tested it today):

Put the aar file in the libs directory (create it if needed), then, add the following code in your build.gradle :

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }
repositories{
      flatDir{
              dirs 'libs'
       }
 }

This line includes all aar and jar files from libs folder:

implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs/')

This is my structure, and how I solve this:

MyProject/app/libs/mylib-1.0.0.aar

MyProject/app/myModulesFolder/myLibXYZ

On build.gradle

from Project/app/myModulesFolder/myLibXYZ

I have put this:

repositories {
   flatDir {
    dirs 'libs', '../../libs'
  }
}
compile (name: 'mylib-1.0.0', ext: 'aar')

Done and working fine, my submodule XYZ depends on somelibrary from main module.


Edit: The correct way (currently) to use a local AAR file as a build dependency is to use the module import wizard (File | New Module | Import .JAR or .AAR package) which will automatically add the .aar as a library module in your project.

Old Answer

Try this:

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

...

compile(name:'slidingmenu', ext:'aar')

For anyone who has this problem as of Android Studio 1.4, I got it to work by creating a module within the project that contains 2 things.

  1. build.gradle with the following contents:

    configurations.create("default")

    artifacts.add("default", file('facebook-android-sdk-4.7.0.aar'))

  2. the aar file (in this example 'facebook-android-sdk-4.7.0.aar')

Then include the new library as a module dependency. Now you can use a built aar without including the sources within the project.

Credit to Facebook for this hack. I found the solution while integrating the Android SDK into a project.


If you already use Kotlin Gradle DSL, the alternative to using it this way:

Here's my project structure

|-root
|----- app
|--------- libs // I choose to store the aar here
|-------------- my-libs-01.aar
|-------------- my-libs-02.jar
|--------- build.gradle.kts // app module gradle
|----- common-libs // another aar folder/directory
|----------------- common-libs-01.aar
|----------------- common-libs-02.jar
|----- build.gradle.kts // root gradle

My app/build.gradle.kts

  1. Using simple approach with fileTree
// android related config above omitted...

dependencies {
    // you can do this to include everything in the both directory
    // Inside ./root/common-libs & ./root/app/libs
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
    implementation(fileTree(mapOf("dir" to "../common-libs", "include" to listOf("*.jar", "*.aar"))))
}
  1. Using same approach like fetching from local / remote maven repository with flatDirs
// android related config above omitted...

repositories {
    flatDir {
        dirs = mutableSetOf(File("libs"), File("../common-libs") 
    }
}

dependencies {
   implementation(group = "", name = "my-libs-01", ext = "aar")
   implementation(group = "", name = "my-libs-02", ext = "jar")

   implementation(group = "", name = "common-libs-01", ext = "aar")
   implementation(group = "", name = "common-libs-02", ext = "jar")
}

The group was needed, due to its mandatory (not optional/has default value) in kotlin implementation, see below:

// Filename: ReleaseImplementationConfigurationAccessors.kt
package org.gradle.kotlin.dsl

fun DependencyHandler.`releaseImplementation`(
    group: String,
    name: String,
    version: String? = null,
    configuration: String? = null,
    classifier: String? = null,
    ext: String? = null,
    dependencyConfiguration: Action<ExternalModuleDependency>? = null
)

Disclaimer: The difference using no.1 & flatDirs no.2 approach, I still don't know much, you might want to edit/comment to this answer.

References:

  1. https://stackoverflow.com/a/56828958/3763032
  2. https://github.com/gradle/gradle/issues/9272

In my case, I just put the aar file in libs, and add

dependencies { ... api fileTree(dir: 'libs', include: ['*.aar']) ... }

in build.gradle and it works. I think it is similar with default generated dependency:

implementation fileTree(dir: 'libs', include: ['*.jar'])


The easiest way now is to add it as a module

enter image description here

This will create a new module containing the aar file, so you just need to include that module as a dependency afterwards


This solution is working with Android Studio 4.0.1.

Apart from creating a new module as suggested in above solution, you can try this solution.

If you have multiple modules in your application and want to add aar to just one of the module then this solution come handy.

In your root project build.gradle

add

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

Then in the module where you want to add the .aar file locally. simply add below lines of code.

dependencies {
api fileTree(include: ['*.aar'], dir: 'libs')
implementation files('libs/<yourAarName>.aar')

}

Happy Coding :)


In my case the none of the answers above worked! since I had different productFlavors just adding repositories{ flatDir{ dirs 'libs' } }

did not work! I ended up with specifying exact location of libs directory:

repositories{
flatDir{
    dirs 'src/main/libs'
}

}

Guess one should introduce flatDirs like this when there's different productFlavors in build.gradle


I got this working on Android Studio 2.1. I have a module called "Native_Ads" which is shared across multiple projects.

First, I created a directory in my Native_ads module with the name 'aars' and then put the aar file in there.

Directory structure:

libs/
aars/    <-- newly created
src/
build.gradle
etc

Then, the other changes:

Top level Gradle file:

allprojects {
    repositories {
        jcenter()
        // For module with aar file in it
        flatDir {
            dirs project(':Native_Ads').file('aars')
        }
    }
}

App module's build.gradle file: - no changes

Settings.gradle file (to include the module):

include ':app'
include 'Native_Ads'
project(':Native_Ads').projectDir = new File(rootProject.projectDir, '../path/to/Native_Ads')

Gradle file for the Native_Ads module:

repositories {
    jcenter()
    flatDir {
        dirs 'aars'
    }
}
dependencies {
    compile(name:'aar_file_name_without_aar_extension', ext:'aar')
}

That's it. Clean and build.


You can do it this way. It needs to go in the maven format:

repositories {
  maven { url uri('folderName')}
}

And then your AAR needs to go in a folder structure for a group id "com.example":

folderName/
  com/
    example/
       verion/
          myaar-version.aar

Then reference as a dependency:

compile 'com.example:myaar:version@aar'

Where version is the version of your aar file (ie, 3.0, etc)


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-studio

A failure occurred while executing com.android.build.gradle.internal.tasks "Failed to install the following Android SDK packages as some licences have not been accepted" error Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util This version of Android Studio cannot open this project, please retry with Android Studio 3.4 or newer WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Flutter plugin not installed error;. When running flutter doctor ADB.exe is obsolete and has serious performance problems Android design support library for API 28 (P) not working Flutter command not found How to find the path of Flutter SDK

Examples related to android-gradle-plugin

WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Android Material and appcompat Manifest merger failed Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries Invoke-customs are only supported starting with android 0 --min-api 26 error: resource android:attr/fontVariationSettings not found Exception : AAPT2 error: check logs for details Could not resolve com.android.support:appcompat-v7:26.1.0 in Android Studio new project Failed to resolve: com.android.support:appcompat-v7:27.+ (Dependency Error)

Examples related to build.gradle

Android Material and appcompat Manifest merger failed Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve Android dependency has different version for the compile and runtime Setting up Gradle for api 26 (Android) What's the difference between implementation and compile in Gradle? More than one file was found with OS independent path 'META-INF/LICENSE' Android Studio - Failed to notify project evaluation listener error Gradle error: Minimum supported Gradle version is 3.3. Current version is 3.2 All com.android.support libraries must use the exact same version specification DELETE_FAILED_INTERNAL_ERROR Error while Installing APK

Examples related to aar

Adding local .aar files to Gradle build using "flatDirs" is not working Create aar file in Android Studio How to convert AAR to JAR