Search code examples
androidfluttergradle

How to get off Java 8 source/target compatibility in a Flutter/Android application


I just created a new flutter application with flutter create tutorial_flutter_redux_navigation --platforms android --empty. Here is some information I think will be helpful to provide context for my question.

flutter doctor -v:

[✓] Flutter (Channel stable, 3.24.4, on macOS 15.0.1 24A348 darwin-arm64, locale en-US)
    • Flutter version 3.24.4 on channel stable at /Users/axiopisty/apps/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 603104015d (9 days ago), 2024-10-24 08:01:25 -0700
    • Engine revision db49896cf2
    • Dart version 3.5.4
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/axiopisty/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/axiopisty/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16B40
    • CocoaPods version 1.15.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.2)
    • Android Studio at /Users/axiopisty/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-79915917-b509.11)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.2.4)
    • IntelliJ at /Users/axiopisty/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 82.0.3
    • Dart plugin version 242.22855.32

[✓] Connected device (4 available)
    • XQ DQ62 (mobile)                • QV7702KBJA            • android-arm64  • Android 14 (API 34)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.0.1 24A348 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.0.1 24A348 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 130.0.6723.92

[✓] Network resources
    • All expected network resources are available.

• No issues found!

gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip

settings.gradle:

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

gradle.properties:

org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.enableJetifier=true

android/build.gradle:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

android/app/build.gradle:

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 {

    def javaVersion = JavaVersion.VERSION_1_8

    namespace = "com.github.axiopisty.tutorial_flutter_redux_navigation"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = javaVersion
        targetCompatibility = javaVersion
    }

    kotlinOptions {
        jvmTarget = javaVersion
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.github.axiopisty.tutorial_flutter_redux_navigation"
        // 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 = "../.."
}

When I build and run the application in the debugger, I see this message:

Running Gradle task 'assembleDebug'...
warning: [options] source value 8 is obsolete and will be removed in a future release
warning: [options] target value 8 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
3 warnings

It is unclear to me what exactly is causing this warning to be displayed, but I think it has to do with the source/target compatibility configured in android/app/build.gradle which has the following definitions:

android {
    ...
    def javaVersion = JavaVersion.VERSION_1_8
    compileOptions {
        sourceCompatibility = javaVersion
        targetCompatibility = javaVersion
    }

    kotlinOptions {
        jvmTarget = javaVersion
    }
    ...
}

Wanting to fix/remove the deprecation warnings appearing in build logs, I tried to upgrade the javaVersion from JavaVersion.VERSION_1_8 to any version higher than 1.8. But doing so causes this build time warning in the logs to be replaced with an error that prevents the application from running. For example, if I update javaVersion to be JavaVersion.VERSION_11 I see this error message:

Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':app:androidJdkImage'.
   > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JdkImageTransform: /Users/axiopisty/Library/Android/sdk/platforms/android-34/core-for-system-modules.jar.
         > Error while executing process /Users/axiopisty/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/jlink with arguments {--module-path /Users/axiopisty/.gradle/caches/transforms-3/d9e926982c2c139ff9125eb7b6be4144/transformed/output/temp/jmod --add-modules java.base --output /Users/axiopisty/.gradle/caches/transforms-3/d9e926982c2c139ff9125eb7b6be4144/transformed/output/jdkImage --disable-plugin system-modules}

Given this information I know I can just live with the deprecation warning in the build logs and leave things alone (for now). At least it builds successfully. But, when I see warnings in the build logs, I like to address them to make sure my application is ready for future updates/upgrades.

My question is, what needs to be done in my project to get off the Java 8 source/target compatibility in order to address the build log warnings? The newest version of Android Studio is using Java 21 to build the project. I'm not looking for a flag to pass as an argument to the build to suppress the warning.


Solution

  • The problem appears to have been related to the fact that I am using Android Studio (Ladybug) that uses a Java 21 JDK.

    I was able to get the warning to go away by updating the javaVersion to JavaVersion.VERSION_17. I'm sure any version higher than JavaVersion.VERSION_1_8 will work to remove the warning from the logs.

    Additionally, I needed to configure flutter to not use the JDK bundled with Android Studio (Ladybug) when running my application.

    I used sdkman to install Java 17 and then configured flutter like so:


    flutter config --jdk-dir "/Users/axiopisty/.sdkman/candidates/java/17.0.13-tem"


    This allowed me to update the source/target compatibility parameters AND not get the error:

    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Could not resolve all files for configuration ':app:androidJdkImage'.
       > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
          > Execution failed for JdkImageTransform: /Users/axiopisty/Library/Android/sdk/platforms/android-34/core-for-system-modules.jar.
             > Error while executing process /Users/axiopisty/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/jlink with arguments {--module-path /Users/axiopisty/.gradle/caches/transforms-3/d9e926982c2c139ff9125eb7b6be4144/transformed/output/temp/jmod --add-modules java.base --output /Users/axiopisty/.gradle/caches/transforms-3/d9e926982c2c139ff9125eb7b6be4144/transformed/output/jdkImage --disable-plugin system-modules}