Android Studio 构建错误 - 无法在预览中预览 UI window

Android Studio build error - unable to preview UI in preview window

坦白说,我没有理解和调试这个错误的知识。所以如果我遗漏了什么,请告诉我。

我怎么会出现这个错误:

  1. 我想使用 compose 并在预览中查看预览 window
  2. 我创建了一个新的 android studio 项目,其中包含空的 compose activity
  3. 构建成功,但未加载预览。它只是在那里显示“构建和刷新”按钮。
  4. 当我单击“构建并刷新”按钮时 android Studio 再次启动构建过程并失败并显示以下消息:
FAILURE: Build failed with an exception. 

What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

在查看日志时,它仍然没有让我对问题有任何具体的了解:

Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    /Users/user1/.gradle/caches/transforms-3/ece73d8c62f06068285ab9f622839930/transformed/jetified-kotlin-stdlib-jdk8-1.5.30.jar (version 1.5)
    /Users/user1/.gradle/caches/transforms-3/eb2652034b57731d1f9e2d78d329ace4/transformed/jetified-kotlin-stdlib-jdk7-1.5.30.jar (version 1.5)
    /Users/user1/.gradle/caches/transforms-3/201581d8ae804b86784bd93bdadbd711/transformed/jetified-kotlin-stdlib-1.6.10.jar (version 1.6)
    /Users/user1/.gradle/caches/transforms-3/b9df9c1454100a3c8c23b28a1d48a919/transformed/jetified-kotlin-stdlib-common-1.6.10.jar (version 1.6)

我的应用级别build.gradle 文件:

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

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.composebasics"
        minSdk 23
        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
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.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.1'
    implementation 'androidx.activity:activity-compose:1.4.0'
    testImplementation 'junit:junit:4.13.2'
    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"

我的项目级别 build.gradle 文件是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        compose_version = '1.1.0'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

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

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

我不知道上面的信息有多少以及相关性如何,但如果需要我可以添加更多。

在项目级别 gradle 更改 kotlin 版本以与 compose 1.1.0

兼容

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

勾选这个version compatibility