关于compose和kotlin的一些兼容性问题

最近需要将项目中的一些模块封装成aar给客户使用。于是新建了构建脚本为KSL项目。但在项目模块迁移的过程中出现了兼容性问题。记录下方便后来者。

以下是出现的兼容性问题之一:

androidx.compose.compiler.plugins.kotlin.IncompatibleComposeRuntimeVersionException: You are using an outdated version of Compose Runtime that is not compatible with the version of the Compose Compiler plugin you have installed. The compose compiler plugin you are using (version 1.4.3) expects a minimum runtime version of 1.0.0

.这个问题要添加运行时依赖项:

implementation("androidx.compose.runtime:runtime:1.5.0-alpha01")

以下是app下的build.gradle.kts

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.xxx.xxx"
    compileSdk = 33

    defaultConfig {
        applicationId = "com.xxx.xxx"
        minSdk = 23
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"//1.4.3
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
    implementation("androidx.activity:activity-compose:1.7.0")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    

    implementation("androidx.compose.runtime:runtime:1.5.0-alpha01")

}

以下是需要封装模块下的build.gradle.kts

plugins {
    id("com.android.library")
    id("org.greenrobot.greendao")
    id("kotlin-android")
    id("kotlin-kapt")
}

android {
    namespace = "com.stonex.surpadbasesdk"
    compileSdk = 33

    defaultConfig {
        minSdk = 23

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")

        ndk {
            //设置支持的so库架构,iAppPDFLib只支持armeabi
            //abiFilters 'armeabi-v7a', 'arm64-v8a'//, 'x86', 'x86_64' //不支持armeabi
            //abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
            abiFilters += setOf("armeabi","armeabi-v7a","x86", "mips")
            //abiFilters "armeabi"
        }


    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    dataBinding {
        enable=true
    }
    buildFeatures {
        dataBinding=true
        compose=true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"//1.4.3
    }

    buildFeatures {
        viewBinding=true
    }
    greendao {
        schemaVersion=1 //数据库版本号
        daoPackage="com.xxx.xxx// 设置DaoMaster、DaoSession、Dao 包名
        //targetGenDir=File("src/main/java")//设置DaoMaster、DaoSession、Dao目录,
        generateTests=false //设置为true以自动生成单元测试。
        //targetGenDirTests=File("src/main/java") //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
    }
}

dependencies {

    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.8.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

    implementation("androidx.compose.runtime:runtime:1.5.0-alpha01")

    

    /*okhttp网络框架*/
    api("com.squareup.okhttp3:okhttp:4.0.1")
    /*retrofit框架*/
    api("com.squareup.retrofit2:retrofit:2.7.2")
    api("com.squareup.retrofit2:adapter-rxjava2:2.7.2")

    /*rxjava框架*/
    api("io.reactivex.rxjava2:rxjava:2.2.21")
    api("io.reactivex.rxjava2:rxandroid:2.1.1")

    /*rxrelay*/
    api("com.jakewharton.rxrelay2:rxrelay:2.1.0")
    /**
     * 使用Rxlifecycle解决RxJava引起的内存泄漏
     */
    api("com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.2")

    //LifecycleService存在于扩展包中
    api("android.arch.lifecycle:extensions:1.1.1")
    /*gson*/
    api("com.google.code.gson:gson:2.8.5")

    /*greendao数据库框架*/
    api("org.greenrobot:greendao:3.3.0")
    implementation("org.greenrobot:greendao-generator:3.3.0")
    //支持数据库数据迁移
    api("io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1")
    //快速适配器
    api("com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4")

    /*圆形图片*/
    api("de.hdodenhof:circleimageview:2.2.0")
    /*图形库glide*/
    api("com.github.bumptech.glide:glide:4.8.0")
    implementation("jp.wasabeef:glide-transformations:3.0.1")

    /*switchbutton*/
    api("com.kyleduo.switchbutton:library:2.0.0")
    /*状态栏*/
    api("com.gyf.immersionbar:immersionbar:2.3.3-beta15")
    //mmkv
    implementation("com.tencent:mmkv-static:1.2.7")
    /*dialog*/
    api("com.afollestad.material-dialogs:core:0.9.6.0")
    /*loading特效*/
    api("com.wang.avi:library:2.1.3")

    //viewmodel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")


    
}
以下是项目下的build.gradle.kts
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {

        mavenCentral() // add repository
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.0.2")
        classpath("org.greenrobot:greendao-gradle-plugin:3.3.1")
        classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
    }
}

plugins {
    id("com.android.application") version "8.1.1" apply false
    id("org.jetbrains.kotlin.android") version "1.8.10" apply false
    //id("org.jetbrains.kotlin.android") version "1.7.10" apply false
    id("com.android.library") version "8.1.1" apply false
}
注意compose
composeOptions {
    kotlinCompilerExtensionVersion = "1.4.3"//1.4.3
}

和kotlin的兼容性问题

id("org.jetbrains.kotlin.android") version "1.8.10" apply false

以下是兼容对照表:

Compose 与 Kotlin 的兼容性对应关系  |  Android 开发者  |  Android Developers (google.cn)

关于compose和kotlin的一些兼容性问题_第1张图片

注意这个编译选项

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

和下面这里需要一致:

关于compose和kotlin的一些兼容性问题_第2张图片

注意setting.gradle.kts,防止依赖下载不下来:

import java.net.URI

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()

        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/google"))}
        maven { url=(uri("https://maven.aliyun.com/repository/public")) }
        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/jcenter"))}

        maven { url=(uri("https://maven.aliyun.com/nexus/content/groups/public/"))}

        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/gradle-plugin'"))}
        maven { url=(uri("https://jitpack.io"))}
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()

        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/google"))}
        maven { url=(uri("https://maven.aliyun.com/repository/public")) }
        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/jcenter"))}

        maven { url=(uri("https://maven.aliyun.com/nexus/content/groups/public/"))}

        maven { url=(uri("https://maven.aliyun.com/nexus/content/repositories/gradle-plugin'"))}
        maven { url=(uri("https://jitpack.io"))}
    }
}

rootProject.name = "xxx"
include(":app")
include(":xxx")

编译成功!

你可能感兴趣的:(kotlin,开发语言,android)