I am working on a Fabric Native component for Android as part of a React Native app and need to include a .aar file as a dependency.

I have included a static lib file, mylibrary-debug.aar in a directory called libs that is a sibling to my src folder in the Fabric Native component's android folder. The lib can't be found when running react-native run-android and this Fabric component is included in my node_modules.

The build.gradle is shown below.

buildscript {
  ext.safeExtGet = {prop, fallback ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
  }
  repositories {
    google()
    gradlePluginPortal()
  }
  dependencies {
    classpath("com.android.tools.build:gradle:7.3.1")
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20")
  }
}

def isNewArchitectureEnabled() {
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
  apply plugin: 'com.facebook.react'
}
apply plugin: 'org.jetbrains.kotlin.android'

android {
  compileSdkVersion safeExtGet('compileSdkVersion', 33)
  namespace "com.composabletext"

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 21)
    targetSdkVersion safeExtGet('targetSdkVersion', 33)
    buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", "true")
  }
}

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

dependencies {
  implementation(name:'mylibrary-debug', ext:'aar')
  implementation 'com.facebook.react:react-native'
}

This build.gradle configuration allows me to import Kotlin classes from com.mylibrary.* in Android Studio and the gradle sync succeeds, but when I run react-native run android and build the gradle project using this Fabric Native component's source code in the node_modules folder of my React Native Android app, the build fails with the following:

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 -PreactNativeDebugArchitectures=arm64-v8a -PreactNativeArchitectures=arm64-v8a

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':composable-text:compileDebugAidl'.
> Could not resolve all task dependencies for configuration ':composable-text:debugCompileClasspath'.
   > Could not find :mylibrary-debug:.
     Required by:
         project :composable-text

Why does it seem to work in Android Studio but fail when building the Android app?

2

There are 2 best solutions below

0
On

I was able to fix this by putting the .aar library inside the example libs folder (sibling to src folder) and calling it on the example build.gradle like this:

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

and inside the parent android folder (outside example folder), I added the library like this:

implementation fileTree(dir: file("${rootDir}/app/libs"), include: ['*.aar', '*.jar'])

The rootDir points to the android folder inside the example folder and loads the libraries inside ${rootDir}/app/libs

0
On

I was able to resolve it by adding the following to the Android app's build.gradle (the build.gradle in the root of the Android app project)

dirs project(':composable-text').file('libs')