本文介绍了在生成.aar文件的Gradle项目中添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gradle项目,它生成.aar文件,我用它在另一个项目中导入以运行应用程序。这两个项目都有build.gradle文件,我必须在两个项目gradle文件中添加依赖项才能使应用程序正常工作。



有什么办法可以从一个版本中删除依赖项。 gradle使其他项目不受影响?

解决方案

aar 文件 doesn' t包含嵌套(或)依赖关系,并且没有描述库使用的依赖关系的pom文件。



这意味着,如果您要使用 flatDir repo你必须在你的项目中指定依赖项。


$ b 你应该使用 maven仓库(您必须在私有或公共Maven回购库中发布该库),则不会有同样的问题。

在这种情况下,gradle下载t他使用包含依赖关系列表的pom文件进行依赖关系。



另一种方法是将模块添加为外部模块。

Inside a您可以参考外部模块



只需使用:

 项目
| __build.gradle
| __settings.gradle
| __app(应用程序模块)
| __build.gradle

$ b $ settings.gradle

  include':app'
include':myExternalLib'
project(':myExternalLib')。projectDir = new File('pathLibrary')

app / build.gradle 中:

 依赖关系{
编译项目(':myExternalLib')
}

请注意 myExternalLib

您必须使用路径在另一个项目中的库,不是项目的根

I have a gradle project which generates .aar file which i used to import in another project to run app. Both projects have build.gradle file and i have to add dependencies in both projects gradle file to make application work.

Is there any way we can remove dependency from one build.gradle so that the other project is not affected?

解决方案

The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.

It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.

You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.

An alternative is to add the module as "external module".
Inside a project you can refer an external module.

Just use:

Project
|__build.gradle
|__settings.gradle
|__app (application module)
   |__build.gradle

In settings.gradle:

include ':app'
include ':myExternalLib'
project(':myExternalLib').projectDir=new   File('pathLibrary')

In app/build.gradle:

dependencies {
    compile project(':myExternalLib')
}

Pay attention to myExternalLib.
You have to use the path of the library inside the other project, not the root of the project.

这篇关于在生成.aar文件的Gradle项目中添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:37