Flutter Android构建失败:flutter_native_timezone命名空间缺失+AGP版本警告,尝试修复无效求助
各位大佬好!我最近在Android上运行Flutter应用时突然遭遇构建失败,核心问题和flutter_native_timezone包有关。我已经尝试了升级Kotlin版本、更换时区相关包、调整AGP版本等方法,但都没能解决问题,同时还收到了AGP版本即将被Flutter弃用的警告,实在没辙了来求助!
遇到的完整错误信息
首先是AGP版本的警告提示:
Warning: Flutter support for your project's Android Gradle Plugin version (Android Gradle Plugin version 8.1.0) will soon be dropped. Please upgrade your Android Gradle Plugin version to a version of at least Android Gradle Plugin version 8.3.0 soon. Alternatively, use the flag "--android-skip-build-dependency-validation" to bypass this check.
Potential fix: Your project's AGP version is typically defined in the plugins block of thesettings.gradlefile (/Users/rajpurohittanu/Documents/GitHub/generic-quiz-flutter/android/settings.gradle), by a plugin with the id of com.android.application. If you don't see a plugins block, your project was likely created with an older template version. In this case it is most likely defined in the top-level build.gradle file (/Users/rajpurohittanu/Documents/GitHub/generic-quiz-flutter/android/build.gradle) by the following line in the dependencies block of the buildscript: "classpath 'com.android.tools.build:gradle:'".
紧接着是导致构建失败的核心错误:
FAILURE: Build failed with an exception.
- What went wrong:
A problem occurred configuring project ':flutter_native_timezone'.Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
Namespace not specified. Specify a namespace in the module's build file.
- 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
Running Gradle task 'assembleDebug'... 1,792ms
Error: Gradle task assembleDebug failed with exit code 1
我的当前项目配置
顶层android/build.gradle文件内容
buildscript { ext.kotlin_version = '1.8.22' repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:8.2.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.google.gms:google-services:4.3.15" } } 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" id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" } def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file("key.properties") if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { namespace = "" compileSdk = 35 ndkVersion = flutter.ndkVersion compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "17" } defaultConfig { applicationId = "" minSdk = 26 targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release minifyEnabled false shrinkResources false } } } flutter { source = "../../" }
pubspec.yaml部分内容
name: generic_quiz_flutter description: "A new Flutter project." publish_to: 'none'
我已经尝试过的修复手段
- 将Kotlin版本升级到
1.8.22 - 把AGP版本从8.1.0调整到
8.2.2 - 尝试替换
flutter_native_timezone为其他时区相关Flutter包 - 按照警告提示检查了AGP版本的配置位置,确认配置正确
有没有大佬遇到过类似的问题?或者有其他我没考虑到的修复方向?麻烦指点一下!
内容来源于stack exchange

