我创建了一个新的Android项目,然后向其中添加了一个新的模块。我的想法是将所有网络调用和序列化分离到单独的模块中。
通过 右键单击项目 > 新建 > 模块 > Java或Kotlin库
创建了该模块。这样添加了一个新的模块,但是在所有作用于字符串的扩展函数上都显示错误:
无法访问 'java.io.Serializable',它是 'kotlin.String' 的超类型。请检查您的模块类路径以查找缺失或冲突的依赖项
我的新模块 build.gradle 文件如下:
plugins {
id 'java-library'
id 'kotlin'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.31'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.2")
testImplementation 'junit:junit:4.13.2'
}
和一个有错误的类:
package com.example.lib
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
class MyClass {
private val client = OkHttpClient()
fun makeRequest() {
val randomVariable = "lowercase letters".uppercase()
val request = Request.Builder()
.get()
.url("https://stackoverflow.com/")
.build()
val response = client.newCall(request).execute().body?.string()!!
}
}
我尝试过清除缓存、清理.gradle缓存、重启,使用Java 8和Java 11作为Gradle JDK,但都没有起作用...