skip to Main Content

I have been working on a Flutter application that I’ve successfully run on both an Android emulator and a physical device during the past couple of weeks. Somehow and all of a sudden, I’m unable to run it in debug mode.
I tried several answers found in SO but they all seem to be hacks that don’t really fix the issue.

The Error

When I tried to run the application, an error is thrown from Gradle daemon because it could not create a JVM as seen in the screenshot below…

enter image description here

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --add-opens

Possible cause???

I remember I upgraded the Gradle Plugin to I-don’t-know-what-version, I wasn’t really paying attention to it (my bad) but I kept on working without any issues. Then, I upgrade Flutter to version 2.2.2, that’s when the issue started to creeping in (Maybe).

Supporting Info

OS: Windows Server 2016 Standard (x64)
IDE: Android Studio Artic Fox 2020.3.1
Flutter Version: 2.2.2
JAVA_HOME: Pointing to JDK 1.8

Project Structure -> Project Settings -> Project

enter image description here

Java version…

enter image description here

Flutter doctor output…

enter image description here

Gradle files

android/build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.domain.app_name"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    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 '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Question

Is there an obvious fix to the error?
I haven’t worked on Android for about 2 years since I started this project so it might be possible that my problem is quite obvious.
I might be missing something here but if I am, please let me know and I’ll update the answer.

3

Answers


  1. –add-opens option itself is legal, I don’t think it is the root cause, please see https://docs.oracle.com/en/java/javase/16/migrate/migrating-jdk-8-later-jdk-releases.html#GUID-12F945EB-71D6-46AF-8C3D-D354FD0B1781

    I think it might be a java version issue, try to check which SDK’s are available on your machine

    Login or Signup to reply.
  2. 4 Options

    1. Use latest Gradle

    2. Set the JDK version to Embedded JDK

    WHY?

    A copy of the latest OpenJDK comes bundled with Android Studio 2.2+. This is the JDK version GOOGLE recommend.

    To use the bundled JDK, do the following:

    HOW?

    Android Studio > Preferences… > Build, Execution, Deployment > Build Tools > Gradle Under Gradle JDK, choose the Embedded JDK option. Click OK.

    For windows

    select File > Settings… > Build, Execution, Deployment > Build Tools > Gradle Under Gradle JDK, choose the Embedded JDK option.

    1. –add-opens
      If you have to allow code on the class path to do deep reflection to access nonpublic members, then use the –add-opens runtime option.

    Some libraries do deep reflection, meaning setAccessible(true), so they can access all members, including private ones. You can grant this access using the –add-opens option on the java command line. No warning messages are generated as a result of using this option.

    If –illegal-access=deny, and you see IllegalAccessException or InaccessibleObjectException messages at runtime, you could use the –add-opens runtime option, basing the arguments upon the information shown in the exception message.

    1. config jvm option
      Since version 9, the Java compiler can be configured to produce bytecode for an older Java version while making sure the code does not use any APIs from a more recent version. Gradle now supports this release flag on CompileOptions directly for Java compilation. This option takes precedence over the properties described below.

    Due to a bug in Java 9 that was fixed in Java 10, Gradle cannot leverage the release flag when compiling with Java 9.

    Example 6. Setting Java release flag
    GroovyKotlin
    build.gradle
    compileJava {
    options.release = 7
    }
    Historical options for the Java compiler remain available:

    for more info
    https://docs.gradle.org/current/userguide/gradle_daemon.html

    Login or Signup to reply.
  3. Please check the gradel/properties file

     org.gradle.jvmargs=-Xmx1536M --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED 
     android.useAndroidX=true
     android.enableJetifier=true
    

    Change this to :-

    enter image description here

    remove the line after your jvmargs

    Let me know if this works out. Cheers!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search