Android 报错:Duplicate class android.support.v4

最近将 Android Studio 升级到3.5版本后打开以前的项目并升级了 Gradle 后报错如下

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:22.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:22.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:22.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:22.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:22.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:22.0.0)
Duplicate class android.support.v4.app.ActivityCompat$SharedElementCallback21Impl found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.ActivityManagerCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.ActivityOptionsCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.BackStackRecord found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.BackStackRecord$Op found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.BackStackState found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.BackStackState$1 found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.DialogFragment found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.Fragment found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.Fragment$1 found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.Fragment$InstantiationException found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)
Duplicate class android.support.v4.app.Fragment$SavedState found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.0.0)

解决方案

gradle.properties中添加下面两行代码以选择androidX作为您的支持库

android.useAndroidX=true
android.enableJetifier=true
最后修改:2019 年 10 月 15 日 12 : 01 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论