[Flutter FAQs] Fixing Failed to transform bcprov-jdk18on-1.78.1.jar
Fixing Failed to transform bcprov-jdk18on-1.78.1.jar
Problem
When building a Flutter APK with the command flutter build apk --release src/main.dart
, the build process failed with the following error:
1 | FAILURE: Build failed with an exception. |
The error indicated that the Java class file major version 65 (which corresponds to Java 21) was incompatible with the current build environment. This incompatibility occurred during the Jetifier transformation process when trying to process the BouncyCastle cryptography library.
Root Cause
The issue stemmed from a Java version compatibility problem:
- Java Class File Major Version 65 corresponds to Java 21
- The build environment was using an older Java version that couldn’t process Java 21 compiled libraries
- Jetifier, which converts Android Support libraries to AndroidX, couldn’t handle the newer bytecode format
Solution
The fix involved a comprehensive approach addressing multiple compatibility layers:
1. Update Java Version
Updated .tool-versions
to use a compatible Java version:
1 | - java adoptopenjdk-21.0.3+9.0.LTS |
Rationale: Java 17 provides better compatibility with the current Flutter/Gradle ecosystem while supporting modern language features.
2. Disable Jetifier
Modified android/gradle.properties
:
1 | - android.enableJetifier=true |
Rationale: Since the project uses AndroidX libraries exclusively, Jetifier transformation is unnecessary and was causing the compatibility issue.
3. Enhanced Packaging Configuration
Added comprehensive packaging options in android/app/build.gradle
:
1 | packagingOptions { |
Rationale: Prevents conflicts when multiple dependencies include the same AndroidX metadata files and explicitly excludes deprecated Android Support libraries.
4. AndroidX Core Component Factory
Added to android/app/src/main/AndroidManifest.xml
:
1 | <application |
Rationale: Ensures proper AndroidX component initialization and resolves potential conflicts with legacy component factories.
Key Learnings
-
Java Version Alignment: Always ensure Java version compatibility across the entire build chain (Flutter, Gradle, Android SDK)
-
Jetifier Assessment: Evaluate whether Jetifier is necessary for your project. Modern Flutter projects typically use AndroidX exclusively
-
Proactive Dependency Management: Use explicit packaging rules to prevent META-INF conflicts in Android builds
-
Version Monitoring: Regularly check for Java/Gradle compatibility matrices when updating dependencies
Verification
After applying these changes, the build completed successfully:
1 | flutter build apk --release src/main.dart |
This solution provides a robust foundation for future builds while maintaining compatibility with modern Android development practices.