Search code examples
androidkotlindependenciesqr-codezxing

Conflicting dependencies error when adding zxing-android-embedded & zxing-core in app to create QR image


I'm trying to make an app in Kotlin which scans a QR Code, generates a PDF with the result of the scan at the centre of the page & adds below it a 125x125 px QR Code image of the same text (essentially the qr code which was scanned) & then stores the file in my Downloads folder. I am using PdfDocument class for PDF generation, com.github.yuriy-budiyev:code-scanner:2.3.2 dependency for scanning QR Code, and the Zxing library to generate the QR Code image for the PDF.

The problem I am facing is when I add below dependencies for the zxing library in my build.gradle file,

implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'

I am getting an error when my app builds which essentially says - (full error : https://pastebin.com/m6Vj0bAJ)

Duplicate class com.google.zxing.Result found in modules zxing-core-3.4.1.jar (com.google.zxing:core:3.4.1) and zxing-android-embedded-3.6.0.jar (com.journeyapps:zxing-android-embedded:3.6.0)

Here is what the dependencies in my build.gradle file look like -

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    implementation 'com.github.yuriy-budiyev:code-scanner:2.3.2' //for scanning qr
    implementation 'com.google.zxing:core:3.4.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}

And the imports & code I am using for generating qr code with zxing lib. in my kotlin code -

import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.BarcodeFormat
import com.google.zxing.qrcode.QRCodeWriter
import com.journeyapps.barcodescanner.BarcodeEncoder

//code in the function which creates the PDF using PdfDocument lib.

val qrCodeWriter = QRCodeWriter()
val bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 125, 125)
val barcodeEncoder = BarcodeEncoder()
val qrCodeBitmap = barcodeEncoder.createBitmap(bitMatrix)
val qrCodeX = (pageInfo.pageWidth - qrCodeBitmap.width) / 2f
val qrCodeY = y + textBounds.height() + 20f
canvas.drawBitmap(qrCodeBitmap, qrCodeX, qrCodeY, null)

Everything works fine if I don't do the whole qrcode image in pdf part; When I added the two dependencies I mentioned for the zxing lib., my build failed with the error I mentioned above. What went wrong here?


Solution

  • According to this page, zxing-android-embedded 3.6.0 has a dependency to zxing:core 3.3.2. You've added zxing:core 3.4.1 as a dependency, so you're getting duplicate copies of zxing:core.

    There are a couple of different possible solutions:

    • Use the same version of zxing:core (i.e. 3.3.2) that zxing-android-embedded does.
    • Remove the transitive dependencies of zxing-android-embedded, so only your version (3.4.1) of zxing:core is used:
      implementation ('com.journeyapps:zxing-android-embedded:3.6.0') { transitive false }

      Note: This may or may not work, depending on whether zxing-android-embedded is compatible with any other versions of its dependencies.