How to Include an External .aar File Using Gradle?
There are 2 ways to do that
First Way: Using Android Studio Menu (this is not my favourite)
File -> New -> New Module -> Import .jar/.aar and import your .aar.
Then in your project’s build.gradle (the one under ‘app’) add the following:
dependencies {
compile project(‘:Name-of-the-Project’)
}
Clean Build after all the above steps.
With this approach, every .aar file you add, will be visible in your project hierarchy (like the libraries you create and maintain in project).
Second Way: By Creating libs Folder
Create ‘libs’ folder under src/main
Copy your .aar file to src/main/libs
Open ‘build.gradle’ file(the one under ‘app’) and add dependency compile(name:’my_library’, ext:’aar’)
We now have an error message saying ‘Failed to resolve :my_library’. To fix this error open the top level ‘build.gradle’ file and add
flatDir {
dirs ‘src/main/libs’
}
A lot of places in the web flatDir { dirs ‘libs’ } is suggested but it doesn’t work.
And press ‘Try again’ to resolve dependencies and get back to coding.