解决Flutter项目报错:Could not create an instance of LibraryVariantBuilderImpl
解决Flutter项目中
:wakelock模块的Namespace配置错误 问题背景
格式化电脑并重装Android Studio及相关依赖后,原本可正常运行的Flutter项目启动报错,错误指向:wakelock模块未指定namespace,尽管android/app/build.gradle中已正确配置namespace。
报错信息
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':wakelock'. > Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl. > Namespace not specified. Please specify a namespace in the module's build.gradle file like so: android { namespace 'com.example.namespace' } If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.
当前配置文件
android/app/build.gradle
plugins { id "com.android.application" id "kotlin-android" 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.generalomosco.myapp" compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "com.generalomosco.myapp" minSdkVersion 24 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { /*implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"*/ }
settings.gradle
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "8.0.2" apply false id "org.jetbrains.kotlin.android" version "1.7.10" apply false } include ":app"
解决方案
方案1:升级wakelock依赖到适配AGP 8.0+的版本
打开项目根目录的pubspec.yaml,将wakelock的版本更新为最新稳定版,执行flutter pub get拉取新依赖。新版本的wakelock已适配Android Gradle Plugin(AGP)8.0+的namespace要求。
方案2:通过Gradle脚本为:wakelock模块补全namespace
在项目根目录的build.gradle中添加以下代码,自动为未配置namespace的库模块添加默认namespace:
subprojects { afterEvaluate { project -> if (project.plugins.hasPlugin('com.android.library') && project.android.namespace == null) { if (project.name == "wakelock") { project.android { namespace "dev.fluttercommunity.wakelock" } } else { project.android { namespace "com.example.${project.name}" } } } } }
方案3:降级Android Gradle Plugin版本(临时方案)
修改settings.gradle中的AGP版本为7.x系列(如7.4.2),同时调整Kotlin版本以匹配:
plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.4.2" apply false id "org.jetbrains.kotlin.android" version "1.8.20" apply false }
此方案仅作为临时 workaround,长期建议使用方案1或2以适配最新工具链。
内容的提问来源于stack exchange,提问作者General Omosco
相关产品推荐
相关产品推荐

