Adding Dependencies in Gradle Project which genera

2019-04-17 13:19发布

问题:

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?

回答1:

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.