How to add .aar dependency in library module?
Asked Answered
U

7

39

I am having one .aar file of one library module.
I want to use it as a library or dependency in my other project's library module.
How do I do it?

I tried options provided at below links:
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/

It works only if I add .aar reference in my project's application module. But not working in library module.

Thanks.

Ulla answered 13/1, 2016 at 11:13 Comment(4)
please provide your build.gradleInductee
Possible duplicate of Adding local .aar files to my gradle buildSymphony
allprojects { repositories { jcenter() flatDir { dirs 'libs' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.0' compile 'com.android.support:recyclerview-v7:23.0.1' compile(name:'mylibmodule', ext:'aar') }Ulla
@Ulla check above link .Immix
A
71

Follow this setting and you will able to add .aar dependency to library module

build.gradle (Project: ....)

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        flatDir {
            dirs 'libs'
            dirs project(':library_module').file('libs')
        }
    }
}

build.gradle (Module: app)

dependencies {
    ...
    compile project(':library_module')
}

build.gradle (Module: library_module)

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

settings.gradle (Project Settings)

include ':app', ':library_module'
Aurthur answered 7/6, 2017 at 2:30 Comment(4)
dirs project(':library_module').file('libs') do the jobRosena
@Android, I am sorry since I answer it for a long time so I am not sure about the change. If you found the way for solving it, hope you can correct me. Thank you so muchAurthur
No issue @PhanVanLinhBruno
After lot of struggle, this answer help me to fix the issue.Thank you Phan Van Linh.Carlin
A
15

In all modules (library or application) where you need the aar file you have to add in your build.gradle the repository:

repositories {
    flatDir {
        dirs 'libs'
    }
}

and add the dependency:

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

You can use the top-level file to add the repositories, but you can't add the dependencies in the top-level file.
Pay attention to the relative path of the libs folder that you are using in the module.

Abbotson answered 13/1, 2016 at 12:24 Comment(3)
I want library with resources.Ulla
What does it mean? The aar has the resourcesAbbotson
@Ulla You have to add the dependencies in your modules, not in your top-level file. Reading your comment you are adding the dependencies in the wrong fileAbbotson
B
9

Sorry process involves 10 steps but these are super simple and easy.

1- New->Module

enter image description here

2- Select import .jar/aar package

enter image description here

3- Select your .aar file from location

enter image description here

4- We can see module is added but not configured with app module yet.

enter image description here

5- Go to Project Structure

enter image description here

6- Current project structure is like this one

enter image description here

7- Go to app module and press '+' icon

enter image description here

8- Select 3rd option Module dependency

enter image description here

9- Select newly added .arr module

enter image description here

10- you can see the new module attached with app module. Now click apply.

enter image description here

you can see we are good to go. enter image description here

Boilermaker answered 21/8, 2019 at 6:8 Comment(2)
I Miss this steps in Arctic Fox , If You find then please let me know :(Dogmatic
The dialog in step 2 is not available anymore in Chipmunk...Struggle
T
5

I did it a bit differently than others have posted here...

My main goal was to create a library module that contained all the jar's and aar's that I would need. My main project would then depend on this library module - I only wanted a single line in the main project's build.gradle to link this dependency.

  1. Add a new library module to the project: File -> New -> New Module -> Android Library

  2. within the library module, open the build.gradle and add:

// so that the library project can locate the aar in /libs
repositories {
    flatDir {
        dirs 'libs'
    }
}

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

now you can pop all the jar's and aar's into the /libs folder of your library module. Bonus: whatever the library name is doesn't matter, it's automatically discovered

Tart answered 8/4, 2019 at 18:1 Comment(4)
I was trying the same thing but this didn't work for me. it looks at app/libs directory insteadMasao
I can't find the answer to this. There's hundreds of answers just like this one but none of them seem to work for me. Error is Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting A AR.Metzler
@DanielWilson I'm facing the same issue, do you find any solution?Charterhouse
@WellingtonYogui see this answer: https://mcmap.net/q/49387/-error-building-android-library-direct-local-aar-file-dependencies-are-not-supportedMetzler
D
4
  1. File -> New Module -> Import .JAR/.AAR
  2. import the .AAR file.
  3. in library module build.gradle add the dependency dependencies{compile project(':Name-Of-Your-Module-aar')}

http://tools.android.com/tech-docs/new-build-system/tips#TOC-Handling-transitive-dependencies-for-local-artifacts-jars-and-aar-

Deration answered 13/6, 2017 at 8:19 Comment(1)
I was able to work through my problem following these steps. The settings.gradle and build.gradle files are automatically updated, leaving me to only implement step # 3. Thank you!Backstay
H
0

The easiest way will be to copy your aar file to the library module's libs folder. and in the app level build.gradle implement the dependency like below

implementation files('libs/aar_file_name.aar')
Hampson answered 2/9, 2022 at 11:25 Comment(0)
S
0

Under dependencies in the module's build.gradle add the associated .aar files with

compileOnly files('libs/api-release.aar')

And then in the importing project's app/build.gradle add the .aar files with

implementation files('../../node_modules/react-native-misnap/android/libs/api-release.aar')
Stgermain answered 19/7, 2023 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.