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

时间:2016-08-15 14:42:45

标签: android maven unity3d gradle aar

我正在尝试在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的所有依赖项(transitive = true属性)。当我打开aar文件时,Zendesk没有依赖关系。有什么问题?

3 个答案:

答案 0 :(得分:8)

默认情况下,AAR不包含任何依赖项。如果要包含它们,则必须将artifactory / cache文件夹中的这些库复制到包中,方法是手动执行,或者此任务可能对您有所帮助:https://stackoverflow.com/a/33539941/4310905

答案 1 :(得分:1)

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

您编写的transitive参数将包含传递依赖项(依赖项的依赖项),这些依赖项必须在您设置为pom.xml的依赖项的compile文件中设置。因此,您不需要为aar包装做到这一点,除非它是出于任何其他目的。

首先,假设你可以打包aar里面有一些jar(在...中) libs文件夹),但您无法在aar内打包aar

解决问题的方法是:

  • 从您感兴趣的依赖项中获取已解析的工件。
  • 检查哪些已解析的工件是jar个文件。
  • 如果他们是jar,请将其复制到一个文件夹,然后将compile关闭中的dependencies文件夹设置为<{1}}。

或多或少是这样的:

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 buildaar包中将包含已解析的jar

我希望这会有所帮助。

答案 2 :(得分:-3)

编译项目时,它会根据必要的库进行编译,但库不会自动打包。

你需要的是什么叫&#34; fatjar / uberjar&#34;你可以通过Gradle shadow plugin实现这一目标。

相关问题