简体   繁体   English

如何将 3rd 方 aar 包含到 Android React Native 模块中?

[英]How to include 3rd party aar into Android React Native module?

I am building an android React Native module and my code imports classes from a 3rd party SDK which is provided as an aar file.我正在构建一个 android React Native 模块,我的代码从作为 aar 文件提供的 3rd 方 SDK 导入类。 How should I bundle this file into my module?我应该如何将此文件捆绑到我的模块中? I tried adding我尝试添加

allprojects {
    repositories {
        flatDir {
            dirs: 'libs'
        }
    }
}

and

dependencies {
    ... other deps ...
    compile (name:'my-external-lib', ext:'aar')
}

to the build.gradle file and putting this my-external-lib.aar file into libs/ folder, but still getting an error when building MyApp react-native application with react-native-my-module included:到 build.gradle 文件并将此 my-external-lib.aar 文件放入libs/文件夹,但在构建包含 react-native-my-module 的 MyApp react-native 应用程序时仍然出错:

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration 
':app:_developmentDebugApkCopy'.
   > Could not find :my-external-lib:.
     Required by:
         MyApp:app:unspecified > MyApp:react-native-my-module:unspecified

Any advice?有什么建议吗?

Looks like I managed to solve this question by myself.看起来我自己设法解决了这个问题。 It seems that you need to define repositories in the main project to allow sub-projects locate their dependencies.看来需要在主项目中定义repositories ,让子项目定位到它们的依赖。

Putting this to the main project build.gradle file:把它放到主项目 build.gradle 文件中:

repositories {
    flatDir {
        dirs: 'libs'
    }
}

did the trick.成功了。

I believe though, that this is not the correct way to include sub-projects with dependencies, because you're obviously can't know how to amend your build.gradle file if this library is a 3rd party lib from npm.不过,我相信,这不是包含具有依赖项的子项目的正确方法,因为如果此库是来自 npm 的第 3 方库,您显然无法知道如何修改 build.gradle 文件。 So, can someone explain why this worked and how should it be done in the right way?那么,有人可以解释为什么这有效以及应该如何以正确的方式完成吗?

In your module's android/build.gradle file, you'll want to add the .aar as an artifact.在您模块的android/build.gradle文件中,您需要将.aar添加为工件。

configurations.maybeCreate("default")
artifacts.add("default", file("NAME_OF_YOUR_FILE.aar"))

What ever you have written is absolutely correct but you just written in wrong place你所写的都是绝对正确的,但你只是写错了地方
you have written the code in the build.gradle(Project:project_name)您已经在 build.gradle(Project:project_name) 中编写了代码
you just have to write the code in build.gradle(Module:app) file and the .arr file has to be paste in the projects lib folder您只需要在 build.gradle(Module:app) 文件中编写代码,并且 .arr 文件必须粘贴到项目 lib 文件夹中

ie
repositories {存储库{
flatDir {平面目录{
dirs 'libs'目录'库'
} }
} }

dependencies {依赖{
... other deps ... ... 其他部门 ...
compile (name:'my-external-lib', ext:'aar')编译(名称:'my-external-lib',ext:'aar')
} }

In your settings.gradle you should include this lib eg:在您的 settings.gradle 中,您应该包含这个库,例如:

include ':app',  ':my-external-lib'

And you can compile it as a normal project eg: compile project(':my-external-lib')您可以将其编译为普通项目,例如: compile project(':my-external-lib')

Also your my-external-lib build.gradle should be like :另外你的my-external-lib build.gradle 应该是这样的:

configurations.maybeCreate("default")
artifacts.add("default", file('my-external-lib.aar'))

I successfully included a 3rd party .aar file into android react native module.我成功地将第 3 方.aar文件包含到 android react native 模块中。

Please look at my answer to this question: How to include AAR in React Native Android build?请看我对这个问题的回答: How to include AAR in React Native Android build?

after trying so Long I finally got it there how you should do it :经过这么长时间的尝试,我终于明白了你应该怎么做:

  1. Add添加

    repositories { flatDir { dirs 'libs' } }存储库 { flatDir { dirs 'libs' } }

into project:grindle (not module.grindle) and进入项目:grindle(不是module.grindle)和

  1. Add dependies into app:grindle (not module.grindle)将依赖项添加到 app:grindle(不是 module.grindle)

I you are trying to use third party software in react-native module than我您正在尝试在 react-native 模块中使用第三方软件而不是

  1. Add aar file in reactnativeModule/android/libs在 reactnativeModule/android/libs 添加 aar 文件

  2. Add添加

    allprojects { repositories { flatDir { dirs: 'libs' } } } in build.gradle of reactnativeModule/android/build.grindle reactnativeModule/android/build.grindle 的 build.gradle 中的 allprojects { repositories { flatDir { dirs: 'libs' } } }

  3. add dependencies just in dependencies { implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}" of reactnativeModule/android/build.grindle仅在dependencies { implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}" of reactnativeModule/android/build.grindle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM