What does transitive = true in Gradle exactly do (

2019-01-12 21:56发布

问题:

What does Gradle transitive = true do exactly? It is not clear from the Gradle documentation. This is in the context of compile within build.gradle. In my case I'm depending Android's crashlytics.

compile('com.crashlytics.sdk.android:crashlytics:2.2.2@aar') {
    transitive = true;
}

Several Gradle docs (here and here) imply that "transitive" defaults to true. Yet removing transitive = true results in transitive dependencies not being brought in (in particular KitGroup).

class file for io.fabric.sdk.android.KitGroup not found

The docs say it defaults to true, yet the actual behavior seems to be the opposite.

I am running Gradle 2.2.1. Perhaps the behavior changed between 2.2 and 2.4?

Edit: Related Transitive dependencies not resolved for aar library using gradle

回答1:

You are using the @aar notation.
It means that you want to download only the aar artifact, and no dependencies.
You can check this part of documentation:
Check the 1.4.1.2. Artifact only notation section:

An artifact only notation creates a module dependency which downloads only the artifact file with the specified extension. Existing module descriptors are ignored.

Using the @aar notation if you want to download the dependencies, you should add transitive=true.

I'd expect that omitting @aar it should work without adding the transitive attribute.



回答2:

My guess is that the Crashlytics artifact to which you're referring manually specifies dependencies as not transitive (transitive=false) so that you aren't forced to bring those dependencies in by default. That's why you're seeing the opposite behavior. For example some developers may not want to pull in all of Google Play Services or whatever else Crashlytics may use if present.

So, by removing that, Gradle no longer pulls in the dependency, and it fails to build. You can specify that dependency manually if you need to.

That being said - I think the bigger issue at hand is that you shouldn't be referencing the Crashlytics artifact directly - you should be using Fabric, and pulling in Crashlytics as a result: https://dev.twitter.com/fabric/android/integrating



回答3:

On a more general note: Setting transitive = false on the crashlytics library causes gradle to ignore all libraries required by crashlytics (="transient libraries") and not download and link them.

You would have to either manually add the required libraries to your project or rely on other transient libraries added by other dependencies.

Default for gradle is transitive = true.

Examples and full explanation here: http://www.devsbedevin.com/android-understanding-gradle-dependencies-and-resolving-conflicts/



回答4:

Sets whether this dependency should be resolved including or excluding its transitive dependencies. The artifacts belonging to this dependency might themselve have dependencies on other artifacts. The latter are called transitive dependencies.



回答5:

Gradle follows transitive dependencies by default. If you want to turn that off for a particular library, use the transitive flag.

Changing the value of the transitive flag to false prevents the download of transitive dependencies, so you’ll have to add whatever is required yourself. If you only want a module jar, without any additional dependencies, you can specify that as well.



回答6:

transitive controls transitivity. Gradle normally defaults to transitive, except when it doesn't. There's a bug with transitivity and classifiers, see https://issues.gradle.org/browse/GRADLE-3188.