Flutter安卓构建失败:Kotlin元数据版本不兼容问题求助
问题描述
已尝试修复Kotlin Gradle插件版本不兼容问题,但未成功。项目在iOS模拟器上可正常构建,在安卓模拟器上构建失败,错误信息如下:
The class is loaded from .gradle/caches/transforms-3/c3f0666cf8984b118654915928619821/transformed/jetified-kotlin-stdlib-1.9.0.jar!/kotlin/Unit.class e: .pub-cache/hosted/pub.dev/stripe_android-9.6.0+2/android/src/main/kotlin/com/reactnativestripesdk/utils/Mappers.kt: (926, 7): Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1. FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':stripe_android:compileDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * 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 1m 33s ┌─ Flutter Fix ──────────────────────────────────────────────────────────────────────────────┐ │ [!] Your project requires a newer version of the Kotlin Gradle plugin. │ │ Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then │ │ update /wwwdir/doclink_ios/android/build.gradle: │ │ ext.kotlin_version = '<latest-version>' │ └────────────────────────────────────────────────────────────────────────────────────────────┘ Error: Gradle task assembleDebug failed with exit code 1
项目配置文件内容
android/build.gradle
buildscript { ext.kotlin_version = '1.9.20' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.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 }
pubspec.yaml
name: doclink description: A new Flutter project. # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # In Android, build-name is used as versionName while build-number used as versionCode. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning # In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. version: 1.0.1 environment: sdk: '>=2.18.5 <3.0.0' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions # consider running `flutter pub upgrade --major-versions`. Alternatively, # dependencies can be manually updated by changing the version numbers below to # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependency_overrides: intl: ^0.18.1 package_info_plus: ^4.0.1 http: ^1.0.0 dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.6 firebase_core: ^2.4.1 cloud_firestore: ^4.4.5 firebase_messaging: ^14.5.0 #------------ Service, Permission, Video, Image, State Management ------------# intl: ^0.17.0 get: ^4.6.5 permission_handler: ^10.2.0 agora_rtc_engine: ^6.2.2 shared_preferences: ^2.0.17 image_picker: ^0.8.6+1 video_thumbnail: ^0.5.3 webview_flutter: ^4.4.4 video_player: ^2.6.0 timezone: ^0.9.1 #------------ Google map ------------# google_maps_flutter: ^2.2.3 geolocator: ^9.0.2 #------------ UI ------------# intl_phone_number_input: ^0.7.2 dotted_border: ^2.0.0+3 table_calendar: ^3.0.8 syncfusion_flutter_charts: ^20.4.38 cached_network_image: ^3.2.3 qr_code_scanner: ^1.0.1 url_launcher: ^6.1.10 flutter_rating_bar: ^4.0.1 shimmer: ^2.0.0 photo_view: ^0.14.0 collection: ^1.17.0 #------------ Notification ------------# flutter_local_notifications: ^14.0.0+1 flutter_app_badger: ^1.5.0 qr_flutter: ^4.1.0 open_filex: ^4.3.4 syncfusion_flutter_pdf: ^20.4.54 flutter_stripe: ^9.4.0 razorpay_flutter: ^1.3.5 flutterwave_standard: ^1.0.8 toggle_switch: ^2.1.0 multiselect: ^0.1.0 firebase_analytics: ^10.7.1 signature: ^5.4.1 device_preview: ^1.1.0 flutter_paystack: ^1.0.7 flutter_paypal: ^0.2.0 firebase_auth: ^4.16.0 dev_dependencies: flutter_test: sdk: flutter # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter packages. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true generate: true # To add assets to your application, add an assets section, like this: assets: - assets/ - assets/icon/ - assets/image/ - assets/json/ # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: fonts: - family: black fonts: - asset: assets/fonts/Montserrat-Black.ttf - family: bold fonts: - asset: assets/fonts/Montserrat-Bold.ttf - family: extraBold fonts: - asset: assets/fonts/Montserrat-ExtraBold.ttf - family: extraLight fonts: - asset: assets/fonts/Montserrat-ExtraLight.ttf - family: light fonts: - asset: assets/fonts/Montserrat-Light.ttf - family: medium fonts: - asset: assets/fonts/Montserrat-Medium.ttf - family: regular fonts: - asset: assets/fonts/Montserrat-Regular.ttf - family: semiBold fonts: - asset: assets/fonts/Montserrat-SemiBold.ttf - family: thin fonts: - asset: assets/fonts/Montserrat-Thin.ttf # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages flutter_intl: enabled: true
解决方案建议
- 清理缓存统一依赖:执行以下命令彻底清理Flutter和Gradle缓存,避免残留旧版本文件干扰:
flutter clean rm -rf ~/.gradle/caches cd android && ./gradlew cleanBuildCache - 升级Gradle插件版本:当前Gradle插件7.4.2与Kotlin 1.9.x兼容性不足,建议升级到7.5+版本,修改
android/build.gradle中的配置:classpath 'com.android.tools.build:gradle:7.5.2' // 或更高兼容稳定版 - 强制子模块使用统一Kotlin版本:在根项目
build.gradle中添加配置,确保所有子模块(包括stripe_android)使用指定的Kotlin版本:subprojects { afterEvaluate { project -> if (project.hasProperty('android')) { project.android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' apiVersion = '1.9' languageVersion = '1.9' } } } } } - 升级flutter_stripe依赖:当前使用的
flutter_stripe9.4.0版本可能未适配Kotlin 1.9.x,升级到pub.dev上的最新稳定版:flutter_stripe: ^latest_version # 替换为实际最新版本号 - 检查JDK版本:确保本地JDK版本为11或以上,Kotlin 1.9.x对JDK版本有最低要求,过低版本会导致编译异常。
内容的提问来源于stack exchange,提问作者designtocode
相关产品推荐
相关产品推荐

