14

I am building an android React Native module and my code imports classes from a 3rd party SDK which is provided as an aar file. How should I bundle this file into my module? I tried adding

allprojects {
    repositories {
        flatDir {
            dirs: 'libs'
        }
    }
}

and

dependencies {
    ... other deps ...
    compile (name:'my-external-lib', ext:'aar')
}

to the build.gradle file and putting this my-external-lib.aar file into libs/ folder, but still getting an error when building MyApp react-native application with react-native-my-module included:

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration 
':app:_developmentDebugApkCopy'.
   > Could not find :my-external-lib:.
     Required by:
         MyApp:app:unspecified > MyApp:react-native-my-module:unspecified

Any advice?

Denys Mikhalenko
  • 3,966
  • 2
  • 14
  • 18

7 Answers7

2

Looks like I managed to solve this question by myself. It seems that you need to define repositories in the main project to allow sub-projects locate their dependencies.

Putting this to the main project build.gradle file:

repositories {
    flatDir {
        dirs: 'libs'
    }
}

did the trick.

I believe though, that this is not the correct way to include sub-projects with dependencies, because you're obviously can't know how to amend your build.gradle file if this library is a 3rd party lib from npm. So, can someone explain why this worked and how should it be done in the right way?

Denys Mikhalenko
  • 3,966
  • 2
  • 14
  • 18
  • Hey I followed exactly but still I am getting Project with path `xxxx could not found in project yyyyy`. If possible can you share the structure of your module? – Pushpendra Jul 18 '18 at 12:13
2

In your module's android/build.gradle file, you'll want to add the .aar as an artifact.

configurations.maybeCreate("default")
artifacts.add("default", file("NAME_OF_YOUR_FILE.aar"))
gtfargo
  • 337
  • 2
  • 5
2

What ever you have written is absolutely correct but you just written in wrong place
you have written the code in the build.gradle(Project:project_name)
you just have to write the code in build.gradle(Module:app) file and the .arr file has to be paste in the projects lib folder

i.e.
repositories {
  flatDir {
   dirs 'libs'
  }
}

dependencies {
   ... other deps ...
   compile (name:'my-external-lib', ext:'aar')
}

0

In your settings.gradle you should include this lib e.g:

include ':app',  ':my-external-lib'

And you can compile it as a normal project e.g: compile project(':my-external-lib')

Also your my-external-lib build.gradle should be like :

configurations.maybeCreate("default")
artifacts.add("default", file('my-external-lib.aar'))
0

I successfully included a 3rd party .aar file into android react native module.

Please look at my answer to this question: How to include AAR in React Native Android build?

Luis Fer Garcia
  • 3,168
  • 3
  • 15
  • 13
0

after trying so Long I finally got it there how you should do it :

  1. Add

    repositories {   flatDir {    dirs 'libs'   } }

into project:grindle (not module.grindle) and

  1. Add dependies into app:grindle (not module.grindle)
0

I you are trying to use third party software in react-native module than

  1. Add aar file in reactnativeModule/android/libs

  2. Add

    allprojects { repositories { flatDir { dirs: 'libs' } } } in build.gradle of reactnativeModule/android/build.grindle

  3. add dependencies just in dependencies { implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}" of reactnativeModule/android/build.grindle