Are you getting error for direct .aar Dependencies in React Native/Android Projects, find out an easy way to fix this.

With recent changes in the Gradle some projects were getting errors for including the local aar library directly as shown below. This happens mainly in newer React Native projects mostly above React Native version 0.66 and also in Android projects with gradle version 4 and above. I’ll show you a simple way to fix the errors by including the local AAR library directly.

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

To fix this issue follow the steps mentioned below

  • Update the dependency block of the build.gradle file in the Android package which throws this error. Which must be ideally located under your node module. Replace the line as mentioned below.
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.aar'])
}
//change the above line as mentioned below 
dependencies {
    implementation(name: '<Library_Name>', ext: 'aar')
}
  • In the main android project’s gradle file, under allprojects { repositories {}} block add the following
allprojects {
    repositories {
        flatDir { dirs "$rootDir/../node_modules/path_to_Library" }
     }
}

Pro Tip:- Specify the paths in double quotation mark, the path expansion like $rootDir won’t work in single quotation mark. Its a bash script thumb rule.

  • Another key factor, make sure the dependencyResolutionManagement block in settings.gradle is not set to PREFER_SETTINGS or it is empty. If setting to PREFER_SETTINGS will override our project level setup and this fix won’t work.

These changes will fix the issue of direct local .aar file dependencies in the React Native project.

Way to fix in native Android project

If the same issue pops up in the native Android project we can fix it with the help of the module approach. Include the library file in a new module and refer to the library through the module as mentioned below.

  • 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'))
  • Place the AAR into this new directory. Next to the build.gradle file.
  • Add the newly created Gradle project to the settings.gradle file
  • Include the project in your library where you want to use the AAR file
implementation project(":pathToTheCreatedDirectory", configuration = "default")

Using this approach you can fix the issue of direct local .aar file dependencies in the Native Android project.

Written By
Fareeth John

I’m working as a Sr. Solution Architect in Akamai Technologies. I have more than 12 years of experience in the Mobile app development industry. Worked on different technologies like VR, Augmented reality, OTT, and IoT in iOS, Android, flutter, and other cross-platform apps. Have worked on 45+ apps from scratch which are in the AppStore and PlayStore. My knowledge of mobile development including design/architecting solutions, app development, knowledge around backend systems,  cloud computing, CDN, Test Automation, CI/CD, Frida Pentesting, and finding mobile app vulnerabilities

Leave a Reply

Your email address will not be published. Required fields are marked *