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

14

我正在尝试将一个AAR文件包含到我的React Native Android应用程序中,以便可以使用本地代码访问其功能。如何捆绑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
嗨@Micer,请看我刚刚发布的答案。 - c-h-
3个回答

18
library.aar 文件复制到 react-native-module-name/android/app/libs 目录下。如果不存在,则创建 libs 文件夹。
react-native-module-name/android/app/build.gradle 文件中:
dependencies {
  implementation fileTree(dir: "libs", include: ["*.aar"])
  implementation 'com.facebook.react:react-native:+'  // From node_modules
}

在哪里创建“react-native-module-name”,是在node_module或其他位置?如果可能的话,能否提供如何在react-native代码中使用的文档? - Somnath Kadam
1
这对我在RN 0.65上起作用,路径稍有更改:react-native-project-name/android/app/libs - vicvans20
1
确认它在 react-native-module-name/android/app/libs 文件夹中。 - d512

4
我尝试了很多方法,
用Android Studio打开文件夹,然后只需添加AAR或JAR即可。
最简单的方法是使用Android Studio打开React Native项目中生成的文件夹,然后像将AAR添加到常规项目一样添加AAR。

enter image description here

enter image description here

在Android Studio中像往常一样添加(友好提醒):

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

1

我找到了两种将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'
        ])
    }

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接