【问题标题】:Problems with android Composeandroid Compose 的问题
【发布时间】:2022-06-20 21:30:06
【问题描述】:

我正在尝试完成关于 android compose 的教程。在我使用:kotlin-gradle-plugin:1.5.31 时效果很好,但 android studio 已更新为:kotlin-gradle-plugin:1.6.10,并且当我再次尝试运行该程序时,它不会编译。我不明白问题是什么。但即使在旧版本的原始运行中,我也会收到以下消息:

w:注意! 此构建使用不安全的内部编译器参数: -XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes 不建议将此模式用于生产用途, 因为没有给出稳定性/兼容性保证 编译器或生成的代码。使用它需要您自担风险!

当我尝试新版本时,它告诉我必须迁移代码,但是当我这样做时,我收到以下消息:

e:Compose 编译器的此版本 (1.0.5) 需要 Kotlin 版本 1.5.31,但您似乎使用的是未知兼容的 Kotlin 版本 1.6.10。请修正你的配置(或suppressKotlinVersionCompatibilityCheck,但不要说我没有警告你!)。

任务:app:mergeExtDexDebug FAILURE:构建失败并出现异常。

  • 出了什么问题: 任务 ':app:compileDebugKotlin' 执行失败。

编译错误。查看日志了解更多详情

  • 尝试: 使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。
  • https://help.gradle.org 获得更多帮助 10 秒内构建失败 25 个可操作的任务:23 个已执行,2 个是最新的

即使你想迁移回来也不能。

我不知道该怎么办或如何继续前进。

【问题讨论】:

  • 当前compose 1.0版本只支持1.5.31。

标签: android kotlin android-jetpack-compose


【解决方案1】:

我设法发现 kotlin 1.6.10 的正确 compos 版本1.2.0-alpha01,但我仍然收到消息说这不是稳定版本,不应该用于生产。但至少我的程序又运行了。

【讨论】:

  • compos 版本再次更新为 1.2.0-alpha03。
  • 1.1.0 是一个稳定版本。所以我认为使用稳定版是一个不错的选择。
  • 因为 Compose 还是比较新的,今天稳定的东西明天可能就不那么稳定了,不要执着于一个版本。
【解决方案2】:

最后,找出并修复问题以运行 compose version

目前稳定版 1.1.0

这是我的 build.gradle(Project:app)

代码
buildscript {
    ext {
        compose_version = '1.1.0'
    }

    dependencies {

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.1' apply false
    id 'com.android.library' version '7.1.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

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

build.gradle(模块)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.dreammeanings"
        minSdk 21
        targetSdk 32
        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'
    }
    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.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"
}

【讨论】:

    【解决方案3】:

    在我的例子中,我在 build.gradleapp 模块中为 composeOptions 指定了 kotlinCompilerExtensionVersion 并修复了错误:

    build.gradle(app)

    android {
       composeOptions { kotlinCompilerExtensionVersion = "your compose version"}
    }
    

    【讨论】:

      【解决方案4】:

      我认为您的问题在于 Compose 和 Kotlin 的版本。 前几天遇到了类似的错误,更新了Kotlin和Compose版本后,问题就解决了。

      首先改变composeOptions:

      composeOptions {
          kotlinCompilerExtensionVersion compose_version
          //kotlinCompilerVersion '1.5.31'  the old version 
          kotlinCompilerVersion '1.6.21'
      }
      

      然后更新所有依赖版本

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

      然后将此类路径添加到 gradle 依赖项

      dependencies {
        
          //the older version of kotlin
          //classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
        
          classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
          
          .
          .
          .
      

      }

      最后,我认为这是 Kotlin 和 compose 库之间的版本兼容性问题。

      你只需要更新你的依赖。

      注意:我在依赖项中使用的 $compose_version 是 buildscript 中的变量

      buildscript {
          ext {
              kotlin_version = "1.6.21"
              //compose_version = '1.0.5'
              compose_version = '1.2.0-rc01'
           }
           .
           .
           .
      }
      

      希望它可以帮助您解决问题。

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 2023-02-24
        • 2021-09-20
        • 2022-11-12
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 1970-01-01
        相关资源
        最近更新 更多