0

I am trying to make a flutter application but I constantly get this error :

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     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`

I followed this question : Android Studio Error "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8"

and this was the output :

------------------------------------------------------------
Gradle 7.4
------------------------------------------------------------

Build time:   2022-02-08 09:58:38 UTC
Revision:     f0d9291c04b90b59445041eaa75b2ee744162586

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          13.0.7 (Private Build 13.0.7+5-Ubuntu-0ubuntu120.04)
OS:           Linux 5.4.0-125-generic amd64

JVM points to 13.0.7. What can be issue and how can it be resolved? Thankyou

Arjun Malhotra
  • 351
  • 2
  • 11

3 Answers3

0

In your Android Studio IDE, go to ~

Preferences → Build, Execution, Deployment → Build Tools → Gradle → *Gradle JDK enter image description here and choose the appropriate JDK version and apply. Try to take the embedded JDK as that would be IDE version appropriate. After this sync project and run.

GeekyChick
  • 17
  • 4
0

Check your version of Android Studio and update to Dolphin (2021.3.x).

There are plenty of answers to this question out there that simply ask you to point to the right SDK. None of them appreciate that in some 4.x versions of Android Studio, you can be pointing to the right SDK, but Android Studio keeps pointing to the v1.8, even if you have a version >11 installed and selected in:

  • Project Structure, or
  • Preferences

It's possible that: C:*\java -version --> Results in jre/jvm >11, your 'flutter doctor -v' still says: jre/jvm-1.8.

If you don't see the above screen posted by GeekyChick in settings (i.e., "it's a location that only opens up a checkbox where its written generate *.iml files"), than you probably have Android Studio v4.x, rather than a 2021.x or greater.

Upgrade your Android Studio to Dolphin, and re-follow the instructions to choose the right SDK in Project Structure (not Settings).

Joe W
  • 88
  • 1
  • 6
0

For me this error came up after downloading JDK 11 and upgrading flutter.

First start by following the instructions at https://stackoverflow.com/a/76172567/840958.

If that doesn't work, here's what fixed this error for me:

  1. Download JDK v11 from https://www.openlogic.com/openjdk-downloads and install is. Then make sure you set these configurations:
  • In android/build.gradle under buildscript set
buildscript {
    ext.kotlin_version = '1.7.10'
    ...
}
  • Also in android/build.gradle, update the dependencies to
dependencies {
    classpath 'com.android.tools.build:gradle:7.2.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    ...
}
  • In android/app/build.gradle update the dependencies to
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    ...
}
  • Also in android/app/build.gradle, under android update the compileSdkVersion and targetSdkVersion as such:
android {
    compileSdkVersion flutter.compileSdkVersion
    ...
}
defaultConfig {
    targetSdkVersion flutter.targetSdkVersion
    ...
}
  • Also in android/app/build.gradle, add or update:
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    ...
}

and

kotlinOptions {
    jvmTarget = '1.8'
}
  1. Set the JDK gradle will use in the Flutter app by following the instructions at https://stackoverflow.com/a/76172567/840958.

  2. In build.gradle I had code under 'repositories' that was binding to an earlier version of gradle, which I had to remove:

    allprojects {
        repositories {
          ...
          tasks.withType(JavaCompile).configureEach {
             javaCompiler = javaToolchains.compilerFor {
                languageVersion = JavaLanguageVersion.of(8)
             }
           }
        }
    }
    
  3. Remove all references to repositories that are using older versions of gradle:

jcenter()

and

maven { url 'https://maven.fabric.io/public' }

  1. From gradle.properties remove (if it exists):

android.enableR8=true

  1. From app/build.gradle remove (if it exists):

useProguard true

and

apply plugin: 'io.fabric'

Almog C
  • 795
  • 6
  • 14