19

在 Android Studio 3.0 Canary 4 中设置 dagger 2.x 时出现以下错误

错误:(71, 20) 无法解析:com.google.dagger:dagger:2.x

错误:(73, 20) 无法解决:com.google.dagger:dagger-android:2.x

错误:(74, 20) 无法解决:com.google.dagger:dagger-android-support:2.x

我的构建文件如下所示:

dependencies {
    //For DI - Dagger 2
    implementation 'com.google.dagger:dagger:2.x'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
    implementation 'com.google.dagger:dagger-android:2.x' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
}

项目构建文件有以下片段

allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

感谢您的帮助...

4

2 回答 2

43

如果你像我一样陷入这个问题,这就是我为摆脱这种情况所做的。

我去https://github.com/google/dagger/releases找出最新的 dagger 发布版本,发现 v2.11 是最新的。我在构建文件中此库配置的版本部分将 2.x 替换为 2.11,并且构建成功。

dependencies {
    //For DI - Dagger 2
    implementation 'com.google.dagger:dagger:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
    implementation 'com.google.dagger:dagger-android:2.11' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}
于 2017-06-25T18:35:05.223 回答
14

编辑:我完全同意提到使用该库的特定版本而不是 + 的评论,因此例如应该使用 2.11 而不是 2.+ 2.+ 旨在解决 2.x 的问题,因为大多数初学者就像我第一次使用它时一样,从字面上看 2.x。这里的 x 表示稳定版本的最新次要版本。请检查最新的发行说明并将 x 替换为最新的次要版本的库。

原始答案:我相信您现在已经解决了您的问题,尽管在尝试了其他几个和这个之后,我找到了一个可靠的解决方案并将其发布以帮助他人。而不是 2.x 使用 2.+。

它为我解决了所有问题,不仅解决了上述问题,还确保提供最新版本的 dagger 2.x。

它应该如下所示:

dependencies {
    implementation 'com.google.dagger:dagger:2.+'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.+'
    implementation 'com.google.dagger:dagger-android:2.+' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.+' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.+'
}

谢谢!

于 2018-04-04T10:20:55.177 回答