问题:
e: /Users/apple-wangguiye/AndroidStudioProjects/MyKotlin1/app/src/main/java/com/example/mykotlin1/viewmodel/FavDishViewModel.kt: (27, 7): Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option
如下代码报错:
class FavDishViewModelFactory(private val repository: FavDishRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(FavDishRepository::class.java)) {
@Suppress("UNCHECKED_CAST")
return FavDishViewModel(repository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
原因:需要直接或者间接地依赖于版本 2.5.0-alpha01
在某些情况下,希望使用all-compatibility
而不是all
,
解决办法:
在build.gradle中添加如下内容
tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
}
或直接在kotlinOptions中添加如下内容
freeCompilerArgs += [
"-Xjvm-default=all",
]
注:
1、参考资料:
https://www.5axxw.com/questions/content/op9gwe
2、build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.mykotlin1"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.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
}
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_mudule'
}
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.karumi:dexter:6.2.3'
implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
def roomVersion = "2.4.2"
// Room components
implementation("androidx.room:room-runtime:$roomVersion")
// kapt "androidx.room:room-compiler:$rootProject.roomVersion"
annotationProcessor("androidx.room:room-compiler:$roomVersion")
// room针对kotlin协程功能的扩展库
implementation "androidx.room:room-ktx:$roomVersion"
// implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"
def lifecycle_version = "2.5.0-beta01"
// Lifecycle components
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
// ViewModel utilities for Compose
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.0"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
// UI
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"
// Testing
testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
}