How to fix 'Duplicate classes' in Android Studio

Viewed 7912

I'm making a barcode scanner application in Android Studio and I use the zxing library for it. The problem if I am adding the library and want to build my project, the 'app:checkDebugDuplicateClasses' step won't work and many 'duplicate classes' errors appear in the console.

I already tried to fix it with thousands of little steps in 'build.gradle' but it all didn't work.

That's my build.gradle file:

    compileSdkVersion 28
    defaultConfig {
        applicationId "eu.lenni.kran"
        minSdkVersion 28
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.journeyapps:zxing-android-embedded:3.4.0'
}

Thats the errors:

org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:129)
    ... 6 more
Caused by: com.android.ide.common.workers.WorkerExecutorException: 1 exception was raised by workers:
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:23.1.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle found in

and so on...

2 Answers

try to add this library in build.gradle file

implementation 'me.dm7.barcodescanner:zxing:1.9'

implementation 'com.google.android.gms:play-services-vision:17.0.2'

And also add this line in project level gradle file :

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.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"
        }
    }
}

Hope it helps :)

Related