You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter打包APK报错:Unsupported class file major version 64求助

Flutter打包APK报错:Unsupported class file major version 64

问题描述

完成Flutter应用开发后,执行flutter build apk打包APK时持续报错,已创建并配置key.properties但未定位到问题原因。

报错信息

FAILURE: Build failed with an exception.

* What went wrong:
Could not open settings generic class cache for settings file '/Users/osuyaalex/AndroidStudioProjects/multichoice/android/settings.gradle' (/Users/osuyaalex/.gradle/caches/7.6/scripts/181f8ztwffnirbvka8qh2s66o).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 64

* 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 2s
Running Gradle task 'bundleRelease'...                              3.7s
Gradle task bundleRelease failed with exit code 1

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: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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'
    }

    defaultConfig {
        applicationId "com.osuyaalex.multichoice"
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

解决方案

核心原因

Unsupported class file major version 64对应Java 20,而当前使用的Gradle 7.6最高仅支持Java 18,版本不匹配导致编译报错。

具体修复步骤

  • 方案1:降低Java版本
    安装Java 17或18,配置系统JAVA_HOME环境变量指向该JDK;或在项目的android/gradle.properties中指定JDK路径:

    org.gradle.java.home=/path/to/your/java17_or_18_jdk
    
  • 方案2:升级Gradle版本
    若不想更换Java版本,可升级Gradle到支持Java 20的版本(如Gradle 7.6.2+或8.x):

    1. 打开android/gradle/wrapper/gradle-wrapper.properties
    2. 修改distributionUrl为对应版本,例如:
      distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip
      
    3. 同步项目后重新执行flutter build apk
  • 方案3:对齐编译配置(可选)
    若升级Java版本,可同步调整app/build.gradle中的编译选项,比如将sourceCompatibility和targetCompatibility改为JavaVersion.VERSION_17,kotlinOptions.jvmTarget改为'17',确保配置与使用的Java版本一致。

内容的提问来源于stack exchange,提问作者alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 08:42:33