已更新至最新版仍提示Kotlin Gradle插件版本过低的Flutter打包问题
Flutter构建APK时Kotlin Gradle插件版本不匹配问题
执行flutter build apk时控制台报错:
Your project requires a newer version of the Kotlin Gradle plugin
提示需更新指定路径下build.gradle中的ext.kotlin_version至最新版本,但已将根项目build.gradle中的ext.kotlin_version设置为1.8.20,且尝试过flutter upgrade、flutter clean、flutter pub upgrade操作,Flutter版本为3.7.11,问题仍未解决。
相关配置文件
根项目build.gradle
buildscript { ext.kotlin_version = '1.8.20' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.signuppage"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:label="AppName" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
/android/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 new GradleException("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' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { 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' } namespace "com.example.signuppage" defaultConfig { applicationId "com.example.signuppage" minSdkVersion 21 //flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.work:work-runtime-ktx:2.7.0' }
系统信息
- 操作系统:Windows 10
- 编辑器:Visual Studio Code
解决办法
匹配Gradle插件与Kotlin版本
当前使用的Gradle插件7.1.2仅兼容最高1.6.21版本的Kotlin插件,与你设置的1.8.20不匹配。修改根项目build.gradle中的Gradle插件版本为7.4.2(兼容Kotlin 1.8.x):classpath 'com.android.tools.build:gradle:7.4.2'更新Gradle wrapper版本
打开/android/gradle/wrapper/gradle-wrapper.properties,将distributionUrl修改为对应Gradle 7.5版本(匹配7.4.2插件):distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip彻底清理后重新构建
执行以下命令:flutter clean rm -rf android/.gradle android/build flutter pub get flutter build apk检查依赖库兼容性
项目依赖的androidx.work:work-runtime-ktx:2.7.0可能依赖旧版Kotlin,可升级至兼容版本:implementation 'androidx.work:work-runtime-ktx:2.8.1'
内容的提问来源于stack exchange,提问作者Henry Davis
相关产品推荐
相关产品推荐

