'How to include AAR in React Native Android build?

I'm trying to include an AAR file with my React Native Android app so that I can access its features using native code. How can I bundle an AAR so that native code can import it with React Native Android? Thanks!

The error I get is this when compiling:

~\app\android\app\src\main\java\com\grind\GrindModule.java:13: error: package com.estimote.sdk does not exist
import com.estimote.sdk.EstimoteSDK;
                        ^

I've made the following changes:

Create android/app/libs/estimote-sdk.aar.

Create react native native module (I've done this a few times before, it works fine until I try to use the SDK).

android/app/build.gradle

dependencies {
    compile(name:'estimote-sdk', ext:'aar')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

android/build.gradle

...
allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        flatDir {
            dirs 'libs'
        }
    }
}

These are the instructions for including the SDK: https://github.com/Estimote/Android-SDK#installation

https://github.com/Estimote/Android-SDK/blob/master/Docs/manual_installation.md#estimote-sdk-for-android-manual-installation



Solution 1:[1]

Copy the library.aar file into react-native-module-name/android/app/libs. Create the libs folder if it doesn't exists.

In react-native-module-name/android/app/build.gradle:

dependencies {
  implementation fileTree(dir: "libs", include: ["*.aar"])
  implementation 'com.facebook.react:react-native:+'  // From node_modules
}

Solution 2:[2]

I tried many ways,

OPEN FOLDER WITH ANDROID STUDIO AND JUST ADD THE AAR OR JAR

The easiest way was to open the generated folder inside de react native project with android studio. Them add the aar as adding the aar to a regular project.

enter image description here

enter image description here

In android Studio add as always (Friendly Remainder):

  1. Open Module Settings (Right click on top of the project)
  2. Import aar/jar
  3. Add to Dependencies

Solution 3:[3]

There were two ways I figured out to include an AAR for compilation.

The Estimote SDK specific way was to add this line to android/app/build.gradle:

dependencies {
    ...
     compile 'com.estimote:sdk:1.0.3:release@aar'
}

Note, the documentation was out of date for me for the released version of the SDK, so it was tricky figuring out what the classes and methods to use were.

The other way I got AARs included in the build was the following changes:

Created folder android/libs, put the AAR file inside of it.

In android/app/build.gradle:

fileTree(dir: 'libs', include: '**/*.aar')
    .each { File file ->
        dependencies.add("compile", [
            name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, 
            ext: 'aar'
        ])
    }

With that done I could start importing the classes and methods.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 d512
Solution 2 GoRoS
Solution 3 c-h-