Transitive dependencies not resolved for aar library using gradle

I have solved my problem by setting transitive attribute for my aar dependency:

compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar'){
    transitive=true
}

you should not use "@aar", if use "@" is become "Artifact only notation", if you want to use "@" and want have dependence transitive, you should add "transitive=true"


Try this if you are using aar locally:

compile(project(:your-library-name)) {
    transitive=true
}

I was having a similar problem and felt I could share the steps of solving the problem.

The basic idea of not being able to use the transitive dependencies while you are publishing your own aar is actually not having the .pom file generated with the expected transitive dependencies.

I was using 'maven-publish' plugin for my android aar dependency to publish it in my own private maven repository. The transitive dependencies were not resolved when my other projects were adding my aar dependency in their build.gradle. Hence here what I did to modify the .pom file while publishing my aar.

An important thing to note here that, the dependencies which you want to have the transitive behavior should be imported using the api in your library project's build.gradle file like the following.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api 'com.android.volley:volley:1.0.0'
    api "com.google.code.gson:gson:$globalGsonVersion"
}

Now as I said earlier, I was using maven-publish plugin to publish the aar dependency and hence my publishing task in the gradle looks like the following.

publishing {
    publications {
        mavenAar(MavenPublication) {
            from components.android
        }

        mavenJava(MavenPublication) {
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                // Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.api.allDependencies.each {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }

    repositories {
        maven {
            // Your repository information goes here
        }
    }
}

Hence, I used another mavenJava task to publish the .pom file in my private maven repo so that when the aar file is added as a dependency to some other module, it gets the .pom information and download the transitive dependency.

To complete the answer, this is how you should add the dependency in the build.gradle file for your own published aar to me imported.

api('com.example.masudias:my_lib:1.0.0@aar') {
    transitive = true
}

Transitive dependency

transitive means that the consumer(e.g. app) includes a producer and all producer's dependencies(e.g. libraries). It increase build time and can create some issues with dependency versions

By default, Gradle dependency has transitive = true

api ('com.package:library:0.0.1')
//the same
api ('com.package:library:0.0.1') {
    transitive = true
}

When you use @artifact notation it has transitive = false

api ('com.package:library:0.0.1@aar')
//the same
api ('com.package:library:0.0.1@aar') {
    transitive = false
}