代码之家  ›  专栏  ›  技术社区  ›  somnathchakrabarti

从Kotlin 2.0开始,启用Compose时需要Compose编译器Gradle插件

  •  1
  • somnathchakrabarti  · 技术社区  · 6 月前

    以下是我的build.gradle(应用程序和项目)文件的片段,以及libs.versions.toml(版本目录)。由于我使用的是Kotlin 2.1.0版本,我启用了compose编译器,并遵循了下面提到的说明 Compose Compiler Gradle plugin 。但是,在尝试构建时,我仍然收到错误“需要编写编译器Gradle插件”

    A problem occurred configuring project ':app'.
    > com.android.builder.errors.EvalIssueException: Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required
      when compose is enabled. See the following link for more information:
      https://d.android.com/r/studio-ui/compose-compiler
    
    * Try:
    > Run with --stacktrace option to get the stack trace.
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.
    > Get more help at https://help.gradle.org.
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
    You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
    For more on this, please refer to https://docs.gradle.org/8.10.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
    BUILD FAILED in 231ms
    

    build.gradle(:app)

    plugins {
      id 'com.android.application'
      id 'kotlin-android'
      id 'com.google.gms.google-services'
      alias(libs.plugins.compose.compiler) apply false
    }
    
    android {
      compileSdk 35
    
      defaultConfig {...}
    
      buildTypes {...}
    
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1.8
        targetCompatibility JavaVersion.VERSION_1.8
      }
    
      kotlinOptions {
        jvmTarget = '1.8'
      }
    
      buildFeatures {
        compose true
      }
    
      composeOptions {
        kotlinCompilerExtensionVersion '1.5.1'
      }
    
      packaging {...}
    
    }
    
    dependencies {
      implementation libs.androidx.core.ktx
      implementation libs.androidx.appcompat
      implementation libs.material
      implementation libs.androidx.constraintlayout
      implementation libs.androidx.lifecycle.runtime.ktx
      implementation libs.androidx.activity.compose
      implementation platform(libs.androidx.compose.bom)
      implementation libs.androidx.compose.foundation
      implementation libs.androidx.ui
      implementation libs.androidx.ui.graphics
      implementation libs.androidx.ui.tooling.preview
      implementation libs.androidx.material3
      implementation libs.junit
      implementation libs.androidx.junit
      implementation libs.androidx.espresso.core
    
      //firebase google authentication
      implementation platform(libs.firebase.bom)
      implementation libs.firebase.auth
      implementation libs.play.services.auth
    
      androidTestImplementation platform(libs.androidx.compose.bom)
      androidTestImplementation libs.androidx.ui.test.junit4
      debugImplementation libs.androidx.ui.tooling
      debugImplementation libs.androidx.ui.test.manifest     
    
    }
    

    build.gradle(项目)

    buildScript {
      ext.kotlin_version = '2.1.0'
    
      repositories {
        google()
        mavenCentral()
      }
    
      dependencies {
        classpath 'com.android.tools.build:gradle:8.8.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
        classpath 'com.google.gms.google-services:4.4.2'
      }
    
    }
    
    tasks.register('clean', Delete) {
      delete rootProject.buildDir
    }
    

    libs.versions.toml(版本目录)

    [versions]  
    activityCompose = "1.10.0"  
    appcompat = "1.7.0"  
    composeBom = "2025.02.00"  
    constraintlayout = "2.2.0"  
    coreKtx = "1.15.0"  
    espressoCore = "3.6.1"  
    firebaseBom = "33.9.0"  
    junit = "4.13.2"  
    junitVersion = "1.2.1"  
    kotlin = "2.1.0"  
    lifecycleRuntimeKtx = "2.8.7"  
    material = "1.12.0"  
    playServicesAuth = "21.3.0"  
    [libraries]  
    androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" }
    androidx-appcompat = { module = "androidx.aappcompat:appcompat", version.ref = "appcompat" }
    androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBom" }
    androidx-compose-foundation = { module = "androidx.compose.foundation:foundation" }
    androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
    androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
    androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
    androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" }
    androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
    androidx-material3 = { module = "androidx.compose.material3:material3" }
    androidx-ui = { module = "androidx.compose.ui:ui" }
    androidx-ui-graphics = { module = "androidx.compose.ui:ui-graphics" }
    androidx-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" }
    androidx-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
    androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
    androidx-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
    firebase-auth = { module = "com.google.firebase:firebase-auth" }
    firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
    material = { module = "com.google.android.material:material", version.ref = "material" }
    play-services-auth = { module = "com.google.android.gms:play-services-auth", version.ref = "playServicesAuth" }
    
    [plugins]
    org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
    compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
    
    1 回复  |  直到 6 月前
        1
  •  1
  •   CommonsWare    6 月前

    替换:

    alias(libs.plugins.compose.compiler) apply false
    

    与:

    alias(libs.plugins.compose.compiler)
    
    推荐文章