如何在 Flutter/Android 应用程序中摆脱 Java 8 源/目标兼容性

问题描述 投票:0回答:1

我刚刚使用

flutter create tutorial_flutter_redux_navigation --platforms android --empty
创建了一个新的 flutter 应用程序。我认为以下信息有助于为我的问题提供背景信息。

扑医生-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

设置.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.属性:

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 = "../.."
}

当我在调试器中构建并运行应用程序时,我看到以下消息:

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

我不清楚到底是什么导致显示此警告,但我认为这与

android/app/build.gradle
中配置的源/目标兼容性有关,它具有以下定义:

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

    kotlinOptions {
        jvmTarget = javaVersion
    }
    ...
}

想要修复/删除构建日志中出现的弃用警告,我尝试将

javaVersion
JavaVersion.VERSION_1_8
升级到任何高于 1.8 的版本。但这样做会导致日志中的构建时警告被替换为阻止应用程序运行的错误。例如,如果我将 javaVersion 更新为
JavaVersion.VERSION_11
,我会看到以下错误消息:

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}

鉴于这些信息,我知道我可以忍受构建日志中的弃用警告,而不要管事情(暂时)。至少它构建成功了。但是,当我在构建日志中看到警告时,我喜欢解决它们,以确保我的应用程序已为将来的更新/升级做好准备。

我的问题是,我的项目需要做什么才能摆脱 Java 8 源/目标兼容性以解决构建日志警告?最新版本的 Android Studio 使用 Java 21 来构建项目。我不是在寻找一个标志作为参数传递给构建来抑制警告。

android flutter gradle
1个回答
0
投票

该问题似乎与我使用的 Android Studio (Ladybug) 使用 Java 21 JDK 有关。

我可以通过将

javaVersion
更新为
JavaVersion.VERSION_17
来让警告消失。我确信任何高于
JavaVersion.VERSION_1_8
的版本都可以从日志中删除警告。

此外,我需要将 flutter 配置为 not 在运行我的应用程序时使用与 Android Studio (Ladybug) 捆绑的 JDK。

我使用 sdkman 安装 Java 17,然后像这样配置 flutter:


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


这允许我更新源/目标兼容性参数并且不会收到错误:

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}
© www.soinside.com 2019 - 2024. All rights reserved.