Denys Mikhalenko
Denys Mikhalenko

Reputation: 4264

How to include 3rd party aar into Android React Native module?

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?

Upvotes: 14

Views: 6013

Answers (7)

Monsoon khadka
Monsoon khadka

Reputation: 1

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

Upvotes: 0

Monsoon khadka
Monsoon khadka

Reputation: 1

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)

Upvotes: 0

Luis Fer Garcia
Luis Fer Garcia

Reputation: 3794

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?

Upvotes: 0

Mithun Halmare
Mithun Halmare

Reputation: 33

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')
}

Upvotes: 2

gtfargo
gtfargo

Reputation: 337

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"))

Upvotes: 2

Denys Mikhalenko
Denys Mikhalenko

Reputation: 4264

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?

Upvotes: 2

user6490462
user6490462

Reputation:

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'))

Upvotes: 0

Related Questions