Android Studio 3.4.2
我有 java 文件(我暂时无法将其转换为Kotlin )。
在build.gradle中:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 29
defaultConfig {
minSdkVersion 23
targetSdkVersion 29
versionCode 1296
versionName "2.3.1296"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
def RETROFIT_VERSION = '2.6.0'
def KOTLIN_COROUTINE_VERSION = '1.2.1'
ext.KOTLIN_VERSION = '1.3.41'
dependencies {
api "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
api "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$KOTLIN_COROUTINE_VERSION"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLIN_COROUTINE_VERSION"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.2.2'
在我的活动中- java 文件(我暂时无法转换为Kotlin ):
import android.app.Activity;
import com.myproject.services.transport.TransportService;
import java.util.concurrent.CompletableFuture;
public class LoginActivity extends Activity {
CompletableFuture<Response<?>> response = TransportService.doLogin("email", "pass", false);
}
这是Kotlin文件片段:
import android.util.Log
import com.google.gson.JsonObject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.future
import kotlinx.coroutines.withContext
import retrofit2.Response
import java.util.concurrent.CompletableFuture
class TransportService {
companion object {
@JvmStatic
fun doLogin(
email: String,
password: String,
isCustomtHandle: Boolean = false
): CompletableFuture<Response<*>> = GlobalScope.future { // error here
if (BuildConfig.DEBUG)
Log.d(TAG, "doLogin_login = $isCustomtHandle")
login(email, password, isCustomtHandle)
}
suspend fun login(email: String, password: String, isCustomtHandle: Boolean = false): Response<*> {... }
但是在此行中出现运行时错误:
CompletableFuture<Response<*>> = GlobalScope.future {
在Kotlin文件中出现错误消息:
E/AndroidRuntime(22132): FATAL EXCEPTION: pool-3-thread-2
E/AndroidRuntime(22132): Process: com.myptoject.debug, PID: 22132
E/AndroidRuntime(22132): java.lang.NoClassDefFoundError: Failed resolution of: Ljava/util/concurrent/CompletableFuture;
E/AndroidRuntime(22132): at kotlinx.coroutines.future.FutureKt.future(Future.kt:41)
E/AndroidRuntime(22132): at kotlinx.coroutines.future.FutureKt.future$default(Future.kt:36)
E/AndroidRuntime(22132): at com.myproject.services.transport.TransportService$Companion.doLogin(TransportService.kt:38)
E/AndroidRuntime(22132): at com.myproject.services.transport.TransportService.doLogin(TransportService.kt)
E/AndroidRuntime(22132): at com.myptoject.LoginActivity.login(LoginActivity.java:257)
E/AndroidRuntime(22132): at com.myptoject.LoginActivity_.access$001(LoginActivity_.java:22)
我的代码有什么问题?
P.S。
minSdkVersion 23
这是客户要求。所以我不能增加它
答案 0 :(得分:1)
java.util.concurrent.CompletableFuture
仅在Java 8和更高版本中可用。
我会尝试使用“ org.jetbrains.kotlin:kotlin-stdlib- jdk8 :$ KOTLIN_VERSION”代替build.gradle文件中的jdk7实现。
您也可以尝试在kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8 }
块中添加compileOptions
。
答案 1 :(得分:0)
如CompletableFuture
reference中所述,它仅在API级别24之后可用,因此如果您的要求要求sdk 23,则不能使用该类。
不过,您可以使用两种大致等效的类:
Guava \ androidx.concurrent的ListenableFuture
:https://developer.android.com/jetpack/androidx/releases/concurrent + https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-guava/
Deferred
/ CompletableDeferred
:https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-completable-deferred/index.html