How to include AAR in React Native Android build?

人盡茶涼 提交于 2019-11-28 08:14:47

问题


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


回答1:


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.

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



回答2:


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.



来源:https://stackoverflow.com/questions/43859114/how-to-include-aar-in-react-native-android-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!