I have to add .aar files as a library in a sub project in Android studio. It gives me an error.

10,058

Solution 1

In the repositories of your module: app

repositories {

    flatDir {
        dirs 'libs'
    }
}

Add the following in the dependencies of your module: app In the case that your have both a JAR and an AAR file, do the following.

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

}

Solution 2

You need to set the dependencies too:

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

This can be done in the subprojects build.gradle.


Edit: You might need to specify the file itself:

compile(name:'name-of-file',ext:'aar')

Solution 3

classpath 'com.android.tools.build:gradle:1.3.0'
  1. File -> New Module -> Import JAR/AAR Package

  2. "File name" select .aar file, example:

d:/myproject/mylib/build/outputs/aar/mylib-release.aar
  1. "Subproject name",example:

    mylib-release

  2. /app/build.gradle

    dependencies{

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

    compile project(':mylib-release')

    }

Solution 4

For filename.aar

repositories {
   flatDir {
        dirs 'libs'
    }
}

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

Solution 5

You are adding a aar file in libs folder.
The aar file doesn't contain the dependencies, then you have to add these dependencies also in the main project.

In your module1/build.gradle you should have something like:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile('com.android.support:appcompat-v7:22.2.1') //for example
    //..
 }

In your mainModule/build.gradle you have to add all the dependencies used by your module1.

dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile(name:'fileName',ext:'aar')
        compile('com.android.support:appcompat-v7:22.2.1') //for example
        //...
     }
Share:
10,058
Vatsal Singh
Author by

Vatsal Singh

Updated on July 18, 2022

Comments

  • Vatsal Singh
    Vatsal Singh almost 2 years

    I have added the .aar files to libs folder in the sub-project. And have given the repositories as:

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

    in the build.gradle of this sub-project. Do I need to add dependencies in Main project's build.gradle also? If yes, how should that be done?

  • Vatsal Singh
    Vatsal Singh almost 9 years
    Did that. Didn't help.
  • user25
    user25 almost 6 years
    His solution works ok, compile fileTree(dir: 'libs', include: ['*.jar','*.aar']) this will link all libraries (*.jar or *.aar) to your project or you can use manual link for every lib. If it doesn't work for then it's your fault