android、Jetpack Compose使用 Room数据库kapt注解时出现的相关问题整理。

535 阅读3分钟

谢谢阅览、关注!!

一、问题描述:

问题 1:> a failure occurred while executing org.jetbrains.kotlin.compilerrunner.gradlecompilerrunnerwithworkers$gradlekotlincompilerworkaction > internal compiler error. see log for more details

问题 2:a failure occurred while executing org.jetbrains.kotlin.gradle.internal.kaptwithoutkotlinctask$kaptexecutionworkaction

问题 3:execution failed for task ':app:kspdebugkotlin'. > could not resolve all files for configuration ':app:kotlin-extension'. > could not find androidx.compose.compiler:compiler:1.9.0.

问题 4:task :app:kspdebugkotlin failed e: java.lang.stackoverflowerror

二、原因

其他错误只要有“ksp”或“kapt”关键字,只要 ksp\kapt按官方文档正常添加依赖,那么问题的根本原因多数就是因为 Gradle\Compose\Kotlin三者之间版本不兼容所导致。

三、Compose与 Kotlin不同版本兼容性:

1、Compose与 Kotlin 的兼容性对应关系:developer.android.com/jetpack/and…,compose 和 Kotlin开发使用的版本需按此链接中的说明象对应,

//项目级别 build.gradle 
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false  //kotlin版本
}

转存失败,建议直接上传图片文件

//app级 build.gradle 配置文件android{}标签中 compose的版本
   composeOptions {
        kotlinCompilerExtensionVersion '1.4.1'
    }

转存失败,建议直接上传图片文件

2、Gradle的版本,主要影响编译相关的问题,一般AndroidStudio建立好项目都是没有问题的,若出现问题可能就是 java版本需要调整是坑需要降低、更新 Gradle版本,来与 java(jvm)适配。

四、提供可正常运行 app的配置。

1、app下的 build.gradle

plugins {
    id  ' xx.xx.xxxx.xxxx'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    namespace ' xx.xx.xxxx.xxxx'
    compileSdk 34

    defaultConfig {
        applicationId 'com.kiudysng.shortvideoproject'
        minSdk 26
        targetSdk 34
        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_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    buildFeatures {
        compose true
    }
    buildFeatures {
        viewBinding true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.1'
    }
//    packaging {
//        resources {
//            excludes += '/META-INF/{AL2.0,LGPL2.1}'
//        }
//    }

}
dependencies {

    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation platform('androidx.compose:compose-bom:2023.08.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    implementation project(':sdk')
//    implementation project(':short_video_sdk')
    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.08.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
    debugImplementation 'androidx.compose.foundation:foundation-layout-android:1.6.2'
//
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.firebase:firebase-firestore-ktx:24.10.0'


    //图片加载
    implementation 'com.github.bumptech.glide:glide:4.16.0'

    // 协程核心库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
    // 协程Android支持库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3"
    // 协程Java8支持库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.3"

    // Retrofit
    api 'com.squareup.retrofit2:retrofit:2.9.0'
    api 'com.squareup.retrofit2:converter-moshi:2.9.0'
    api 'com.squareup.okhttp3:logging-interceptor:4.9.0'
    api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
    api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
    // Retrofit gson转换器
    api 'com.squareup.retrofit2:converter-gson:2.9.0'
    //RecyclerView 适配器
    implementation "io.github.cymchad:BaseRecyclerViewAdapterHelper4:4.1.4"
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'

    //exoplayer 播放M3u8视频
    implementation 'com.google.android.exoplayer:exoplayer-core:2.19.1'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.19.1'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.19.1' // 如果需要支持 HLS
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.19.1' // 如果需要支持 SmoothStreaming
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.19.1'

    //banner
    implementation 'io.github.youth5201314:banner:2.2.2'


    //room数据库
    def room_version = "2.5.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    implementation "androidx.room:room-rxjava3:$room_version"
    implementation "androidx.room:room-paging:$room_version"

    // 协程核心库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
    // 协程Android支持库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3"
    // 协程Java8支持库
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.3"

    //通信类
    implementation("org.greenrobot:eventbus:3.3.1")

    //订阅
    def billing_version = "6.0.1"
    implementation "com.android.billingclient:billing-ktx:$billing_version"
    implementation("com.android.billingclient:billing:$billing_version")


    //评分
    implementation("com.google.android.play:review:2.0.1")
    //若使用kotlin同时导入
    implementation("com.google.android.play:review-ktx:2.0.1")

    //Admob广告
    implementation ("com.google.android.gms:play-services-ads:22.6.0")
}

转存失败,建议直接上传图片文件

2、项目级下的 build.gradle

plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

转存失败,建议直接上传图片文件

3、settings.gradle

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "A05_2"
include ':app'
include ':sdk'

转存失败,建议直接上传图片文件

4、gradle版本:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-8.0-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

转存失败,建议直接上传图片文件

如有其他问题请联系博主。

谢谢阅读,烦请关注:

后续将持续更新!!