How to include AAR in React Native Android build?
Asked Answered
O

3

14

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

Orator answered 9/5, 2017 at 0:15 Comment(2)
Hey, were you able to solve the issue?Selfreproach
Hi @Selfreproach see the answer I just posted.Orator
M
18

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
}
Morgue answered 25/2, 2020 at 17:11 Comment(3)
where to create "react-native-module-name" in node_module or any other location? and if possible can give document for how to use inside react-native code?Standice
This worked for me on RN 0.65 with a slightly change on the path: react-native-project-name/android/app/libsHypothetical
Confirming that it is in the react-native-module-name/android/app/libs folder.Putto
S
4

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
Slop answered 19/10, 2017 at 21:22 Comment(0)
O
1

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.

Orator answered 8/8, 2017 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.