android中的gradle依赖错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25488388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:29:29  来源:igfitidea点击:

gradle dependency error in android

androidandroid-recyclerviewandroid-gradle-plugin

提问by nomongo

In the following build.gradle, I added the configuration section to avoid double inclusion of support libraries. Support libraries are used in the main project and in the dependent projects like facebook sdk. Without the configuration section, I get "UNEXPECTED TOP-LEVEL EXCEPTION". Adding that configuration makes the error go away and the app all works fine.

在下面的 build.gradle 中,我添加了配置部分以避免双重包含支持库。支持库用于主项目和依赖项目,如 facebook sdk。如果没有配置部分,我会得到“意外的顶级异常”。添加该配置会使错误消失,应用程序一切正常。

Now, I'm trying to add RecyclerView to my app and I get RecyclerView class not found while inflating the recyclerview (although it builds ok). If I remove the facebook SDK and configuration section, the recyclerview works just fine.

现在,我正在尝试将 RecyclerView 添加到我的应用程序中,但在给 recyclerview 充气时找不到 RecyclerView 类(尽管它构建正常)。如果我删除 facebook SDK 和配置部分,recyclerview 工作得很好。

Question: What changes can I make to the build.gradle to make the facebook SDK work and RecyclerView work? In other words, why is the config section excluding v7 when it is only supposed to exclude v4?

问题:我可以对 build.gradle 进行哪些更改以使 facebook SDK 工作和 RecyclerView 工作?换句话说,为什么 config 部分只应该排除 v4 而排除 v7?



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:support-v13:+'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.google.android.gms:play-services:4.4.52'
    compile project(':facebook-3.15')
    compile project(':parse-1.5.1')
    compile project(':viewpagerindicator-2.4.1')
}

configurations {
    // to avoid double inclusion of support libraries
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

采纳答案by nomongo

Found a solution to this:

找到了一个解决方案:

  1. Removed the config section in the build.gradlethat excludes support-v4

  2. It turns out that .aarfiles are basically a zip, so removed the support-v4 jar from the dependency .aar library (using 7-zip). And now, I don't get the top-level exception and at the same time able to use recyclerview-v7.

  1. 删除了build.gradle中排除的配置部分support-v4

  2. 事实证明,.aar文件基本上是一个 zip,因此从依赖项 .aar 库中删除了 support-v4 jar(使用 7-zip)。现在,我没有得到顶级异常,同时能够使用recyclerview-v7.

If you are using dependency projects instead of .aar files, try removing the support-v4.jar files in the dependency projects before compiling.

如果您使用的是依赖项目而不是 .aar 文件,请在编译前尝试删除依赖项目中的 support-v4.jar 文件。

Shouldn't the gradle build tool be intelligent enough to exclude the duplicate packages rather than having the users go thru this kind of workarounds and headaches?

难道 gradle 构建工具不应该足够智能以排除重复的包,而不是让用户经历这种变通方法和头痛吗?

回答by Kevin Coppock

If you're having a dependency conflict with the v4 support library, you can just exclude it from one of the libraries via the gradle script:

如果您与 v4 支持库存在依赖项冲突,则可以通过 gradle 脚本将其从库之一中排除:

compile ('com.android.support:recyclerview-v7:+') {
    exclude module: 'support-v4'
}

回答by Ultimo_m

I fixed that adding:

我修复了添加:

compile ('com.facebook.android:facebook-android-sdk:3.22.0@aar'){
    exclude module: 'support-v4'
}