How to fix the “Failed to scan code” error with ML Kit barcode scanning

Ivan July 29, 2024 2 mins read
app store

Many developers have encountered the error com.google.mlkit.common.MlKitException: Failed to scan code when using Google’s ML Kit barcode scanning library. This issue stems from problems with the barcode scanning module not being correctly installed or loaded. Here’s how you can resolve it:

Use the ModuleInstallClient API

The most reliable fix is to explicitly request the installation of the barcode scanning module before using it. Google provides documentation on how to send an urgent module install request:

val moduleInstall = ModuleInstall.getClient(context)
val moduleInstallRequest = ModuleInstallRequest.newBuilder()
    .addApi(GmsBarcodeScanning.getClient(context))
    .build()

moduleInstall.installModules(moduleInstallRequest)
    .addOnSuccessListener { response ->
        if (response.areModulesAlreadyInstalled()) {
            // Module already installed, proceed with scanning
            startScanning()
        } else {
            // Module was just installed, wait briefly then scan
            Handler(Looper.getMainLooper()).postDelayed({
                startScanning()  
            }, 1000)
        }
    }
    .addOnFailureListener { e ->
        // Handle installation failure
    }

Check module availability first

As an extra precaution, you can check if the module is available before attempting to use it:

moduleInstall.areModulesAvailable(GmsBarcodeScanning.getClient(context))
    .addOnSuccessListener { response ->
        if (response.areModulesAvailable()) {
            startScanning()
        } else {
            // Request installation
        }
    }

Clear Google Play Services Data

If you’re still having issues, try clearing the data for Google Play Services on the device:

Settings > Apps > Google Play Services > Storage > Clear Data

Note that this will remove some user data, such as saved payment methods.

Update Google Play Services

Ensure you have the latest version of Google Play Services installed on the device.

Alternative solution

While the steps above should resolve the “com.google.mlkit.common.MlKitException: Failed to scan code” error for most users, some developers have reported ongoing reliability issues with the ML Kit barcode scanning library. If you continue to experience problems, consider alternative barcode scanning libraries like ZXing or commercial solutions like the Scanbot Barcode Scanner SDK.

Scanbot SDK offers several benefits for barcode scanning:

  1. Performance: Fast and accurate barcode scanning, trained using machine learning techniques.
  2. Developer support: Expert integration assistance during implementation and chat support for troubleshooting.
  3. Ready-to-Use UI: Pre-built scanning interface to speed up implementation.
  4. Offline scanning: 100% offline scanner, reducing dependencies on network connectivity.
  5. Cross-platform support: Feature parity across Android and iOS, supporting all major cross-platform frameworks.

While Scanbot SDK is a commercial product and not free like ML Kit, it may provide value if you need a more robust scanning solution for your app. You can evaluate if the benefits outweigh the cost for your specific use case.