【问题标题】:Unable to Run Kotlin App- Execution failed for task ':app:kaptDebugKotlin'无法运行 Kotlin 应用程序 - 任务 ':app:kaptDebugKotlin' 执行失败
【发布时间】:2022-06-11 01:06:35
【问题描述】:

我继续运行我的应用程序,它一直显示以下错误:

    Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

该应用无法在我的手机上启动。我试图清理项目并运行 ./gradlew clean 但仍然遇到相同的错误。应用程序构建成功,但无法运行。以下是我的项目级 gradle 文件:

    buildscript {
    ext.kotlin_version = '1.7.0-RC2'
    repositories {
        google()
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()

    }
}

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

以下是我的应用级 gradle 文件:

    apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdk 32
    defaultConfig {
        applicationId "com.schatzdesigns.mobileloanappuiconcept"
        minSdkVersion 16
        targetSdkVersion 32
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    buildToolsVersion '33.0.0 rc2'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'androidx.appcompat:appcompat:1.6.0-alpha04'
    implementation 'androidx.core:core-ktx:1.9.0-alpha04'
    implementation 'com.google.android.material:material:1.7.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.vectordrawable:vectordrawable:1.2.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0-rc01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    //noinspection LifecycleAnnotationProcessorWithJava8
    kapt 'androidx.lifecycle:lifecycle-compiler:2.5.0-rc01'

    implementation 'com.google.dagger:dagger:2.35.1'
    implementation 'com.google.dagger:dagger-android:2.35.1'
    implementation 'com.google.dagger:dagger-android-support:2.16'
    kapt 'com.google.dagger:dagger-android-processor:2.16'
    kapt 'com.google.dagger:dagger-compiler:2.16'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test:runner:1.5.0-alpha04'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha07'
    
}

kapt {
    generateStubs = false
}

【问题讨论】:

  • 尝试在buildToolsVersion下方添加kotlinOptions { jvmTarget = '11' }
  • 当我运行应用程序时没有出现同样的错误

标签: android kotlin gradle


【解决方案1】:

问题是您使用不同版本的 dagger-android 和 dagger-processor 导致 kapt 问题。

试试这个:

implementation 'com.google.dagger:dagger:2.21'
implementation 'com.google.dagger:dagger-android:2.16'
implementation 'com.google.dagger:dagger-android-support:2.16'
kapt 'com.google.dagger:dagger-android-processor:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'

或者,如果您需要升级 Dagger 版本,请尝试使用此处的升级指南Dagger 2 Releases 或升级到 Dagger Hilt。

【讨论】:

  • 2.21 dagger 版本是否进行了更改,并且可以正常工作,谢谢
【解决方案2】:

我在将 Kotlin 版本升级到 1.7.0 时遇到了这个问题

修复 - 将匕首版本更新到最新版本 - 2.42

实现“com.google.dagger:dagger:2.42”

kapt "com.google.dagger:dagger-compiler:2.42"

要查找最新的 dagger 版本 - https://github.com/google/dagger/releases

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多