Flutter Native Timezone库Android构建失败求助
问题现象
使用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.50Try:
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模块的本地配置
- 定位到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
- macOS/Linux:
- 打开该目录下的
build.gradle文件,将kotlin_version修改为你项目使用的版本:buildscript { ext.kotlin_version = '1.8.10' // 替换为你的项目Kotlin版本 // 保留原有其他配置内容 } - 返回项目执行
flutter clean,然后重新构建。
方法3:替换为无原生依赖的替代库
如果上述方法无效,可改用package:timezone_localization,它无需额外原生依赖,直接通过Flutter API获取时区:
import 'package:timezone_localization/timezone_localization.dart'; // 获取本地时区 String localTimezone = await TimezoneLocalization.getCurrentTimezone();
内容的提问来源于stack exchange,提问作者MeLean

