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

引入Hive/Hive_Flutter后Flutter项目Gradle构建报错求助

问题:Flutter引入Hive后Android构建失败(parseDebugLocalResources错误)

当在Flutter项目中引入hive、hive_flutter包或执行pub get后,项目出现如下构建错误:

Execution failed for task ':path_provider_android:parseDebugLocalResources'.
> Could not resolve all files for configuration ':path_provider_android:androidApis'.
   > Failed to transform android.jar to match attributes {artifactType=android-platform-attr, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for PlatformAttrTransform: C:\Users\shahahmadian\AppData\Local\Android\Sdk\platforms\android-31\android.jar.
         > C:\Users\shahahmadian\AppData\Local\Android\Sdk\platforms\android-31\android.jar

详细构建日志(来自flutter run -v)

Task :path_provider_android:compileDebugLibraryResources UP-TO-DATE
[        ] Caching disabled for task ':path_provider_android:compileDebugLibraryResources' because:
[        ]   Build cache is disabled
[   +1 ms] Skipping task ':path_provider_android:compileDebugLibraryResources' as it is up-to-date.
[        ] :path_provider_android:compileDebugLibraryResources (Thread[included builds,5,main]) completed. Took 0.001 secs.
[        ] :path_provider_android:parseDebugLocalResources (Thread[included builds,5,main]) started.
[   +1 ms] > Task :path_provider_android:parseDebugLocalResources FAILED
[        ] Transforming android.jar with PlatformAttrTransform
[   +1 ms] Caching disabled for PlatformAttrTransform: C:\Users\shahahmadian\AppData\Local\Android\Sdk\platforms\android-31\android.jar because:
[   +2 ms]   Build cache is disabled
[   +1 ms] PlatformAttrTransform: C:\Users\shahahmadian\AppData\Local\Android\Sdk\platforms\android-31\android.jar is not up-to-date because:
[  +49 ms]   Task has failed previously.

额外提示信息

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
[        ] You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
[        ] See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings
[        ] 24 actionable tasks: 5 executed, 19 up-to-date

Flutter环境检测结果

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.10, on Microsoft Windows [Version 10.0.19045.2311], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.4.1)
[√] Android Studio (version 2021.3)
[√] VS Code (version 1.74.2)
[√] Connected device (4 available)
[√] HTTP Host Availability

• No issues found!

已尝试调整minSdkVersion、targetSdkVersion和compileSdkVersion,推测问题源于Gradle版本,希望了解问题根源及可行修复方案。


问题根源

这个错误的核心原因是Android SDK Platform 31的android.jar文件损坏或不完整,导致Gradle的资源解析任务无法正常处理该文件。此外,当前使用的Gradle版本(7.4)与Android SDK 31的兼容性冲突,也会触发这类资源转换失败的问题。

修复方案

方案1:修复Android SDK Platform 31

  • 打开Android Studio的SDK Manager,找到Android 12 (API 31)
  • 先卸载该SDK Platform,再重新下载安装
  • 清理项目缓存并重新构建:
    flutter clean
    rm -rf android/.gradle
    flutter pub get
    flutter run
    

方案2:调整Gradle版本以适配环境

针对Flutter 3.3.10稳定版,推荐使用兼容的Gradle组合:

  1. 修改android/build.gradle中的Gradle插件版本:
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
    }
    
  2. 修改android/gradle/wrapper/gradle-wrapper.properties中的Gradle分发版本:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
    
  3. 同步Gradle配置后,执行flutter clean && flutter pub get再运行项目

方案3:切换到已兼容的SDK版本

既然你的Android SDK已安装33.0.0,直接切换到该版本避免API 31的问题:

  • 修改android/app/build.gradle中的配置:
    android {
        compileSdkVersion 33
        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 33
        }
    }
    
  • 确保SDK Manager中已安装Android 13 (API 33)的SDK Platform,然后清理缓存重新构建

内容的提问来源于stack exchange,提问作者NIMA Shahahmadian

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:40:35