我正在向我的应用程序添加导航抽屉。我收到错误。应用程序 gradle 同步得很好。但是当我运行该应用程序时,我收到一堆重复的类错误。我认为这可能是因为我添加了冲突的依赖项,并且我使用的是 v7 28.0.0,并且一些错误提到了 app: v4。我在网上看到的所有示例都使用 v7 28.0.0,尽管我在 main_activity.xml 中使用了 v4。不知道是否与错误有关。 android.support.v4.widget.DrawerLayout
Caused by: com.android.ide.common.workers.WorkerExecutorException: 1 exception was raised by workers:
java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$Delegate found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$DelegateProvider found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$SlideDrawable found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat$1 found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat$OnRequestPermissionsResultCallback found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "org.pctechtips.netdroid"
minSdkVersion 21
targetSdkVersion 28
versionCode 8
versionName "1.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = false
signingConfig signingConfigs.config
}
buildTypes {
release {
shrinkResources false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
/*androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
firebase
implementation 'com.google.firebase:firebase-core:10.2.1'
})*/
// compile 'com.android.support:appcompat-v7:25.3.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
/*google play plugin for adMob*/
implementation 'com.google.android.gms:play-services:10.2.1'
implementation 'commons-net:commons-net:3.6'
implementation 'org.samba.jcifs:jcifs:1.3.3'
}
异常意味着,在 2 个或更多不同的依赖项中存在重复的类,编译器将无法区分应在
run-time
使用其中的哪一个,并引发异常。
最常见的是,当您尝试导入带有所需库的模块时,就会发生口是心非的情况。 (传递依赖)
您必须从exclude
中的库中
build.gradle
复制类。 如 Log 所示,
support-core-ui
和
support-compat
模块具有重复的类。
apply plugin: 'com.android.application'
android {
...
defaultConfig {
...
}
buildTypes {
...
}
configurations {
all { // You should exclude one of them not both of them
exclude group: "com.android.support", module: "support-core-ui"
exclude group: "com.android.support", module: "support-compat"
}
}
}
有时你不需要排除任何东西,只需要将导入的模块更改为不带依赖项的模块即可。
导致重复类的其他情况是当您将*.jar
添加到项目
libs
目录时。因此,如果项目中不再使用它们,则需要删除它们。
project->app->libs->*.jar
我看到提到的一些解决方案使用这两行可以解决问题,但如果您已迁移到Androidx
,它将默认启用。
android.useAndroidX=true
android.enableJetifier=true
喷射器是
Jetifier工具迁移support-library依赖的库来依赖 而是使用等效的 AndroidX 包。该工具可让您迁移 直接使用单个库,而不是使用 Android gradle 与 Android Studio 捆绑的插件。有关更多信息,请查看
随着应用程序范围的扩大,它可以包含许多依赖项 包括直接依赖和传递依赖(库 您的应用程序的导入库所依赖的)。排除传递性 您不再需要的依赖项,您可以使用如果您在排除类时遇到问题,请检查此线程:
exclude
关键词
implementation 'com.android.support:support-v4:28.0.0'
android.useAndroidX=true
android.enableJetifier=true
我通过创建一个具有相同项目和包名称的新项目,然后将文件从前一个项目复制到新项目来解决这个问题。
com.google.android.gms:play-services
更新至最新版本。它会起作用的。
对我来说,它是这样的
// Force androidx.work:work-runtime andwork-runtime-ktx to use version 2.8.0
// due to transitive dependancy conflict with datadoghq
api(group: "androidx.work", name: "work-runtime") {
version {
strictly "2.8.0"
}
}
api(group: "androidx.work", name: "work-runtime-ktx") {
version {
strictly "2.8.0"
}
}