使用gradle未解析aar库的传递依赖性

mko*_*zun 84 java android gradle android-library aar

我已经调查了一段时间,可能看到这里最流行的答案与aartransitive依赖关系有关,但不知何故,我仍然不清楚如何使这个工作.

所以:

我有给定gradle配置的android库:

apply plugin: 'android-library'
apply plugin: 'android-maven'

version = "1.0.0"
group = "com.somepackage"

buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }

    dependencies {
        classpath 'com.github.dcendents:android-maven-plugin:1.0'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.3'

    defaultConfig {
        minSdkVersion 10
    }
}

repositories {
    maven { url 'http://www.bugsense.com/gradle/' }
}

dependencies {
    provided 'com.google.android.gms:play-services:+'
    provided 'com.android.support:appcompat-v7:+'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.bugsense.trace:bugsense:3.6'
    compile 'commons-net:commons-net:3.3'
}
Run Code Online (Sandbox Code Playgroud)

然后我将它部署到本地maven repo gradle install.部署库的POM文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sprezzat</groupId>
  <artifactId>app</artifactId>
  <version>1.0.0</version>
  <packaging>aar</packaging>
  <dependencies>
    <dependency>
      <groupId>com.bugsense.trace</groupId>
      <artifactId>bugsense</artifactId>
      <version>3.6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.2.4</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

最后使用上面的库作为依赖项来我的android应用程序的gradle配置:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    mavenLocal()
}

android {
    compileSdkVersion 15
    buildToolsVersion "19.0.2"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.somepackage:LIBRARY_NAME:1.0.0@aar'
}
Run Code Online (Sandbox Code Playgroud)

在手机上部署应用程序后,我正在获取NoClassDefFoundError属于我的android库的编译依赖项的类.

使用gradle dependencies以下方法检查我的android应用程序依赖项:

apk - Classpath packaged with the compiled main classes.
+--- com.google.android.gms:play-services:+ -> 4.3.23
|    \--- com.android.support:support-v4:19.0.1 -> 19.1.0
+--- com.android.support:appcompat-v7:+ -> 19.1.0
|    \--- com.android.support:support-v4:19.1.0
\--- com.somepackage:LIBRARY_NAME:1.0.0
Run Code Online (Sandbox Code Playgroud)

根据上面的树,没有检测到所有传递依赖性.问题在哪里以及如何正确完成?

mko*_*zun 85

我通过transitive为我的aar依赖设置属性来解决我的问题:

compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar'){
    transitive=true
}
Run Code Online (Sandbox Code Playgroud)

  • @PeterNiederwieser省略`@ aar`会导致Gradle尝试将工件作为.jar文件获取.这会破坏构建. (3认同)
  • 它对我没用.我有确切的问题.我有2个库,其中一个正在使用另一个.`compile project(':LeafPeripheralsContract'){transitive = true}`不起作用.它抱怨传递.我创建了一个'aar`并试图为它添加传递.它并没有抱怨,但它也没有包含在其他aar包中. (3认同)
  • 如果你同时拥有.jar和.aar工件,这是使用@aar并包含传递的唯一解决方案. (2认同)

小智 18

你不应该使用"@aar",如果使用"@"成为" Artifact only notation ",如果你想使用"@"并希望有依赖传递,你应该添加"transitive = true"

  • 这个答案很有帮助.我之前的评论中有一个拼写错误,我删除了那个.谢谢你的回答,祝你有个美好的一天:). (2认同)

suh*_*_sm 14

如果您在本地使用aar,请尝试此操作:

compile(project(:your-library-name)) {
    transitive=true
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨,它不适合我.我创建了一个内部使用volley库的库项目.我在我的应用程序中包含了使用库项目创建的aar文件.我收到"错误:(8,26)错误:包com.android.volley不存在"错误.在我的图书馆项目中,我使用了编译(项目(':volley')){transitive = true} (6认同)
  • 嘿Manish,面对同样的问题,你找到了解决办法吗? (2认同)

Rea*_*hed 7

我遇到了类似的问题,觉得我可以分享解决问题的步骤。

在发布自己的依赖项时无法使用传递依赖项的基本思想aar实际上是没有.pom使用预期的传递依赖项生成文件。

我正在'maven-publish'为我的 androidaar依赖项使用插件将它发布到我自己的私有 Maven 存储库中。当我的其他项目aar在他们的build.gradle. 因此,在这里我.pom在发布我的aar.

这里需要注意的重要一点是,您希望具有传递行为的依赖项应使用api库项目build.gradle文件中的导入,如下所示。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api 'com.android.volley:volley:1.0.0'
    api "com.google.code.gson:gson:$globalGsonVersion"
}
Run Code Online (Sandbox Code Playgroud)

现在正如我之前所说,我使用maven-publish插件来发布aar依赖项,因此我在 gradle 中的发布任务如下所示。

publishing {
    publications {
        mavenAar(MavenPublication) {
            from components.android
        }

        mavenJava(MavenPublication) {
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                // Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.api.allDependencies.each {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }

    repositories {
        maven {
            // Your repository information goes here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,我使用另一个mavenJava任务.pom在我的私有 Maven 存储库中发布文件,以便当aar文件作为依赖项添加到其他模块时,它获取.pom信息并下载传递依赖项。

要完成答案,您应该如何在build.gradle文件中添加依赖项,以便您自己发布aar给我导入。

api('com.example.masudias:my_lib:1.0.0@aar') {
    transitive = true
}
Run Code Online (Sandbox Code Playgroud)

  • `这里需要注意的重要一点是,您想要具有传递行为的依赖项应该使用库项目的 build.gradle 文件中的 api 导入,如下所示。`天哪,谢谢。 (2认同)

yoA*_*ex5 6

传递依赖

transitive意味着消费者(例如应用程序)包括一个生产者和所有生产者的依赖项(例如库)。它会增加构建时间,并且可能会导致依赖版本出现一些问题

默认情况下,Gradle 依赖项有 transitive = true

api ('com.package:library:0.0.1')
//the same
api ('com.package:library:0.0.1') {
    transitive = true
}
Run Code Online (Sandbox Code Playgroud)

当你使用@artifact notation它时有transitive = false

api ('com.package:library:0.0.1@aar')
//the same
api ('com.package:library:0.0.1@aar') {
    transitive = false
}
Run Code Online (Sandbox Code Playgroud)