集成Visa QRParser-2.2.0时minSdkVersion19下Build失败(transformClassesWithDesugar)
transformClassesWithDesugar Error When Using Visa QRParser Library on minSdkVersion < 21 I'm using Visa's QRParser library to parse data from Visa payment QR codes—for scanning, I'm relying on Google Vision, since this library only handles raw QR value parsing (no scanning functionality included).
The problem hits when I set my project's minSdkVersion to anything below 21 (e.g., 19): the Gradle build fails with a transformClassesWithDesugarForMockDebug error, shown below:
16:09:05.083 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
16:09:05.083 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
16:09:05.083 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
16:09:05.083 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':app:transformClassesWithDesugarForMockDebug'.
I've tried running ./gradlew build --stacktrace --debug to dig into the issue, but didn't find a clear root cause. Oddly enough, removing the QRParser dependency fixes the build entirely, and everything works fine with minSdkVersion 19+. Since this is a pure Java parsing library, it shouldn't be tied to higher Android API levels—so this behavior is confusing.
Potential Fixes to Try
1. Ensure Proper Desugar Configuration for Java 8 Compatibility
Even if the library claims to be pure Java, it might be using Java 8 features (like lambdas, streams, or optional classes) that require Desugar support for minSdkVersion < 21. Double-check your Module-level build.gradle has the correct Desugar setup:
android { compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' // Use the latest stable version }
Make sure you're using an up-to-date version of the desugar library to avoid known compatibility bugs.
2. Inspect the Library's Bytecode for Hidden Android API Dependencies
Use a tool like JD-GUI or Android Studio's built-in decompiler to check the QRParser library's class files. Sometimes even "pure Java" libraries can accidentally reference Android API 21+ classes or methods (e.g., certain utility classes that were added in API 21).
If you find such references, you can either reach out to Visa to request a fix, or modify the library code yourself (if permitted) to replace those high-API calls with compatible alternatives.
3. Adjust Desugar/Dex Compiler Flags
Try tweaking Gradle's compiler settings to work around the Desugar failure:
- Add these lines to your
gradle.properties:android.enableD8.desugar=true android.enableR8.fullMode=false - Or disable pre-dexing in your Module-level
build.gradle:android { dexOptions { preDexLibraries = false } }
Note: These are workarounds, so make sure to thoroughly test your app after applying them to avoid new compatibility issues.
4. Downgrade to an Older Version of the QRParser Library
If Visa offers older versions of the QRParser library, try rolling back to an earlier release. It's possible that the 2.2.0 version introduced Java 8 features or other high-API dependencies that weren't present in previous versions.
内容的提问来源于stack exchange,提问作者Gweltaz Niquel

