如何解决DefaultRealmModuleMediator.java的unchecked/unsafe操作警告?
解决Realm生成代码的unchecked/unsafe operations警告
运行Android Studio Giraffe 2022.3.1 Patch 3时,Gradle输出以下警告:
Note: C:\AndroidStudioProjects\simsim\app\build\generated\source\kapt\debug\io\realm\DefaultRealmModuleMediator.java uses unchecked or unsafe operations.
项目级build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath "io.realm:realm-gradle-plugin:10.8.0" classpath 'com.google.gms:google-services:4.4.0' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }
App级build.gradle配置
plugins { id 'com.android.application' id 'com.google.gms.google-services' id 'com.google.firebase.crashlytics' id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.kapt' } apply plugin: 'realm-android' apply plugin: 'kotlin-android' android { compileSdkVersion 34 buildToolsVersion "32.0.0" defaultConfig { applicationId "com.sampietro.simsim" minSdkVersion 29 targetSdkVersion 34 versionCode 17 versionName "4.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.10.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.android.volley:volley:1.2.1' implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0' implementation 'com.squareup.picasso:picasso:2.8' def lifecycle_version = "2.5.1" annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'androidx.viewpager2:viewpager2:1.0.0' implementation platform('com.google.firebase:firebase-bom:32.5.0') implementation 'com.google.firebase:firebase-crashlytics' implementation 'com.google.firebase:firebase-analytics' implementation "androidx.core:core-ktx:1.9.0" } allprojects { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" } }
解决方法
这个警告来自Realm自动生成的DefaultRealmModuleMediator.java文件,属于Realm库内部的代码问题,并非业务代码错误。可以通过以下方式处理:
1. 升级Realm版本
当前使用的Realm 10.8.0版本较旧,后续稳定版本已修复这类编译警告。建议升级到最新版:
- 修改项目级build.gradle中的Realm插件版本,例如更新为
10.18.0(或最新稳定版):classpath "io.realm:realm-gradle-plugin:10.18.0" - 同步项目后重新编译,警告通常会消失。
2. 针对Realm生成代码屏蔽警告
如果暂时无法升级,可在app级build.gradle中为kapt任务添加参数,忽略该文件的unchecked警告:
在android块内添加kapt配置:
android { // 保留原有配置... kapt { javacOptions { option("-Xlint:-unchecked") } } }
或者修改全局JavaCompile任务,排除Realm生成目录的警告:
allprojects { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" options.exclude 'io/realm/**' } }
3. 全局关闭unchecked警告(应急用)
如果上述方法无效,可直接关闭unchecked警告输出,但这会屏蔽所有相关警告,可能错过自身代码的问题:
修改app级build.gradle中的JavaCompile配置:
allprojects { tasks.withType(JavaCompile) { options.compilerArgs.remove("-Xlint:unchecked") } }
内容的提问来源于stack exchange,提问作者Renzo Sampietro
相关产品推荐
相关产品推荐

