Android Compose版本兼容性问题

1,161 阅读1分钟

1、遇到的问题:

This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.7.0 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

2、找到 compose 与 kotlin 的兼容性版本对照表

Compose Compiler 版本兼容的 Kotlin 版本
使用 Compose Compiler Gradle 插件启用 Compose。2.0.0 及更高版本
1.5.141.9.24
1.5.131.9.23
1.5.121.9.23
1.5.111.9.23
1.5.101.9.22
1.5.91.9.22
1.5.81.9.22
1.5.71.9.21
1.5.61.9.21
1.5.51.9.20
1.5.41.9.20
1.5.31.9.10
1.5.21.9.0 版
1.5.11.9.0 版
1.5.01.9.0 版
1.4.81.8.22
1.4.71.8.21
1.4.61.8.20
1.4.51.8.20
1.4.41.8.10
1.4.31.8.10
1.4.21.8.10
1.4.11.8.0
1.4.01.8.0
1.4.0-alpha021.7.21
1.4.0-alpha011.7.20
1.3.21.7.20
1.3.11.7.10
1.3.01.7.10
1.3.0-rc021.7.10
1.3.0-rc011.7.10
1.3.0-beta011.7.10
1.2.01.7.0
1.2.0-rc021.6.21
1.2.0-rc011.6.21
1.2.0-beta031.6.21
1.2.0-beta021.6.21
1.2.0-beta011.6.21
1.2.0-alpha081.6.20
1.2.0-alpha071.6.10
1.2.0-alpha061.6.10
1.2.0-alpha051.6.10
1.2.0-alpha041.6.10
1.2.0-alpha031.6.10
1.2.0-alpha021.6.10
1.2.0-alpha011.6.10
1.1.11.6.10
1.1.01.6.10
1.1.0-rc031.6.10
1.1.0-rc021.6.10
1.1.0-rc011.6.0
1.1.0-beta041.6.0
1.1.0-beta031.5.31
1.1.0-beta021.5.31
1.1.0-beta011.5.31
1.1.0-alpha061.5.31
1.1.0-alpha051.5.31
1.0.51.5.31
1.0.41.5.31
1.1.0-alpha041.5.30
1.1.0-alpha031.5.30
1.0.31.5.30
1.1.0-alpha021.5.21
1.1.0-alpha011.5.21
1.0.21.5.21
1.0.11.5.21
1.0.01.5.10
1.0.0-rc021.5.10
1.0.0-rc011.5.10

3、解决问题:

解决思路:根据错误提示,分析如下:
  • 1、Compose(V1.1.1版本)和 Kotlin (V1.6.10版本) 是对应的;
  • 2、我们在项目中使用了Kotlin(V1.7.0版本);
  • 3、查看对照表Compose(V1.2.0版本)和 Kotlin (V1.7.0版本) 是对应的

所以,我们把项目配置为Compose(V1.2.0版本)和 Kotlin (V1.7.0版本) 就OK了。

4、更改代码:

发现在 project 下的 build.gradle 中有:

buildscript {
    ext {
        //1、Compose的版本更改为1.2.0
        compose_ui_version = '1.2.0'
    }
}
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    //2、Kotlin的版本更改为1.7.0
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

在module / build.gradle 中,增加

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

android {
    namespace 'com.example.mycomposedemo'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.mycomposedemo"
        minSdk 21
        targetSdk 33
        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 {
        //3、Compose的版本更改为1.2.0
        kotlinCompilerExtensionVersion '1.2.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    //4、Kotlin的版本更改为1.7.0
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    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_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
    //约束布局
    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-rc02"
    //显示图片
    implementation "io.coil-kt:coil-compose:2.2.2"
    implementation "io.coil-kt:coil-svg:2.2.2"
    implementation "com.github.skydoves:landscapist-coil:2.0.3"
    implementation "com.github.skydoves:landscapist-glide:2.1.0"
    implementation "com.github.skydoves:cloudy:0.1.1"
    implementation "io.coil-kt:coil-gif:2.3.0"
}

最终 gradle sync 后,编译运行成功了 ^~^