如何在 React Native Android 构建中包含 AAR?

c-h*_*-h- 11 android estimote react-native

我正在尝试在我的 React Native Android 应用程序中包含一个 AAR 文件,以便我可以使用本机代码访问其功能。如何捆绑 AAR 以便本机代码可以使用 React Native Android 导入它?谢谢!

我在编译时得到的错误是这样的:

~\app\android\app\src\main\java\com\grind\GrindModule.java:13: error: package com.estimote.sdk does not exist
import com.estimote.sdk.EstimoteSDK;
                        ^
Run Code Online (Sandbox Code Playgroud)

我进行了以下更改:

创建android/app/libs/estimote-sdk.aar.

创建 react native 原生模块(我以前做过几次,在我尝试使用 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
}
Run Code Online (Sandbox Code Playgroud)

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'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这些是包含 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

Lui*_*cia 13

library.aar文件复制到react-native-module-name/android/libs. libs如果文件夹不存在,则创建该文件夹。

react-native-module-name/android/build.gradle

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


c-h*_*-h- 1

我想出了两种方法来包含 AAR 进行编译。

Estimote SDK 的特定方法是将这一行添加到android/app/build.gradle

dependencies {
    ...
     compile 'com.estimote:sdk:1.0.3:release@aar'
}
Run Code Online (Sandbox Code Playgroud)

请注意,对于我来说,SDK 发布版本的文档已经过时,因此弄清楚要使用的类和方法是什么是很棘手的。

我将 AAR 包含在构建中的另一种方法是进行以下更改:

创建文件夹android/libs,将AAR文件放入其中。

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'
        ])
    }
Run Code Online (Sandbox Code Playgroud)

完成后,我可以开始导入类和方法。