[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
2
3
4
5
6
7
8
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':camera_android:generateReleaseLintModel'.
> Could not resolve all files for configuration ':camera_android:releaseUnitTestRuntimeClasspath'.
> Failed to transform bcprov-jdk18on-1.78.1.jar (org.bouncycastle:bcprov-jdk18on:1.78.1) to match attributes
> Execution failed for JetifyTransform: bcprov-jdk18on-1.78.1.jar.
> Failed to transform using Jetifier. Reason: IllegalArgumentException, message: Unsupported class file major version 65.

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
2
- java adoptopenjdk-21.0.3+9.0.LTS
+ java adoptopenjdk-17.0.13+11

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
2
- android.enableJetifier=true
+ android.enableJetifier=false

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
2
3
4
5
6
7
8
9
10
packagingOptions {
pickFirst '**/META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
pickFirst '**/META-INF/androidx.core_core.version'
pickFirst '**/META-INF/androidx.appcompat_appcompat.version'
// ... additional META-INF file handling
}

configurations.all {
exclude group: 'com.android.support'
}

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
2
3
<application
android:appComponentFactory="androidx.core.app.CoreComponentFactory"
tools:replace="android:appComponentFactory">

Rationale: Ensures proper AndroidX component initialization and resolves potential conflicts with legacy component factories.

Key Learnings

  1. Java Version Alignment: Always ensure Java version compatibility across the entire build chain (Flutter, Gradle, Android SDK)

  2. Jetifier Assessment: Evaluate whether Jetifier is necessary for your project. Modern Flutter projects typically use AndroidX exclusively

  3. Proactive Dependency Management: Use explicit packaging rules to prevent META-INF conflicts in Android builds

  4. Version Monitoring: Regularly check for Java/Gradle compatibility matrices when updating dependencies

Verification

After applying these changes, the build completed successfully:

1
2
flutter build apk --release src/main.dart
# ✅ Build successful

This solution provides a robust foundation for future builds while maintaining compatibility with modern Android development practices.