Skip to content Skip to sidebar Skip to footer

Gradle: How To Include Dependencies From Repositories To Output Aar File

I am trying to build an arr package in Android Studio. This package contains dependecies for Zendesk: allprojects { repositories { maven { url 'https://zendesk.artifact

Solution 1:

By default AARs do not include any dependencies. If you want to include them you have to copy these libs from artifactory/your cache folder into your package, either by doing it manually or this task might help you: https://stackoverflow.com/a/33539941/4310905

Solution 2:

I know this answer comes a bit late, but still...

The transitive param you wrote is going to include transitive dependencies (dependencies of your dependency), which have to be set in the pom.xml file of the dependency you set to compile. So you don't really need to do that for the aar packaging, unless it's intended for any other purpose.

First, think that you can package an aar with some jars inside (in the libs folder), but you cannot package an aar inside an aar.

The approach to solve your problem is:

  • Get the resolved artifacts from the dependency you are interested in.
  • Check which ones of the resolved artifacts are jar files.
  • If they are jar, copy them to a folder and set to compile that folder in your dependencies closure.

So more or less something like this:

configurations {
    mypackage // create a new configuration, whose dependencies will be inspected
}

dependencies {
    mypackage 'com.zendesk:sdk:1.7.0.1'// set your dependency referenced by the mypackage configuration
    compile fileTree(dir: "${buildDir.path}/resolvedArtifacts", include: ['*.jar']) // this will compile the jar files within that folder, although the files are not there yet
}

task resolveArtifacts(type: Copy) {
    // iterate over the resolved artifacts from your 'mypackage' configuration
    configurations.mypackage.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact resolvedArtifact ->

        // check if the resolved artifact is a jar fileif ((resolvedArtifact.file.name.drop(resolvedArtifact.file.name.lastIndexOf('.') + 1) == 'jar')) {
            // in case it is, copy it to the folder that is set to 'compile' in your 'dependencies' closurefrom resolvedArtifact.file
            into "${buildDir.path}/resolvedArtifacts"
        }
    }
}

Now you can run ./gradlew clean resolveArtifacts build and the aar package will have the resolved jars inside.

I hope this helps.

Solution 3:

When you compile your project, it is compiled against the necessary libraries, but the libraries are not automatically packaged.

What you need is called "fatjar/uberjar" and you can achieve that by Gradle shadow plugin.

Post a Comment for "Gradle: How To Include Dependencies From Repositories To Output Aar File"