Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error building Android library: Direct local .aar file dependencies are not supported

We recently upgraded to Android Gradle Plugin 4.0.0-beta03. We are now seeing this error when building one of our library modules

$ ./gradlew library_module:assemble

Execution failed for task ':library_module:bundleDebugAar'.
> 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 AAR. Previous versions of the Android 
Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The 
following direct local .aar file dependencies of the :library_module project caused this error: 
______.aar

I can see this was added to AGP a few months ago. But they provide no further info on why.

So.

  1. What was the problem? Any more info? I can't find a single bug report anywhere.
  2. How exactly can I fix this? Is this saying that I can't build one .aar that depends on other local .aars? What if this local aar was instead hosted on Maven Central or another remote repo? Why would that make a difference?
like image 996
tir38 Avatar asked Mar 27 '20 01:03

tir38


12 Answers

I recently encountered the same issue, the fix was to remove the library from libs/ and import it using File -> New -> New Module -> Import .JAR/.AAR Package, then referencing it in the library module build.gradle file:

dependencies {
  implementation project(":imported_aar_module")
}

If you are on a newer Android Studio version (4.0.0+), this option is not available. Instead you have to do it manually.

  1. Create a new directory and put the following content into the build.gradle file withing the new directory:
configurations.maybeCreate("default")
artifacts.add("default", file('[nameOfTheAar].aar'))
  1. Place the aar into this new directoy. Next to the build.gradle file.
  2. Add the new created Gradle project to the settings.gradle file:
include(":pathToTheCreatedDirectory")
  1. Include the project in your library where you want to use the aar:
implementation project(":pathToTheCreatedDirectory", configuration = "default")
like image 153
Sandi Avatar answered Sep 29 '22 09:09

Sandi


When building an Android library that depends on other Android libraries (i.e., aar files), you will get the following error message if you include the aar files as dependencies in the project:

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 AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error).

As the above message states, when you build an Android library project, any aar it depends on is not packaged. If you built this way prior to AGP (Android Gradle Plugin) 4, you probably noticed that you had to include the aar dependencies on the project consuming your library.

You can compile your Android library project by specifying that the aar dependencies are compileOnly. See this for more info on when to use compileOnly.

So just add the following to your app build.gradle file:

compileOnly files('libs/some-library.aar')

Note that if you do this you will have to include the aar dependencies on the application project that consumes your library.

Alternatively, you can create a module that imports your aar dependency as @Sandi mentioned in the answer above.

Another way is to publish your aar dependencies to a maven repository and then add them to your library project like this:

implementation 'mylibrarygroup:mylibraryartifact:version-x.y.z@aar'
like image 43
Luis Avatar answered Sep 29 '22 09:09

Luis


I want to call out @StefMa's comment on this question which was incredible simple and solved this issue for me, but it's buried among many other comments on this thread and is easily missed.

The 'correct' answer on this thread no longer works because it's not possible to import AARs in Android Studio anymore as referred to in that answer. But, the solution referred to in StefMa's comment linking to this GitHub post does, and it works perfectly.

Long story short - put your AAR into a separate module.

There's no need to muck around with creating lib directories, just follow these directions -

  1. Create a new directory in your project's root directory. The image below shows two of them - spotify-app-remote and spotify-auth, but one is sufficient. Within that, put your AAR in, and create a new build.gradle file.

    Folder structure

  2. Within the build.gradle file, add the following, replacing the aar filename with the name of your AAR file -

    configurations.maybeCreate("default")
    artifacts.add("default", file('spotify-app-remote-release-0.7.1.aar'))
    
  3. Add this to your settings.gradle file, substituting the name of the directory you created

    include ':spotify-app-remote'
    
  4. Include your new module in the module you wish to use the AAR. eg, if you want to use it within your app module, open app's build.gradle and add

    api project(':spotify-app-remote')
    

    within your dependencies { } block, obviously again substituting spotify-app-remote with whatever the name of your module is.

like image 45
Jarrod Robins Avatar answered Sep 29 '22 09:09

Jarrod Robins


In my experience, when Gradle Plugin version is 4.2.2+ and Gradle version is 7.1+, as in @Luis's answer 'compileOnly' works.

compileOnly files('libs/your_library_name.aar')

It didn't work when the Gradle versions were lower.

like image 33
Vishudh Sasidharan Avatar answered Sep 29 '22 07:09

Vishudh Sasidharan


There are some changes now, You need to add your AAR or JAR as a dependency

1.) First, Navigate to File > Project Structure [Reference Image 1]1

2.) Then go to Dependencies > Declared Dependencies tab, click and select JAR/AAR Dependency in the dropdown [Reference Image 2]2

3.)In the Add Jar/Aar Dependency dialog, first enter the path to your .aar or .jar file, then select the configuration to which the dependency applies. If the library should be available to all configurations, select the "implementation" configuration. [Reference Image 3]3

4.) Click OK then Apply > OK.

You are good to go.

like image 33
Simon Avatar answered Sep 29 '22 07:09

Simon


Getting same error when use this code.

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

Replace your code with following.

Open the top level ‘build.gradle’ file and add.

repositories {
        flatDir {
            dirs('/src/main/libs')
        }
    }

Then in your project’s build.gradle add the following.

api(name:'aar_module_name', ext:'aar')
like image 42
user2659769 Avatar answered Sep 29 '22 07:09

user2659769


I had the same issue, in the sense I wanted to encapsulate a library dependency into a module. However this library dependency had a bunch of aars and creating separate module each of them is just clutter, and can't even find that option in the new studio.

To resolve it I published the aar-s into my local maven, before starting the build process.

So my encapsulating module's build.gradle looked like this:

plugins {
  id 'com.android.library'
  id 'kotlin-android'
  id 'maven-publish'
}
//..
parent.allprojects { // for some reason simply repositories didn't work
  repositories {
    mavenLocal() 
  }
}
//...
publishing {
  publications {
    barOne(MavenPublication) {
        groupId 'foo-aar-dependency'
        artifactId 'bar1'
        version '1.0'
        artifact("$libsDirName/bar1.aar")
    }
    barTwo(MavenPublication) {
        groupId 'foo-aar-dependency'
        artifactId 'bar2'
        version '1.0'
        artifact("$libsDirName/bar2.aar")
    }
    barThree(MavenPublication) {
        groupId 'foo-aar-dependency'
        artifactId 'bar3'
        version '1.0'
        artifact("$libsDirName/bar3.aar")
    }
    // and so on...
  }
}

// add the publication before the build even starts
// used ./gradlew mymodule:assemble --dry-run to find where to put it
afterEvaluate {
  tasks.clean.dependsOn("publishToMavenLocal")
  tasks.preBuild.dependsOn("publishToMavenLocal")
}

dependencies {
  implementation "foo-aar-dependency:bar1:1.0"
  implementation "foo-aar-dependency:bar2:1.0"
  implementation "foo-aar-dependency:bar3:1.0"
  // and so on
  // also I had to make sure to add the aar's transitive dependencies as implementation below
}

Note: When I sync for the first time the dependencies are not found, but as soon as any clean/assemble is called the dependencies are published prior so it runs as it needs.

Note2: most of this can be moved into a separate file to not clutter your build.gradle

Note3: If you actually want to publish your module as a library this solution is not for you.

Note4: This also works on CI if you run clean then your next task.

like image 24
Gergely Hegedus Avatar answered Sep 29 '22 07:09

Gergely Hegedus


If you want to bundle a local .aar within your library and use that library in another project, you could take a look at "fat aars" https://github.com/kezong/fat-aar-android

like image 38
Lukas Lechner Avatar answered Sep 29 '22 09:09

Lukas Lechner


It is bug in Android Studio 4.0.+.However, there is a solution.

First, project/build.gradle:

allprojects {
   repositories {
        google()
        jcenter()
        mavenCentral()
        flatDir {dirs "../MoudleA/aars,../MoudleB/aars,../MoudleC/libs".split(",")
        }
    }
}

Second, Moudle/build.gradle:

// MoudleA/build.gradle

repositories {
    flatDir {
        dirs 'aars'
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    //api fileTree(dir: 'aars', include: ['*.aar'])
    // aar
    new File('MoudleA/aars').traverse(
            nameFilter: ~/.*\.aar/
    ) { file ->
        def name = file.getName().replace('.aar', '')
        api(name: name, ext: 'aar')
    }
}

// MoudleB/build.gradle

repositories {
    flatDir {
        dirs 'aars'
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    //fullApi fileTree(dir: 'aars/full', include: ['*.aar'])
    //liteApi fileTree(dir: 'aars/lite', include: ['*.aar'])
    // aar
    new File('MoudleB/aars/full').traverse(
            nameFilter: ~/.*\.aar/
    ) { file ->
        def name = file.getName().replace('.aar', '')
        fullApi(name: 'full/' + name, ext: 'aar')
    }
    new File('MoudleB/aars/lite').traverse(
            nameFilter: ~/.*\.aar/
    ) { file ->
        def name = file.getName().replace('.aar', '')
        liteApi(name: 'lite/' + name, ext: 'aar')
    }

}

// MoudleC/build.gradle

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    //api fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    api fileTree(dir: 'libs', include: ['*.jar'])
    // aar
    new File('MoudleC/libs').traverse(
            nameFilter: ~/.*\.aar/
    ) { file ->
        def name = file.getName().replace('.aar', '')
        api(name: name, ext: 'aar')
    }
}

It works for me,You can also try.

like image 30
daozi Avatar answered Sep 29 '22 08:09

daozi


EDIT : if the AAR does not contain android resources or native code, this could help you.

If you want this local resource directly linked to an "app" or "sdk" module (no compileOnly)

=> Use a jar.

  • Rename the .aar to .zip
  • Extract it
  • Use the classes.jar inside

That's it.

like image 45
michgauz Avatar answered Sep 29 '22 07:09

michgauz


You can upload the AARs to an Artifactory, and consume them.

like image 35
DoronK Avatar answered Sep 29 '22 07:09

DoronK


For those who prefer to use as a regular dependency (or an item on your Gradle's version catalog):

  1. Create a folder eg. spotifyAppRemote at the same level of app folder
  2. Add the desired .aar file at the root of spotifyAppRemote folder
  3. Create a settings.gradle.kts file at the root of spotifyAppRemote folder. This file will be empty, it just needs to be there for the composite builds. See: docs
  4. Create a build.gradle.kts file at the root of spotifyAppRemote folder:
plugins {
    base //allows IDE clean to trigger clean on this module too
}

configurations.maybeCreate("default")
artifacts.add("default", file("spotify-app-remote-release-0.7.2.aar"))

//Change group to whatever you want. Here I'm using the package from the aar that I'm importing from
group = "com.spotify.android"
version = "0.7.2"
  1. Next add Gradle files to this folder to allow this module to build itself. You can do it manually or add the following snippet at the root of settings.gradle.kts (!! the project root, not the empty one created above)
/* Optional - automatically sync gradle files for included build */
rootDir.run {
    listOf(
        "gradle.properties",
        "gradlew.bat",
        "gradlew",
        "gradle/wrapper/gradle-wrapper.jar",
        "gradle/wrapper/gradle-wrapper.properties"
    ).map { path ->
        resolve(path)
            .copyTo(
                target = rootDir.resolve("spotifyAppRemote").resolve(path),
                overwrite = true
            )
    }
}
  1. Now you can go ahead and add this folder as a module at the settings.gradle.kts on your project root. The same where may add the snippet above:
rootProject.name = "Your project name"
include(":app")
includeBuild("spotifyAppRemote")
  1. Sync and build your project.
  2. Now your included build will be available for your as a regular dependency with the defined group and version. To use this dependency:
dependencies {
    // group:moduleName:version
    implementation("com.spotify.android:spotifyAppRemote:0.7.2")
} 

Thanks other members for the solution.

Source code on github: https://github.com/rsicarelli/SpotifySdkCompositeBuild

like image 40
rsicarelli Avatar answered Sep 29 '22 08:09

rsicarelli