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

Kon*_*mit 4 android unity-game-engine gradle maven aar

我正在尝试在Android Studio中构建一个arr包.此软件包包含Zendesk的dependecies:

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
}
Run Code Online (Sandbox Code Playgroud)

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

and*_*dev 8

默认情况下,AAR不包含任何依赖项.如果你想要包含它们,你必须将这些库从artifactory /你的缓存文件夹复制到你的包中,或者通过手动操作或者这个任务可以帮助你:https://stackoverflow.com/a/33539941/4310905


jjn*_*nog 5

我知道这个答案来得有点晚,但仍然......

transitive您编写的参数将包含传递依赖项(依赖项的依赖项),必须在pom.xml您设置为compile. 所以你真的不需要为aar包装做那件事,除非它是用于任何其他目的。

首先,认为你可以aar用一些jars里面(在 libs文件夹中)打包an,但是你不能在一个aar里面打包an aar

解决您的问题的方法是:

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

所以或多或少是这样的:

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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我希望这有帮助。

  • @stepio aar 文件已生成,但是当我使用该 aar 文件时,我必须在我的应用程序中添加 aar 模块所需的依赖项。否则,它会针对我在模块中使用的第 3 方库中使用的源代码给出 notfound 错误 (2认同)