简体   繁体   English

如何在 Gradle 中为 Kotlin 项目正确设置 java 版本? (由于收到“No virtual method”错误,我无法访问函数)

[英]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)
            }
        }
    }

Error -错误 -

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.

相关问题 由于 Gradle 版本 I 没有和来源 8,Gradle 构建失败 - Gradle Build Failing Due to Gradle Version I Do Not Have and Source 8 如何在Kotlin中覆盖此方法(在Java中有效,但在Kotlin中无效) - How do I override this method in Kotlin (works in Java but not in Kotlin) 如何将 Java/Kotlin 转换为 CLI? - How can I do Java/Kotlin to CLI? 为什么我不能在Kotlin中访问自动生成的默认方法? - Why can't I access automatically generated default method in Kotlin? 如何在Android studio 1.3中设置gradle版本? - How do I set the gradle version in Android studio 1.3? 如何在 gradle 中为我的 jar 设置版本? - How do I set a version for my jar in gradle? 如何在 Gradle 构建中指定所需的 Java 版本? - How do I specify the required Java version in a Gradle build? 如何强制 Gradle 为两个依赖项设置相同的版本? - How can I force Gradle to set the same version for two dependencies? 我无法使用Java类.error获取资源:尝试调用虚拟方法 - I can't get resources using java class .error: Attempt to invoke virtual method 如何在java中设置SSL协议版本? 我怎么知道是哪一个? javax.net.ssl.SSLException:收到致命警报:protocol_version - How do I set SSL protocol version in java? And how do I know which one? javax.net.ssl.SSLException: Received fatal alert: protocol_version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM