Android错误:java.lang.IllegalStateException:在build.gradle中找不到androidx.compose.runtime.derivedStateOf函数

时间:2020-10-02 11:58:28

标签: android android-jetpack android-jetpack-compose

我正在尝试构建我的JetpackCompose项目,在构建该项目时遇到以下错误,

java.lang.IllegalStateException: Function not found androidx.compose.runtime.derived state of

在我的模块级build.gradle之下查找

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

android {
compileSdkVersion 30

defaultConfig {
    applicationId "com.example.samplejetpackcompose"
    minSdkVersion 21
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerVersion = "1.3.70-dev-withExperimentalGoogleExtensions-20200424"
    kotlinCompilerExtensionVersion "0.1.0-dev13"
}
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'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.ui:ui-tooling:$compose_version"
}

和根级别build.gradle如下,

buildscript {
ext {
    kotlin_version = "1.4.10"
    compose_version = '1.0.0-alpha03'
}
repositories {
    google()
    jcenter()
}
dependencies {
    classpath "com.android.tools.build:gradle:4.2.0-alpha13"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
repositories {
    google()
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

1 个答案:

答案 0 :(得分:0)

我在模块级别“ app.gradle”中更改了composeOption

composeOptions {
    kotlinCompilerExtensionVersion compose_version
    kotlinCompilerVersion '1.4.10'
}

以及在项目级别的build.gradle

来自

compose_version = '1.0.0-alpha03'

compose_version = '1.0.0-alpha04'

更改这些内容后,build正确地发生了,我的MainActivity.kt改编了Compose代码。但是在这里,我开始在MainActivity.kt中遇到另一个编译时问题,它说

Class 'androidx.compose.runtime.Composition' is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler

经过大量搜索后,按照This Google Compose Link

解决了此问题

只需在模块级build.gradle

中添加以下内容
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
    jvmTarget = "1.8"
    freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
}
}