javaandroidfluttergradlebuild

When I build an app with flutter it gives the Gradle error


When I try to build an app in Flutter with Android it gives me an error.

I've tried flutter clean, flutter doctor, deleting the build folder but nothing works.

I can build in Windows and Web but Android gives me this error:

FAILURE: Build failed with an exception.
    An exception occurred applying plugin request [id: 'com.android.application']
    Failed to apply plugin 'com.android.internal.application'.
    Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
    Your current JDK is located in C:\Program Files\Android\Android Studio\jre
    You can try some of the following options:
    - changing the IDE settings.
    - changing the JAVA_HOME environment variable.
    - changing `org.gradle.java.home` in `gradle.properties`.

My build.grade code is:

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
   
}

android {
    namespace = "com.example.todo_app"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.todo_app"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}


Solution

  • The error message says it right there:

    Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
    

    So install Java 17.