Android Studio编译报错:RoomProcessor与Java Runtime版本不兼容
错误信息
Cause: androidx/room/RoomProcessor has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
背景
原项目在SDK26设备上运行正常,现在需要重构适配SDK33,已尝试升级Gradle到7.2,但问题仍未解决。
当前项目配置
Top-level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { // Android Gradle Project Version agp_version = '7.1.3' ///agp_version = '7.2' } repositories { mavenCentral() google() } dependencies { ///classpath 'com.android.tools.build:gradle:7.1.3' classpath "com.android.tools.build:gradle:$agp_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenCentral() google() ///jcenter() maven{url 'https://maven.google.com'} } } task clean(type: Delete) { delete rootProject.buildDir }
app模块build.gradle
apply plugin: 'com.android.application' android { ///compileSdkVersion 32 compileSdkVersion 34 defaultConfig { applicationId "com.tmschaaf.earthquakev" // was: com.cis2237.schaafp7 minSdkVersion 26 targetSdkVersion 34 versionCode 2 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } dataBinding.enabled = true compileOptions { ///sourceCompatibility = '1.8' ///targetCompatibility = '1.8' sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } /// kotlinOptions { /// jvmTarget = '11' /// } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.6.1' ///implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test:runner:1.5.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'androidx.recyclerview:recyclerview:1.3.2' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'com.google.android.material:material:1.10.0' implementation 'com.google.android.material:material:1.11.0-beta01' implementation 'androidx.coordinatorlayout:coordinatorlayout:1.3.0-alpha02' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.preference:preference:1.2.1' implementation 'androidx.room:room-runtime:2.6.0' annotationProcessor 'androidx.room:room-compiler:2.6.0' testImplementation 'androidx.room:room-testing:2.6.0' implementation 'com.google.android.gms:play-services-maps:18.2.0' ///implementation 'android:exported:false' implementation 'androidx.activity:activity-ktx:1.8.0' } // added 9/10/2023 allprojects { repositories { maven { url "https://maven.google.com" } google() /// jcenter() /// mavenCentral() } }
解决方案
1. 升级项目Java编译版本到17
Room 2.6.0的编译器(RoomProcessor)基于Java 17编译(class file version 61对应Java 17),当前项目使用Java 11(version 55)导致版本不兼容。修改app模块的compileOptions:
compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }
同时在Android Studio中配置JDK:File → Project Structure → SDK Location,将JDK location设置为Java 17(推荐使用Android Studio自带的Embedded JDK 17,无需额外安装)。
2. 升级Android Gradle Plugin(AGP)版本
AGP 7.1.3对Java 17的支持不完善,建议升级到7.4.0及以上版本(AGP 7.4+正式支持Java 17作为编译版本)。修改顶级build.gradle中的agp_version:
agp_version = '7.4.2'
3. 清理缓存并重构项目
执行命令清理构建缓存:
./gradlew clean
然后在Android Studio中点击Build → Rebuild Project,彻底重新构建项目。
4. 修复依赖冲突
app模块中重复引入了Material组件依赖,保留一个版本即可(建议使用稳定版),删除其中一行:
// 删除下面其中一行,推荐保留稳定版1.10.0 implementation 'com.google.android.material:material:1.10.0' // implementation 'com.google.android.material:material:1.11.0-beta01'
内容的提问来源于stack exchange,提问作者tmschaaf

