You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加shared_preferences导致Flutter构建失败求助

问题描述

原本正常运行的Flutter项目,添加shared_preferences插件用于Token存储后,仅添加依赖(未编写任何相关代码)就出现构建失败。

  1. 添加依赖后,在android目录下执行./gradlew clean build,出现Gradle BUILD FAILED错误,控制台输出如下:
FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':shared_preferences_android:compileDebugUnitTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':shared_preferences_android:compileProfileUnitTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
==============================================================================

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.10.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 1m 23s
  1. 执行flutter run -v同样失败,控制台部分输出如下(完整输出见图片):
    输入图片描述
[        ] 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}.
[   +4 ms]       > Execution failed for JdkImageTransform: C:\Users\23259\AppData\Local\Android\sdk\platforms\android-34\core-for-system-modules.jar.[   +1 ms]          > Error while executing process D:\Program Files\Android\Android Studio\jbr\bin\jlink.exe with arguments {--module-path
E:\WorkPlace\environment\gradle_cache_files\caches\8.10.2\transforms\8818889531d4cc222657e8daa8ba8c92-1a737d64-70f4-4939-938b-f7be28cb8845\transformed\output\temp\jmod --add-modules java.base --output
E:\WorkPlace\environment\gradle_cache_files\caches\8.10.2\transforms\8818889531d4cc222657e8daa8ba8c92-1a737d64-70f4-4939-938b-f7be28cb8845\transformed\output\jdkImage --disable-plugin system-modules}
相关配置
  1. 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
}
  1. 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"
}

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

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

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

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

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

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.trip"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        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 = "../.."
}
  1. gradle-wrapper.properties文件内容:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.10.2-all.zip
  1. pubspec.yml文件内容:
name: trip
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=3.4.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter

  dio: ^5.8.0+1
  shared_preferences: ^2.3.0


dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.0

flutter:
  uses-material-design: true

  assets:
    - assets/images/
版本信息
  • Flutter 3.22.0
  • Gradle 8.10.2
  • Java JDK 17
  • shared_preferences 2.3.0

内容的提问来源于stack exchange,提问作者Mingtiao Shiqi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:57:04