简体   繁体   English

Android Gradle库依赖关系以及使用Nexus的库依赖关系

[英]Android Gradle library dependency with library dependency using Nexus

I'm switching my project over to using Gradle and an internal SonaType Nexus for hosting my dependencies. 我将我的项目切换到使用Gradle和内部SonaType Nexus托管我的依赖项。 My core project depends on library project A and library project A has a dependency on library project B. 我的核心项目依赖于库项目A,而库项目A依赖于库项目B。

My issue is that as soon as I add LibA to my main project I get this error: "Module version com.example:LibA:1.1 depends on libraries but is not a library itself" 我的问题是,一旦我将LibA添加到主项目中,我就会收到此错误:“模块版本com.example:LibA:1.1取决于库,但本身不是库”

I have no issues adding library projects with jar dependencies with the same build script. 使用相同的构建脚本添加具有jar依赖项的库项目时,我没有任何问题。 I have seen people doing this successfully with LOCAL (in the project) android libraries but no one doing it with maven repos. 我见过有人使用LOCAL(在项目中)android库成功完成此操作,但没有人使用maven repos做到这一点。

Is this a bug in gradle or did I misconfigure the library builds? 这是gradle中的错误,还是我没有正确配置库构建?

Core Project Build 核心项目建设

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 Build 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 Build 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 {
    } ...

Edit: Adding -info output for the error. 编辑:为错误添加-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

Edit 2: Adding my local maven upload script for LibA 编辑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
        }
    }
}

Your line in the dependencies to include LibA is wrong. 您在依赖项中包含LibA的行是错误的。 To include a library project, use this: 要包含一个库项目,请使用以下命令:

compile project(':LibA')

If the library's directory isn't at the root of your project directory, you'll need to specify a colon-delimited path. 如果库目录不在项目目录的根目录下,则需要指定一个以冒号分隔的路径。 For example, if your directory structure is: 例如,如果您的目录结构是:

projectFolder
  |
  +--coreProject
  |
  +--libraries
      |
      +--LibA
      |
      +--LibB

your dependency will be: 您的依赖关系将是:

compile project(':libraries:LibA')

This is the same as the notation you use in your settings.gradle file. 这与您在settings.gradle文件中使用的表示法相同。

Maybe problem is that you use mavenCentral as your repository for library projects 可能的问题是您将mavenCentral用作库项目的存储库

repositories {
    mavenCentral()
}

and not yours nexus repository where actual dependencies exists 而不是您存在实际依赖关系的nexus存储库

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

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

如果您同时上传了jar和aar的库工件,请尝试此操作。

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

In my work, I have used compile project(':google-play-services_lib') instead of compile ('google-play-services_lib') when I declare dependent projects in my build.gradle file. 在我的工作中,当我在build.gradle文件中声明依赖项目时,我使用了compile project(':google-play-services_lib')而不是compile ('google-play-services_lib') I think that is the right way to do this with Gradle: http://www.gradle.org/docs/current/userguide/dependency_management.html#sub:project_dependencies 我认为这是使用Gradle执行此操作的正确方法: http : //www.gradle.org/docs/current/userguide/dependency_management.html#sub :project_dependencies

if you don't want to have it as sub-module in the first build.gradle file you can add your local maven repository 如果您不想在第一个build.gradle文件中将其作为子模块,则可以添加本地Maven存储库

mavenLocal() mavenLocal()

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

but you need to run install on libA first. 但是您需要首先在libA上运行install。

Don't know for sure, just a couple of thoughts: 不确定,只有几个想法:

  1. Have you tried running gradle assemble instead gradle build ? 您是否尝试过运行gradle assemble而不是gradle build This should skip tests, as I see error is related to test task. 这应该跳过测试,因为我看到错误与测试任务有关。
  2. Maybe stupid, but try to remove dependcy on 2nd lib from the first and put it to your main build file listing before the first. 也许很愚蠢,但是请尝试从第一个库中删除对第二个库的依赖,并将其放在第一个库之前的主构建文件列表中。 I have a memory of something related. 我有一些相关的记忆。 This way the second lib may be added to classpath allowing the first to compile. 这样,第二个库可以添加到类路径中,从而允许第一个库进行编译。
  3. Try to create .aar files by hand and upload it to repo also by hand. 尝试手动创建.aar文件,然后手动将其上传到.aar
  4. It's a hack, but maybe it'll work: have you considered to exclude this :GradleTest module? 这是一个技巧,但也许会奏效:您是否考虑过排除此:GradleTest模块? See section 50.4.7 50.4.7节

I had a similar error message after introducing by mistake a cyclic dependency between libraries: 错误地引入了库之间的循环依赖关系后,我收到了类似的错误消息:

build.gradle in commons-utils 在commons-utils中的build.gradle

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

build.gradle in test-utils 在test-utils中的build.gradle

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

Fixing this solved the problem. 解决此问题已解决。 The error message is not very explicit. 该错误信息不是很明确。

This issue has gone away with the later versions of Gradle and the Android Gradle Plugin. 更高版本的Gradle和Android Gradle插件已不再解决此问题。 Seems to have just been an early release bug. 似乎只是早期发行版的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM