为什么在运行Android项目时会出现重复的类 [英] Why I'm Getting Duplicate Class When Running My Android Project
问题描述
我正在向我的应用添加导航抽屉.而且我遇到了错误.应用程序gradle同步就好了.但是当我运行该应用程序时,出现了一堆重复的类错误.我认为这可能是因为我添加了相互冲突的依赖项,并且我正在使用v7 28.0.0,并且某些错误提到了应用程序:v4.我在网上看到的所有示例都使用v7 28.0.0,尽管我在使用v4的main_activity.xml中也有.不知道是否与错误有关. android.support.v4.widget.DrawerLayout
I'm in the process of adding a navigation drawer to my app. and I'm getting errors. The app gradle synchs just fine. but when I run the app I get a bunch of duplicate class error. I think it might be because I have conflicting dependencies added and that I'm using v7 28.0.0 and some of the errors mention app: v4. all the examples I've seen online use v7 28.0.0 eventhough I have this in main_activity.xml which uses v4. don't know if it's got something to do with the error. 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)
渐变文件
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
中使用哪个类.并且引发了异常.
The exception means, There were duplicated classes in 2 or more different dependencies and compiler wouldn't be able to distinguish which of them should be used at run-time
and the exception was thrown.
最常见的,当您尝试导入带有所需库的模块时,会发生重复.(传递依赖项)
Most often, Duplicity happens when you are trying to import modules that carrying their required libraries.(transitive dependencies)
您必须exclude
从build.gradle
中的库复制类.
如Log所示,support-core-ui
和support-compat
模块具有重复的类.
You have to exclude
duplicated classes from libraries in the build.gradle
.
As Log shows, support-core-ui
and support-compat
modules have duplicated classes.
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"
}
}
}
有时您不需要排除任何内容,而只需将导入的模块更改为不带依赖项的模块即可.
Sometimes you don't need to exclude anything and you only need to change imported module to that one that does not bring its dependencies.
导致重复类的其他情况是将*.jar
添加到项目libs
目录中的情况.因此,如果它们没有在项目中开始使用,则需要将其删除.
Other situation that causes duplicated classes is when you have add *.jar
to the project libs
directory. Therefore, You need to delete them if they are not begin used in the project.
project->app->libs->*.jar
我看到有提到使用这2行的解决方案可以解决此问题,但是如果您迁移到Androidx
,则默认情况下将启用它.
i see there are some solutions mentioned using these 2 lines will resolve the problem But if you've migrated to Androidx
it would be enabled by default.
android.useAndroidX=true
android.enableJetifier=true
Jetifier是
Jetifier工具迁移依赖于支持库的库以依赖 等效的AndroidX软件包.该工具可让您迁移 直接使用单个库,而不是使用Android gradle 与Android Studio捆绑在一起的插件.
Jetifier tool migrates support-library-dependent libraries to rely on the equivalent AndroidX packages instead. The tool lets you migrate an individual library directly, instead of using the Android gradle plugin bundled with Android Studio.
有关更多信息,请查看排除传递依赖项
And for more information take a look at Exclude transitive dependencies
随着应用程序范围的扩大,它可能包含许多依赖项 包括直接依赖关系和传递依赖关系(库 应用程序的导入库所依赖的库).排除传递 不再需要的依赖项,可以使用
exclude
关键字
As an app grows in scope, it can contain a number of dependencies including direct dependencies and transitive dependencies (libraries which your app's imported libraries depend on). To exclude transitive dependencies that you no longer need, you can use the
exclude
keyword
If you have problems excluding classes, check this thread: How do i exclude...
这篇关于为什么在运行Android项目时会出现重复的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!