Android项目build.gradle报错:设置applicationId时机过晚求助
解决Android项目中"It is too late to set applicationId"报错问题
问题详情
配置Android项目时出现报错,抛出com.android.build.gradle.internal.dsl.AgpDslLockedException异常,提示信息:
It is too late to set applicationId
原因是设置applicationId的时机过晚,该参数已被读取用于项目配置。
用户提供的app/build.gradle代码如下:
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw newGradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' // START: FlutterFire Configuration apply plugin: 'com.google.gms.google-services' // END: FlutterFire Configuration apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { namespace "com.example.moto_park" compileSdkVersion 33 ndkVersion flutter.ndkVersion buildFeatures { viewBinding true } defaultConfig { multiDexEnabled true } compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.10.0' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.moto_park" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdkVersion 21 targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2' implementation 'com.google.firebase:firebase-analytics' } }
解决方案
问题核心是错误地将defaultConfig和buildTypes嵌套在了configurations.all块内部。configurations.all是用于配置依赖解析策略的代码块,此时Android Gradle插件已经完成了项目基础配置的初始化,再在这里设置applicationId会触发时机过晚的报错。
修正步骤:
- 将
configurations.all里的defaultConfig块移到android块的直接子层级,和compileSdkVersion、compileOptions等同级,合并原有重复的multiDexEnabled配置。 - 移除
configurations.all内部的buildTypes块,将其移到android块的直接子层级。 - 保留
configurations.all里的resolutionStrategy配置,其他内容调整到正确层级。
修正后的代码如下:
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw newGradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' // START: FlutterFire Configuration apply plugin: 'com.google.gms.google-services' // END: FlutterFire Configuration apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { namespace "com.example.moto_park" compileSdkVersion 33 ndkVersion flutter.ndkVersion buildFeatures { viewBinding true } // 正确层级的defaultConfig,合并所有配置项 defaultConfig { applicationId "com.example.moto_park" minSdkVersion 21 targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.10.0' } } // 正确层级的buildTypes buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2' implementation 'com.google.firebase:firebase-analytics' } }
内容的提问来源于stack exchange,提问作者Devesh Tiwari
相关产品推荐
相关产品推荐

