Gradle: how to include dependencies from repositories to output aar file [duplicate]

青春壹個敷衍的年華 提交于 2021-02-07 12:55:25

问题


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?


回答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




回答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 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.




回答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.



来源:https://stackoverflow.com/questions/38957313/gradle-how-to-include-dependencies-from-repositories-to-output-aar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!