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

Flutter Native Timezone库Android构建失败求助

解决Flutter中flutter_native_timezone v2.0.0 Android构建Kotlin版本冲突问题

问题现象

使用package:timezone结合package:flutter_native_timezone v2.0.0获取本地时区时,Android构建报错:

FAILURE: Build failed with an exception.

  • What went wrong:
    The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
    The following dependencies do not satisfy the required version:
    project ':flutter_native_timezone' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50

  • 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

BUILD FAILED in 1s
Exception: Gradle task assembleDebug failed with exit code 1

自身项目Kotlin插件版本为1.8.10,降级至1.5.20或使用低版本库均无法解决问题。

解决方案

方法1:强制统一所有模块的Kotlin Gradle插件版本

在项目根目录的build.gradle(非app模块的配置文件)中,添加subprojects配置,强制所有子模块使用指定的Kotlin版本:

buildscript {
    ext.kotlin_version = '1.8.10' // 填写你项目当前使用的Kotlin版本
    // 保留原有其他配置内容
}

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'org.jetbrains.kotlin' && details.requested.name.startsWith("kotlin-")) {
                details.useVersion "${ext.kotlin_version}"
            }
        }
    }
}

方法2:修改flutter_native_timezone模块的本地配置

  1. 定位到flutter_native_timezone的本地缓存目录:
    • macOS/Linux:~/.pub-cache/hosted/pub.dev/flutter_native_timezone-2.0.0/android
    • Windows:C:\Users\<你的用户名>\AppData\Local\Pub\Cache\hosted\pub.dev\flutter_native_timezone-2.0.0\android
  2. 打开该目录下的build.gradle文件,将kotlin_version修改为你项目使用的版本:
    buildscript {
        ext.kotlin_version = '1.8.10' // 替换为你的项目Kotlin版本
        // 保留原有其他配置内容
    }
    
  3. 返回项目执行flutter clean,然后重新构建。

方法3:替换为无原生依赖的替代库

如果上述方法无效,可改用package:timezone_localization,它无需额外原生依赖,直接通过Flutter API获取时区:

import 'package:timezone_localization/timezone_localization.dart';

// 获取本地时区
String localTimezone = await TimezoneLocalization.getCurrentTimezone();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:20:24