程序类型已存在:android.support.v4.app.BackStackRecord

58
我已经升级了我的Android Studio,但在最新版本中遇到了很多问题。
虽然有许多类似的问题,我检查了所有答案,但没有一个适用于我!
以下是编译代码时出现的错误:
Program type already present: android.support.v4.app.BackStackRecord$Op Message{kind=ERROR, text=Program type already present: android.support.v4.app.BackStackRecord$Op, sources=[Unknown source file], tool name=Optional.of(D8)}
这里是我的gradle文件:
项目:
// Top-level build file where you can add configuration options common to 

all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.0'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
 }

 allprojects {
 repositories {
    google()
    jcenter()
    maven {
        url "https://jitpack.io"
          }
     }
}

task clean(type: Delete) {
delete rootProject.buildDir
}

应用程序:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.alcantara.bugismart"
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
            }
           }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso- 
    core:3.0.1'
    implementation 'com.github.ViksaaSkool:AwesomeSplash:v1.0.0'
}

如果有什么需要补充的,可以告诉我,以便更好地理解我的工作或发现错误。

10个回答

90

程序类型已经存在:android.support.v4.app.BackStackRecord$Op Message{kind=ERROR, text=程序类型已经存在:android.support.v4.app.BackStackRecord$Op,来源=[未知源文件],工具名称=Optional.of(D8)}

问题出现是因为存在重复的support库。这个依赖项:

implementation 'com.github.ViksaaSkool:AwesomeSplash:v1.0.0'

正在使用旧版本的支持库。如果您已经拥有支持库,请尝试将其排除在外:

// support libraries we want to use
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'

// we already have the specific support libraries. So, exclude it
implementation ('com.github.ViksaaSkool:AwesomeSplash:v1.0.0') {    
    exclude group: 'com.android.support'
    exclude module: 'appcompat-v7'
    exclude module: 'support-v4'
}

您需要使用以下命令检查依赖项:

./gradlew app:dependencies

或者,您可以通过添加与您想要的确切版本发生冲突的库来始终覆盖支持库的版本。


更新

添加构建依赖项文档中有一个完整步骤来修复依赖关系解析错误。以下是摘录:

修复依赖关系解析错误

当您向应用项目添加多个依赖项时,这些直接和传递依赖项可能会相互冲突。Android Gradle插件试图优雅地解决这些冲突,但某些冲突可能会导致编译时或运行时错误。

为了帮助您调查哪些依赖项导致错误,请检查您的应用程序的依赖树并查找出现多次或具有冲突版本的依赖项。

如果您无法轻松识别重复的依赖项,可以尝试使用Android Studio的UI搜索包含重复类的依赖项,方法如下:

  1. 从菜单栏中选择导航 >
  2. 在弹出的搜索对话框中,确保选中包括非项目项旁边的框。
  3. 键入在构建错误中出现的类的名称。
  4. 检查结果以获取包含该类的依赖项。

以下各节介绍了可能遇到的不同类型的依赖关系解析错误及其修复方法。

修复重复类错误

如果一个类在运行时类路径上出现多次,则会出现以下类似的错误:

Program type already present com.example.MyClass

通常出现这个错误是由于以下情况之一:

  • 二进制依赖项包含一个库,而您的应用程序也将其作为直接依赖项。例如,您的应用程序声明了对Library A和Library B的直接依赖关系,但Library A已经在其二进制文件中包含了Library B。

    • 要解决此问题,请将Library B作为直接依赖项删除。
  • 您的应用程序具有本地二进制依赖项和远程二进制依赖项,它们都使用相同的库。

    • 要解决此问题,请删除其中一个二进制依赖项。

解决类路径之间的冲突

当Gradle解析编译类路径时,它首先解析运行时类路径,并使用结果确定应添加到编译类路径的依赖项版本。换句话说,运行时类路径确定下游类路径上相同依赖项的所需版本号。

您的应用程序的运行时类路径还确定了Gradle在应用程序测试APK的运行时类路径中所需的匹配依赖项的版本号。类路径的层次结构如下图所示:

enter image description here

当多个类路径中出现同一依赖项的不同版本时,例如您的应用程序使用implementation 依赖配置的某个版本,并且库模块使用runtimeOnly配置的另一个版本,则可能会发生冲突。

在解析运行时和编译时类路径上的依赖项时,Android Gradle插件3.3.0及更高版本尝试自动修复某些下游版本冲突。例如,如果运行时类路径包括Library A版本2.0,并且编译类路径包括Library A版本1.0,则插件会自动更新编译类路径上的依赖项为Library A版本2.0,以避免错误。

但是,如果运行时类路径包括Library A版本1.0,并且编译类路径包括Library A版本2.0,则插件不会将编译类路径上的依赖项降级为Library A版本1.0,您仍然会收到类似以下错误的错误消息:

Conflict with dependency 'com.example.library:some-lib:2.0' in project 'my-library'.
Resolved versions for runtime classpath (1.0) and compile classpath (2.0) differ.
为解决此问题,请执行以下操作之一:
  • 将所需版本的依赖项作为 api 依赖项包含到库模块中。也就是说,只有库模块声明了依赖项,但应用程序模块也可以通过传递性访问其 API。
  • 或者,您可以在两个模块中都声明依赖项,但要确保每个模块使用相同版本的依赖项。考虑配置项目级属性以确保整个项目中每个依赖项的版本保持一致。

@KurniawanAlcantara:不客气;)。如果我的答案解决了您的问题,您可以将其标记为已接受的答案。 - ישו אוהב אותך

39
在你的应用模块的build.gradle文件中添加以下内容:
implementation 'com.android.support:support-v4:27.1.1'

最新版本的URL为https://developer.android.com/topic/libraries/support-library/packages。 - Vyacheslav

10

有一个替代的方法来告诉gradle强制使用更新的版本:

final SUPPORT_LIB_VER = '27.1.1'

configurations.all {
    resolutionStrategy { 
        force "com.android.support:appcompat-v7:${SUPPORT_LIB_VER}"
        force "com.android.support:support-v4:${SUPPORT_LIB_VER}"
    }
}

当您有许多依赖项时,这可能会更加方便。

另请参见:回答“如何强制Gradle为两个依赖项设置相同的版本?”


9
在您的gradle文件的依赖项部分添加这行代码。
implementation 'com.android.support:support-v4:28.0.0'

1
是的,这个答案可行,@Areeba,你是怎么得到它的,参考资料? - Akshay Vijay Jain
@AkshayVijayJain,我猜这可能与类似的Lint警告有关,请看一下:“所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。发现版本28.0.0、21.0.3。例如,com.android.support:animated-vector-drawable:28.0.0和com.android.support:support-v4:21.0.3等...(Ctrl+F1) 有一些库或工具与库的组合是不兼容的,或者可能导致错误。” - JorgeAmVF

4

根据您的"SdkVersion",将此代码添加到gradle中:

implementation 'com.android.support:support-v4:28.0.0'

例如,我的SDK版本是28,那么我使用以下代码:
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

2

请检查您的 libs 文件夹,因为:

implementation fileTree(dir: 'libs', include: ['*.jar'])

它将从那里选择所有的JAR文件。您可能会有重复的JAR文件或较低版本的实现。

我的库中有重复的内容,导致了这个问题。


检查 libs 文件夹,然后删除 support-v4.jar。 - Thunder knight

1
在我的情况下,我将一个非常老的Eclipse项目转换为Android Studio。因此,自动迁移使用了:
dependencies {
    compile files('libs/android-support-v13.jar')
}

只需移除或注释掉包含此库的那一行。

当我注释掉之后,Gradle脚本成功执行了。


0
用它替换你的依赖项。
implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

0

输入图像描述 我尝试了以下方法:

  • 清理/重建
  • 使缓存失效并重新启动
  • 在我的app.gradle中排除依赖项库(见下文)

implementation (project(':libxmiimp')) {exclude group: 'com.android.support', module: 'support-v4'}

  • 调整gradle设置(例如启用multiDex等)

但我的问题是Android-support-v4.jar不知何故进入了我的本地android库的lib文件夹....删除它解决了我的问题


0

multiDexEnabled true 在 gradle-app 的 android 块中添加上述行,对我很有效...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接