【问题标题】:How to include AAR in React Native Android build?如何在 React Native Android 构建中包含 AAR?
【发布时间】:2017-10-07 03:11:29
【问题描述】:

我正在尝试在我的 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;
                        ^

我进行了以下更改:

创建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
}

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'
        }
    }
}

以下是包含 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

【问题讨论】:

  • 嘿,你能解决这个问题吗?
  • 嗨@Micer,看看我刚刚发布的答案。

标签: android react-native estimote


【解决方案1】:

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
}

【讨论】:

  • 在 node_module 或任何其他位置创建“react-native-module-name”的位置?如果可能的话,可以提供如何在 react-native 代码中使用的文档?
  • 这在 RN 0.65 上对我有用,但路径略有变化:react-native-project-name/android/app/libs
【解决方案2】:

我尝试了很多方法,

使用 ANDROID STUDIO 打开文件夹并添加 AAR 或 JAR

最简单的方法是使用 android studio 打开 de react native 项目中生成的文件夹。他们将 aar 添加到常规项目中。

在 android Studio 中像往常一样添加(友好的余数):

  1. 打开模块设置(在项目顶部右击)
  2. 导入 aar/jar
  3. 添加到依赖项

【讨论】:

    【解决方案3】:

    我想出了两种方法来包含用于编译的 AAR。

    Estimote SDK 的具体方法是将此行添加到android/app/build.gradle

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

    请注意,对于我来说,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'
            ])
        }
    

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

    【讨论】:

      猜你喜欢
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2016-09-09
      相关资源
      最近更新 更多