How to solve Error building Android library

How to solve Error building Android library

Filibert Nosizwe Author: Filibert Nosizwe Date: 2022-08-11
How to solve Error building Android library

    1. FAILURE
    2. 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
      

    3. Most common Solution
    4. 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")
      

    5. Solution 2
    6. 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'
      

    7. Solution 3
    8. 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.

    9. Solution 4
    10. 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.