Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 905
Denys Mikhalenko Avatar asked Oct 04 '17 14:10

Denys Mikhalenko


People also ask

Where do I put AAR file in Android project?

Add your AAR or JAR as a dependency Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your . aar or .

Can we use Android library in React Native?

On the React Native side, you can call both iOS and Android native modules. This is a complete example of a React Native component calling the native module: And that's it for coding, now for the final results.

Can I work with React Native in Android Studio?

You will need Node, the React Native command line interface, a JDK, and Android Studio. While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.


1 Answers

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?

like image 119
Denys Mikhalenko Avatar answered Sep 30 '22 00:09

Denys Mikhalenko