Flutter项目编译报错:Unknown constant tag 41 in javax/management/MBeanServer
编译错误:Unknown constant tag 41 in class file javax/management/MBeanServer
原本运行正常的Flutter表单验证应用(包含邮箱、密码、姓名验证功能),未修改任何代码的情况下突然出现编译错误,且所有Flutter项目均触发相同问题。已尝试执行flutter clean、flutter upgrade,问题未解决。
报错信息
FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\-\StudioProjects\-\android\app\build.gradle' line: 24 * What went wrong: A problem occurred evaluating project ':app'. > Unknown constant tag 41 in class file javax/management/MBeanServer * 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 856ms Exception: Gradle task assembleDebug failed with exit code 1
报错行代码(build.gradle第24行)
apply plugin: 'com.android.application'
Flutter Doctor 结果
D:\src\flutter\flutter\bin\flutter.bat doctor --verbose [√] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.22000.795], locale en-US) • Flutter version 3.0.5 at D:\src\flutter\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision f1875d570e (3 weeks ago), 2022-07-13 11:24:16 -0700 • Engine revision e85ea0e79c • Dart version 2.17.6 • DevTools version 2.12.2 [√] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1) • Android SDK at C:/Users/Sully/AppData/Local/Android/Sdk • Platform android-33, build-tools 32.0.0-rc1 • ANDROID_HOME = C:/Users/Sully/AppData/Local/Android/Sdk • Java binary at: D:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) • All Android licenses accepted. [X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe) ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable. [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.0.5) • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community • Visual Studio Community 2022 version 17.0.32112.339 • Windows 10 SDK version 10.0.19041.0 [√] Android Studio (version 2021.1) • Android Studio at D:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) [√] VS Code (version 1.66.2) • VS Code at C:\Users\Sully\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.38.1 [√] Connected device (3 available) • SM N986B (mobile) • R5CT11TPK3M • android-arm64 • Android 12 (API 31) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22000.795] • Edge (web) • edge • web-javascript • Microsoft Edge 103.0.1264.77 [√] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. Process finished with exit code 0
完整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 plugin: 'com.google.gms.google-services' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { ndkVersion flutter.ndkVersion compileSdkVersion 32 compileSdkVersion 31 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.emart" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration. minSdkVersion flutter.minSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName minSdkVersion 31 targetSdkVersion flutter.targetSdkVersion 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" implementation 'com.android.support:multidex:1.0.3' } configurations.all { resolutionStrategy.force 'com.android.support:support-v4:24.0.0' }
解决方案
调整JDK版本:
Unknown constant tag 41通常是JDK版本不兼容导致,当前使用的JDK 11可能与Gradle存在冲突。切换到JDK 8:- 打开Android Studio的
File > Project Structure > SDK Location - 将JDK路径改为本地JDK 8的安装目录
- 执行
flutter clean后重新构建项目
- 打开Android Studio的
修正重复配置:app/build.gradle中存在重复的
compileSdkVersion和minSdkVersion,删除重复行:// 保留一行compileSdkVersion,建议与targetSdkVersion匹配 compileSdkVersion 31 // 保留一行minSdkVersion minSdkVersion 31升级Gradle插件:在项目根目录的build.gradle中,将Gradle插件版本更新为与Android Studio 2021.1兼容的版本(如
classpath 'com.android.tools.build:gradle:7.0.4')清除Gradle缓存:删除用户目录下的
.gradle文件夹(如C:\Users\Sully\.gradle),重新执行构建
内容的提问来源于stack exchange,提问作者Sally
相关产品推荐
相关产品推荐

