【问题标题】:Cannot access rememberCoroutineScope() with jetPack Compose无法使用 jetPack Compose 访问 rememberCoroutineScope()
【发布时间】:2021-12-13 00:31:50
【问题描述】:

我无法访问rememberCoroutineScope(),我将android studio更新到最新版本,所以我该如何解决这个问题,

无法访问类“kotlinx.coroutines.CoroutineScope”。检查您的模块类路径是否存在缺失或冲突的依赖项

在我的主要活动中

import androidx.compose.runtime.rememberCoroutineScope

但仍然无法正常工作。

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.navigationdrawer_navigationmenu_jetpackcompose"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.5.10'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    implementation 'androidx.activity:activity-compose:1.4.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

    implementation("androidx.navigation:navigation-compose:2.4.0-beta02")
}
buildscript {
    ext {
        compose_version = '1.0.0'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
package com.example.navigationdrawer_navigationmenu_jetpackcompose

import android.os.Bundle
import com.example.navigationdrawer_navigationmenu_jetpackcompose.ui.theme.NavigationDrawerNavigationMenuJetpackComposeTheme
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.rememberNavController
import androidx.compose.runtime.rememberCoroutineScope

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NavigationDrawerNavigationMenuJetpackComposeTheme {
                NavigationDrawer()
            }
        }
    }
}

@Composable
fun NavigationDrawer() {
    val context = LocalContext.current
    val navController = rememberNavController()
    val scope = rememberCoroutineScope() // error here :
}

【问题讨论】:

    标签: android android-studio kotlin android-jetpack-compose kotlin-coroutines


    【解决方案1】:

    我从 beta 更新后解决了这个问题 jetback compose ,没有 kotlinx.coroutines 所以如果你想要 kotlinx.coroutines 并使用: 导入 androidx.compose.runtime.rememberCoroutineScope 和 记住CoroutineScope()

    在 Android 上使用 kotlinx.coroutines 时只需添加 kotlinx-coroutines-android 模块作为依赖项:

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      相关资源
      最近更新 更多