Keywords: Flutter | Gradle | Java Compatibility
Abstract: This article provides an in-depth analysis of the 'BUG! exception in phase 'semantic analysis'' error encountered when running Flutter projects on Apple Silicon Macs. The error stems from incompatibility between Java versions and Gradle, particularly Java 17+ with Gradle 6.7. It offers a comprehensive solution including verifying compatibility via official docs, installing a suitable Java version, and configuring Gradle JDK in Android Studio, supported by practical examples and root cause explanations.
Problem Background and Error Analysis
When running Flutter projects on Apple Silicon (ARM) Macs with Android Studio Canary 2020.3.1.22, developers often face build failures with the error: Could not open settings generic class cache for settings file '/path/to/android/settings.gradle'. > BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61. This indicates that Gradle cannot process class files during the semantic analysis phase, primarily due to the unsupported major version 61 (corresponding to Java 17).
Root Cause Investigation
According to the official Gradle compatibility documentation, Java 17 and later versions are not yet supported by Gradle 6.7. The class file major version 61 was introduced in Java 17, while Gradle 6.7 only supports up to Java 16 (major version 60). When the project is configured with JDK 17, Gradle fails to parse the new class file format, leading to exceptions in the semantic analysis phase. This is corroborated by cases in the reference article, where users encountered the same error with Flutter 2.10.3 and JDK 17, highlighting the conflict between Java version upgrades and Gradle support lag.
Solution Implementation
First, verify the compatibility between Gradle and Java. Visit the Gradle official compatibility page to confirm that Gradle 6.7 supports up to Java 16. Thus, the solution involves downgrading the JDK to a compatible version, such as Java 11.
Install Java 11: Download the ARM 64-bit version of JDK 11 from a reliable source like Azul. After installation, ensure the system environment variables point to the new JDK.
Configure Gradle JDK in Android Studio: In the Flutter project, open the Android module separately (File -> Open -> Select the android folder in your project -> Open in a new window). Then, navigate to Preferences -> Build -> Build Tools -> Gradle -> Gradle JDK and change the JDK version to the installed Java 11. This step ensures that Gradle uses a compatible Java runtime, avoiding version conflicts.
Additional Recommendations and Best Practices
Referencing other answers, upgrading the Gradle version may also resolve this issue. For example, modify the distributionUrl in the gradle-wrapper.properties file to a Gradle version that supports Java 17 (e.g., Gradle 7.0+). However, note that upgrading Gradle might introduce other dependency issues, so downgrading the JDK is a more stable quick fix.
To prevent similar issues, it is advisable to check Java and Gradle version compatibility during project initialization. Use tools like ./gradlew --version to verify environment configurations. Additionally, regularly update Flutter and Gradle to stable versions to minimize incompatibility risks.
Conclusion
By downgrading the JDK to Java 11 and properly configuring Android Studio, the 'Unsupported class file major version 61' error can be effectively resolved. This solution is based on official documentation and community practices, ensuring smooth operation of Flutter projects on Apple Silicon Macs. Developers should prioritize version compatibility to enhance development efficiency.