How to include AAR in React Native Android build?

2019-01-28 10:39发布

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

2条回答
啃猪蹄的小仙女
2楼-- · 2019-01-28 11:22

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.

查看更多
太酷不给撩
3楼-- · 2019-01-28 11:33

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
查看更多
登录 后发表回答