【问题标题】:Android Gradle library dependency with library dependency using NexusAndroid Gradle 库依赖与使用 Nexus 的库依赖
【发布时间】:2013-12-02 10:07:13
【问题描述】:

我正在将我的项目切换到使用 Gradle 和内部 SonaType Nexus 来托管我的依赖项。我的核心项目依赖库项目A,库项目A依赖库项目B。

我的问题是,一旦我将 LibA 添加到我的主项目中,我就会收到此错误: "模块版本 com.example:LibA:1.1 依赖于库但本身不是库"

使用相同的构建脚本添加具有 jar 依赖项的库项目没有问题。我见过有人用 LOCAL(在项目中)android 库成功地做到了这一点,但没有人用 maven repos 做到这一点。

这是 gradle 中的错误还是我错误地配置了库构建?

核心项目构建

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

repositories {
    maven {
        url "http://localhost:8081/nexus/content/repositories/releases/"
    }

    maven {
        url "http://localhost:8081/nexus/content/repositories/central/"
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }
}

dependencies {
    compile 'com.android.support:support-v4:+'
    compile('com.example:LibA:1.+')
}

LibA 构建

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17

        versionCode = "3"
        versionName = "1.2"
    }

    android {
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aild.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
            }

        }
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        compile ('com.example:LibB:1.+')
    } ...

LibB 构建

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17

        versionCode = "1"
        versionName = "1.0"
    }

    android {
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aild.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
            }

        }
    }

    repositories {
        mavenCentral()
    }

    dependencies {
    } ...

编辑:为错误添加 -info 输出。

* What went wrong:
A problem occurred configuring project ':GradleTest'.
> Failed to notify project evaluation listener.
   > Module version com.example:LibA:1.+ depends on libraries but is not a library itself

编辑 2:为 LibA 添加我的本地 maven 上传脚本

apply plugin: 'maven'
apply plugin: 'signing'

group = "com.example"
version = defaultConfig.versionName

configurations {
    archives {
        extendsFrom configurations.default
    }
}

signing {
    required { has("release") && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}


uploadArchives {
    configuration = configurations.archives
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
        repository(url: sonatypeRepo) {
            authentication(userName: sonatypeUsername,
                    password: sonatypePassword)
        }

        pom.project {
            name 'com-example'
            packaging 'aar'
            description 'none'
            url 'https://internal github link'

            scm {
                url 'scm:git@https://internal github link'
                connection 'git@https://internal github link'
                developerConnection 'git@https://internal github link'
            }

            licenses {
                license {
                    name 'example'
                    url 'example'
                    distribution 'example'
                }
            }

            developers {
                developer {
                    id 'example'
                    name 'example'
                    email 'example'
                }
            }

            groupId "com.example"
            artifactId rootProject.name //LibA
            version defaultConfig.versionName
        }
    }
}

【问题讨论】:

标签: android maven gradle android-studio


【解决方案1】:

这个问题已经随着 Gradle 和 Android Gradle 插件的更新版本而消失。似乎只是一个早期发布的错误。

【讨论】:

    【解决方案2】:

    如果您为 jar 和 aar 都上传了库工件,请试试这个。

    compile 'com.example:LibA:1.1.1@aar'
    

    【讨论】:

      【解决方案3】:

      不确定,只是一些想法:

      1. 您是否尝试过运行gradle assemble 而不是gradle build?这应该跳过测试,因为我看到错误与测试任务有关。
      2. 也许很愚蠢,但请尝试从第一个库中删除对第二个库的依赖,并将其放在第一个之前的主构建文件列表中。我有一些相关的记忆。这样可以将第二个库添加到允许第一个库编译的类路径中。
      3. 尝试手动创建 .aar 文件并手动将其上传到 repo。
      4. 这是一个 hack,但也许它会起作用:您是否考虑过排除这个 :GradleTest 模块?见section 50.4.7

      【讨论】:

      • 在 assemble 时遇到了同样的错误,我相信其中一个项目实际上称为 GradleTest。 2)我还没有尝试过……但这违背了依赖管理的全部目的——我的项目不应该知道其他项目的依赖。
      【解决方案4】:

      在库之间错误地引入循环依赖后,我收到了类似的错误消息:

      commons-utils 中的 build.gradle

      dependencies {
          ...
          instrumentTestCompile project(':test-utils')
      }
      

      test-utils 中的 build.gradle

      dependencies {
          ...
          compile project(':commons-utils')
      }
      

      解决了这个问题。错误信息不是很明确。

      【讨论】:

        【解决方案5】:

        也许问题是您使用 mavenCentral 作为库项目的存储库

        repositories {
            mavenCentral()
        }
        

        而不是你的存在实际依赖关系的关系库

        repositories {
            maven {
                url "http://localhost:8081/nexus/content/repositories/releases/"
        }
        
            maven {
                url "http://localhost:8081/nexus/content/repositories/central/"
            }
        }
        

        【讨论】:

          【解决方案6】:

          如果您不想将其作为第一个 build.gradle 文件中的子模块,您可以添加本地 maven 存储库

          mavenLocal()

          //repositories
          repositories {
              mavenCentral()
              mavenLocal()
          }
          

          但您需要先在 libA 上运行安装。

          【讨论】:

          • Luis,我很确定使用我的自定义 maven 存储库与本地 maven 的过程完全相同——这仍然会产生我上面遇到的错误。
          【解决方案7】:

          您在依赖项中包含 LibA 的行是错误的。要包含库项目,请使用:

          compile project(':LibA')
          

          如果库的目录不在项目目录的根目录下,则需要指定一个以冒号分隔的路径。例如,如果你的目录结构是:

          项目文件夹 | +--核心项目 | +--库 | +--LibA | +--LibB

          你的依赖将是:

          compile project(':libraries:LibA')
          

          这与您在 settings.gradle 文件中使用的表示法相同。

          【讨论】:

          • 据我所知,这是用于本地依赖项的方法,即代码实际位于项目结构中的依赖项。在我的示例中,我使用从 mavencentral 和自定义 maven 存储库中提取的远程依赖项。我的目标是避免在我的项目中使用本地代码,这样我就不需要子模块来更新它们。
          • 哦,好的。您可以从命令行运行构建并添加 --info 命令行选项吗?这可能会更清楚地说明出了什么问题。可能是您的本地 Maven 存储库设置不正确,Gradle 找不到 LibA。您是如何设置本地 Maven 存储库的?
          • 是的,本地 maven 存储库适用于所有常规 android 库和 jar。在添加具有另一个 android 库项目作为依赖项的库项目时,我只会收到上面的错误消息。在原始帖子中添加了 -info 错误。
          • 您如何将 LibA 部署到本地 Maven 存储库中?如果您只是想将库项目放到那里,它将无法正常工作——您必须配置 Gradle 构建脚本才能将 AAR 部署到存储库中。 gradle.org/docs/current/userguide/maven_plugin.html中有一些相关信息
          • 我会把上面的代码贴出来,如果有错误请告诉我。
          【解决方案8】:

          在我的工作中,当我在 build.gradle 文件中声明依赖项目时,我使用了 compile project(':google-play-services_lib') 而不是 compile ('google-play-services_lib')。我认为这是使用 Gradle 执行此操作的正确方法:http://www.gradle.org/docs/current/userguide/dependency_management.html#sub:project_dependencies

          【讨论】:

          • 这些是 maven 托管的 gradle 项目而不是本地源 gradle 项目,如果您继续阅读链接的部分,您也会看到类似的依赖声明。
          • 我明白了。要尝试的另一件事是列出每个库项目的依赖项 (gradle.org/docs/current/userguide/…),然后列出核心项目并查看差异并进行调试。
          • 我在核心项目上运行了该命令,当我尝试构建时它实际上出现了同样的错误,因此我无法收集任何其他信息。
          • 我在 aar 文件上方看到了 Scott 的帖子,它触发了我的记忆。看看这个帖子:flexlabs.org/2013/06/…,可能会有帮助。
          • 感谢您的链接,但我没有遇到这个问题,因为我的 aars 实际上已经“远程”或由 maven 提供服务(这是自定义关系)。问题是当我添加具有库依赖项的库时会发生什么。这只是添加了一个简单且工作正常的库依赖项。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多