1. First step you need to ensure that the "Gradle Wrapper" in the path:
android/gradle/wrapper/gradle-wrapper.properties.
Support "Java 21".
Depending on the "Gradle" doc, only Gradle 8.4 officially supports Java 21.
2. In the file "android/build.gradle" or "android/settings.gradle" Ensure that you are using AGP 8.3.2 or higher. Don't use AGP < 8.2 if you are using JAVA 21
3. Run the command "flutter doctor -v", and then check which path flutter is using for java.
4. Clear your cache with the command "flutter clean", and then "flutter pub get".
5. Use the command "flutter build apk --verbose" to get the full details of the build stracture in the console to see the log