13

I am trying to build an arr package in Android Studio. This package contains dependecies for Zendesk:

allprojects {
    repositories {
        maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
    }
}

compile (group: 'com.zendesk', name: 'sdk', version: '1.7.0.1') {
    transitive = true
}

compile (group: 'com.zopim.android', name: 'sdk', version: '1.3.1.1') {
    transitive = true
}

I want to build this package for a Unity3d project. This package should contain all dependecies for Zendesk (the transitive = true property). When I open the aar file there is no dependencies for Zendesk. What is wrong?

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
Konstantin Gindemit
  • 382
  • 1
  • 4
  • 10

3 Answers3

11

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

Community
  • 1
  • 1
and_dev
  • 3,723
  • 1
  • 20
  • 28
  • 1
    That link deals with jar files. Most Android libraries will include library dependencies part of the framework that are not jar files. So that solution won't help include those. – Johann Dec 24 '19 at 09:03
6

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 file
        if ((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' closure
            from 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.

jjnunog
  • 171
  • 2
  • 7
  • Here is an option for Gradle 4.6: https://gist.github.com/stepio/824ef073447eb8d8d654f22d73f9f30b – stepio Nov 08 '18 at 22:00
  • @stepio i have alot of thrid party gradle dependencies and i am unable to create aar file having those libraries build with in the aar. i have used this code task copyLibs(type: Copy) { from configurations.myConfig into 'libs' } – Adeel Turk Nov 22 '18 at 10:44
  • 2
    @stepio aar file is generated but when i use that aar file i have to add those dependecies in my app which are required in my aar module. otherwise it gives notfound error against the source code i used from those 3rd party libs in my module – Adeel Turk Nov 22 '18 at 10:46
  • @AdeelTurk, this script worked for me, but in my cases I needed to add each library twice: first time through "copyLibs", another one - just regular "implementation". Hope this helps. – stepio Nov 22 '18 at 13:46
-2

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.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40