Gradle: 如何将存储库中的依赖项包含到输出 aar 文件中

Gradle: how to include dependencies from repositories to output aar file

我正在尝试在 Android Studio 中构建一个 arr 包。此软件包包含 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
}

我想为 Unity3d 项目构建这个包。此包应包含 Zendesk 的所有依赖项(传递性 = true 属性)。当我打开 aar 文件时,没有 Zendesk 的依赖项。怎么了?

默认情况下,AAR 不包含任何依赖项。如果你想包含它们,你必须将这些库从 artifactory/your 缓存文件夹复制到你的包中,可以手动执行,或者此任务可能对你有帮助:

当你编译你的项目时,它会针对必要的库进行编译,但这些库不会自动打包。

您需要的是 "fatjar/uberjar",您可以通过 Gradle shadow plugin 实现。

我知道这个答案来的有点晚,但还是...

您编写的 transitive 参数将包含传递依赖项(您的依赖项的依赖项),必须在您设置为 [=13= 的依赖项的 pom.xml 文件中进行设置].因此,您实际上不需要为 aar 包装执行此操作,除非它用于任何其他目的。

首先,认为你可以将一个 aar 和一些 jar 打包在里面(在 libs 文件夹),但不能将 aar 打包到 aar.

解决您的问题的方法是:

  • 从您感兴趣的依赖项中获取已解决的工件。
  • 检查哪些已解决的工件是 jar 个文件。
  • 如果它们是 jar,请将它们复制到一个文件夹并在 dependencies 关闭中设置为 compile 该文件夹。

差不多是这样的:

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"
        }
    }
}

现在您可以 运行 ./gradlew clean resolveArtifacts build 并且 aar 包中将包含已解析的 jars。

希望对您有所帮助。