How to Include an External .aar File Using Gradle?

Zafer Şevik
2 min readFeb 6, 2017

--

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

src/main/libs folder is ready

Copy your .aar file to src/main/libs

my_library.aar copied into libs

Open ‘build.gradle’ file(the one under ‘app’) and add dependency compile(name:’my_library’, ext:’aar’)

compile dependency for .aar file added

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.

--

--