178

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

Community
  • 1
  • 1
Alex Dmitriev
  • 3,664
  • 3
  • 29
  • 35
  • 1
    flatDirs + compile(name: 'libname', ext: 'aar') works for me on Android Studio 8.2.0, + gradle 1.12. So it must be something wrong with your build file. Check it one more time or post more code – Than Jul 17 '14 at 08:43

15 Answers15

302

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)

dmSherazi
  • 3,743
  • 5
  • 37
  • 62
Ashton Engberg
  • 5,949
  • 2
  • 19
  • 14
  • 1
    Yikes! This did the trick, thank you for the detailed explanation. Under the hood import master does the same thing as described on the Tools site: "Another option is to create a new Gradle sub-project, and to make this project's published artifact be the jar or aar file that you want to reuse. Then you can simply have other Gradle sub-projects depend on this new sub-project. In this new sub-project, simply create a build.gradle with the following: configurations.create("default") artifacts.add("default", file('somelib.aar'))" – Alex Dmitriev Jul 22 '14 at 21:43
  • I am getting error: `Error:(25, 0) Required keys [path] are missing from map {name=mylibrary-debug}.` if I use `compile project(name:'mylibrary-debug')`.. What am I missing? – MysticMagicϡ Feb 12 '15 at 11:44
  • 2
    The recent gradle plugin broke this. Solution here: http://stackoverflow.com/a/28816265/560600 – Sky Kelsey Mar 06 '15 at 04:38
  • I was able to use this method with Android Studio 1.3, Android gradle plugin v1.2.3, and Gradle version 2.4 – amram99 Aug 07 '15 at 20:20
  • I know this post is kinda old but it's a very good work-around; just a little mistake to edit: "build.gradle (here's where to add compile project(':ProjectName') to dependency section)" --> this one is the top level, the one to edit is (as you were saying few lines before) is the one inside app – MarcinKwiatkowski Oct 12 '15 at 10:02
  • 1
    Please also do Clean Build after all the above steps. – Ads Jan 27 '16 at 22:15
  • Hey, since this is the top voted and accepted answer, you should update it to the current behavior. – Sky Kelsey Apr 29 '16 at 17:31
  • 1
    In the line `compile project(':Name-Of-Your-Project')`, I replaced `Name-Of-Your-Project` with the name of my .aar file (without the `.aar` extension). And it worked. – Sufian Apr 26 '17 at 10:46
190

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 the module build.gradle:

dependencies {
   compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
 }

In the project build.gradle:

repositories{
      flatDir{
              dirs 'libs'
       }
 }
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • 1
    This is the right answer and the newest way of doing it. Please upvote this answer ;) – lmo Mar 24 '15 at 13:25
  • I copied your code, but do you know why I might have got this error? *"Error:A problem occurred configuring project ':app'. > Artifact 'signalr-client-sdk-android-release.aar (:signalr-client-sdk-android-release:)' not found. Searched in the following locations: https://jitpack.io//signalr-client-sdk-android-release//signalr-client-sdk-android-release-.aar"* – toddmo Apr 14 '15 at 22:47
  • 11
    The `repositories` part goes in a **different** `build.gradle` in order to work - the global one. The `dependencies` part goes in the module's `build.gradle`. That's why I got the error. – toddmo Apr 15 '15 at 00:02
  • @toddmo So my answer actually applies for local AAR files . i.e an .aar file located inside your libs directory. – Nishant Srivastava Apr 15 '15 at 23:45
  • @toddmo I guess what you are trying to do is pull an aar file from maven , which definitely will go in the global build.gradle file. – Nishant Srivastava Apr 15 '15 at 23:46
  • Android Studio's add "New Module" is working for me, with Android Studio 1.3, Android gradle plugin v1.2.3, and Gradle version 2.4 – amram99 Aug 07 '15 at 20:21
  • Even with Android Studio 1.4 (Beta4) this seems to be the only solution that works if you have more than one .aar file, all with the same package name (I did not design this, sigh ...) from which you want to include one in your app depending on the used flavor, as flavor1Compile(...), flavor2Compile(...) work as well. – Nantoka Sep 22 '15 at 13:58
  • @AlexSales : Can you be more specific as to what is happening at your end ? – Nishant Srivastava Nov 02 '15 at 18:11
  • @AlexSales: I get the "failed to resolve", too, using the structure described above. Were you able to resolve this? – Al Lelopath Dec 01 '15 at 17:48
  • @AlLelopath If your aar contains transitive dependencies then you need to add the flag 'transitive true', which is what i am guessing would not be getting resolved. I see no issues when using this method with various versions of gradle and android studio. – Nishant Srivastava Dec 01 '15 at 17:59
  • @Radix: can you please help me? I have done exactly what you said and added 'transitive = true' within braces as found in docs but it still doesn't include :( :( This is so horrible, man. – aasu Mar 21 '16 at 16:08
  • @user2762451 this is a solution for local aar files. What you are probably asking about is transitive dependencies. In that case this wont work. Since Transitive dependencies work with a `pom` file and in a local aar file no pom is available, you will need to push your local aar to some private maven repository , which is when your `transitive true` will work. – Nishant Srivastava Mar 21 '16 at 21:30
  • @Radix wow so is there any way one can include transitive dependencies of a local aar (without explicitly including them in the project, of course)? – aasu Mar 22 '16 at 06:27
  • @user2762451 I am afraid there is nothing that I know of. You need to understand the local AAR file doesnot know what the transitive dependencies are unless there is a bundled POM file. Your build.gradle doesnot get packaged into the AAR file, so we can rule that out. If you use a POM, you are actually using a maven artifact, which means you need to deploy it somewhere. – Nishant Srivastava Mar 22 '16 at 19:13
  • How to load/build aar for a specific flavor? – Muhammad Babar Jul 18 '16 at 12:04
  • In the latest Android Studio, here's what's in the build.gradle: configurations.maybeCreate("default") artifacts.add("default", file('adclient-sdk-for-android-2.6.3.aar')) The AAR is by itself and then can be compiled into other modules that need it with compile project. – Learn OpenGL ES Nov 17 '16 at 17:46
  • @MuhammadBabar You should look at this nice blog post - https://medium.com/@sahildave/product-flavors-for-android-library-d3b2d240fca2#.edroyrx3d – Nishant Srivastava Dec 07 '16 at 10:44
  • 1
    Thanks. The old version didn't work for me, the new import way worked great! Gradle 3.5, groovy 2.4.10 – Arnold Schrijver Aug 15 '17 at 19:55
26

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')
Hossain Khademian
  • 1,616
  • 3
  • 19
  • 29
Josiah
  • 4,754
  • 1
  • 20
  • 19
  • 1
    Didn't work either... Looks like it's a known bug which hasn't been fixed yet: https://code.google.com/p/android/issues/detail?id=55863 – Alex Dmitriev Jul 02 '14 at 17:47
  • I did as you said in edit but nothing was added in the end. In the wizard I chose path to AAR file, specified module name. After clicking Finish it was sync'ing Gradle files but no new files or any lines in build.gradle appeared (there were no changes in my Git's working copy). I tried with both AAR and JAR files with the same result. (I'm using Android Studio 0.8.1) – Alex Dmitriev Jul 03 '14 at 11:06
  • Thank you for help, your answer pointed to the right direction. The reason why "Import AAR" hadn't worked for me is due to another issue: http://stackoverflow.com/questions/24703734/android-studio-0-8-1-imported-project-missing-root-folder. – Alex Dmitriev Jul 22 '14 at 21:46
  • 3
    This old answer actually does work for me as long as I include the `flatDir { dirs 'libs' }` block as mentioned in the OP using AS 8.13 / Gradle 2.1 – qix Oct 24 '14 at 20:15
  • 4
    I imported .aar file using new method of import. But I am not able to see library's class name when I type half name, and press ctrl + space. So it still doesn't consider library. Any idea? – MysticMagicϡ Feb 12 '15 at 11:54
  • 1
    Edited version works fine for Android Studio 1.2.1.1. No other gradel scripting has to be done. You can create a new module like descibed above. In the dialog 'Create new Model' pick the path to your library aar file, for instance MyLib\mylibrary\build\outputs\aar. AS will copy the file to a new local folder. Rebuild your application project and the new library modul will be shown in the project view. Now you can add the library module as a dependency for your application module (e.g. 'app') in the project structure: Project Structure | modules | app | dependencies | Add | Module dependency. – Christian Schulzendorff Jul 13 '15 at 11:35
  • This works. However, now I have */libs/foo.aar* AND a *module /foo/ with files build.gradle, foo.aar, and foo.iml*. This seems bad in that there are 2 foo.aar files in the Project. – Al Lelopath Dec 01 '15 at 18:10
17

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.

Richard
  • 948
  • 2
  • 11
  • 16
11

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 :)

Kumar Utkarsh
  • 161
  • 1
  • 8
8

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

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
  • Do you maybe have an idea as to why my `next` button is grayed out on the `Create New Module` dialog after selecting Import `JAR/AAR Package`? – Kevin Crain Nov 09 '16 at 08:50
  • 1
    Note that I have selected the .aar file and gave it a name – Kevin Crain Nov 09 '16 at 08:55
  • I found out the reason, the IDE is checking the path to determine if `.aar` file is within a project directory, I simply placed the `.aar` on my desktop and retried and it works, thanks for your solution! – Kevin Crain Nov 09 '16 at 09:02
  • Maybe a dumb question, but regarding `so you just need to include that module as a dependency afterwards` - how do you do that? – boltup_im_coding Dec 31 '17 at 17:58
  • @boltup_im_coding simply by putting >compile project(":yourmodule") in your dependencies or by using the Assistent in module settings – Marian Klühspies Dec 31 '17 at 18:04
  • 1
    @Marian Klühspies I see now, I wasn't sure if having the `include` in the `settings.gradle` was supposed to be enough. I got it working. Thanks! – boltup_im_coding Dec 31 '17 at 18:51
7

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.

lucasddaniel
  • 1,779
  • 22
  • 22
7

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'])
GoRoS
  • 5,183
  • 2
  • 43
  • 66
Glorin
  • 160
  • 2
  • 7
  • 1
    The "api fileTree(dir: 'libs', include: ['*.aar])" worked great. It didn't work if I used "implementation" instead of "api". I never could get Victor Ude's solution to work with multiple aar's. One additional insight: if you are using resources (such as layouts) make sure that the aar(s) you link in doesn't contain a resource with the same name as in your apk. It'll may give you an error that it can't find one of the id's in the layout. The issue is that it uses the wrong namespace for id in the aar layout. – Tom Rutchik Mar 28 '19 at 17:17
  • 1
    In my case the 'implementation fileTree' line worked perfectly. It was, actually, the only solution I tried that actually worked. – VictorEspina Jan 20 '21 at 15:24
6

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)

stuckless
  • 6,515
  • 2
  • 19
  • 27
Andrew Grosner
  • 2,337
  • 2
  • 18
  • 14
5

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

GoRoS
  • 5,183
  • 2
  • 43
  • 66
Rez
  • 4,501
  • 1
  • 30
  • 27
4

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.

Victor Ude
  • 415
  • 4
  • 13
  • What kind of module did you create? *File > New > New Module > ?* – Al Lelopath Dec 01 '15 at 17:50
  • In this case the module type is "Android Library" if you are using the GUI to create a module. You can make an existing module into an Android library by modifying the module's build.gradle. change `apply plugin: 'com.android.application'` to `apply plugin: 'com.android.library'` – Victor Ude Dec 03 '15 at 16:58
  • Is it possible to add multiple aar´s into one module? Otherwise I would need to create over 10 gradle modules just to include my aar files – Marian Klühspies Apr 02 '18 at 13:36
  • @MarianKlühspies a lot has changed since I initially wrote this answer. I would recommend trying the official documentation for including an aar in your project: https://developer.android.com/studio/projects/android-library#AddDependency – Victor Ude May 23 '18 at 19:42
4

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
mochadwi
  • 1,190
  • 9
  • 32
  • 87
3

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

implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs/')
ashakirov
  • 12,112
  • 6
  • 40
  • 40
0

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

Dinesh.P
  • 61
  • 5
0

Under dependencies in the build.gradle I added .aar files like so:

  compileOnly files('libs/api-release.aar')
Dan G Nelson
  • 862
  • 2
  • 11
  • 30