[英]how do I set java version correctly in Gradle for Kotlin projects? (I can't access functions due to `No virtual method` error being received)
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun startStreaming(setImage: (ByteArray) -> Unit) {
scope.launch {
while (true) {
try {
// screenStream is an object of DataInputStream class
val jpegImageByteArray = screenStream!!.readAllBytes() //---error-here
withContext(Dispatchers.Main) { setImage(jpegImageByteArray) }
} catch (e: Exception) {
e.printStackTrace()
}
Thread.sleep(1000)
}
}
}
FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.g_stream, PID: 19524
java.lang.NoSuchMethodError: No virtual method readAllBytes()[B in class Ljava/io/DataInputStream; or its super classes (declaration of 'java.io.DataInputStream' appears in /system/framework/core-oj.jar)
at com.example.g_stream.viewmodel.StreamViewModel$startStreaming$1.invokeSuspend(StreamViewModel.kt:119)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@a113203, Dispatchers.IO]
After some searches I concluded that the reason is because the code is being compiled to a lower java version.经过一些搜索,我得出结论,原因是因为代码被编译到较低的 java 版本。 So, I tried fixing that but, it didn't change anything.所以,我尝试修复它,但它没有改变任何东西。 here's the gradle script -这是 gradle 脚本 -
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.g_stream"
minSdk 26
targetSdk 33
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_12 //---see-here
targetCompatibility JavaVersion.VERSION_12 //---see-here
}
kotlinOptions {
jvmTarget = '11' //---see-here
}
buildFeatures {
viewBinding true
dataBinding true
}
namespace 'com.example.g_stream'
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1' // do not update, keep 1.6.1
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//------------------------------------------------------------------------------------navigation
def nav_version = "2.5.3"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//--------------------------------------------------------------------------------------espresso
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
//---------------------------------------------------------------------------------------barcode
implementation 'com.google.mlkit:barcode-scanning:17.0.3'
//---------------------------------------------------------------------------------------CameraX
def camerax_version = "1.3.0-alpha02"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
implementation "androidx.camera:camera-view:$camerax_version"
//------------------------------------------------------------------------------------------Gson
implementation 'com.google.code.gson:gson:2.10'
//--------------------------------------------------------------------------------------joystick
implementation 'io.github.controlwear:virtualjoystick:1.10.1'
}
Another post said this might be because this is a purely Kotlin project.另一个帖子说这可能是因为这是一个纯粹的 Kotlin 项目。 So I tried setting the jvmTarget
to 11. even that didn't help.所以我尝试将jvmTarget
设置为 11。即使这样也无济于事。
What am I missing here?我在这里错过了什么?
OP here. OP在这里。 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
is probably the culprit here. @RequiresApi(Build.VERSION_CODES.TIRAMISU)
可能是这里的罪魁祸首。 The android device I was using didn't meet the spec.我使用的 android 设备不符合规范。 the readAllBytes
function requires API 33. So naturally, since my device wasn't API level 33, the function wasn't found and hence, the error. readAllBytes
function 需要 API 33。自然地,由于我的设备不是 API 级别 33,因此找不到 function,因此出现错误。 And yes, my assumptions of this issue being a gradle problem was wrong.是的,我认为这个问题是 gradle 问题的假设是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.