任务 ':app:kaptDebugKotlin' 的 java.lang.reflect.InitationTargetException (无错误消息)

Pad*_*der 8 android gradle android-library kotlin kapt

我遇到了以下错误,无法找到解决方案。

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)
Run Code Online (Sandbox Code Playgroud)

我不能说,我上次做了什么,可能有一些库导致了我更新的这个错误。 构建 Gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.padder.application"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "androidx.core:core-ktx:$ktxVersion"
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$testExtJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragmentVersion"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"

    // Lifecycle + ViewModel & LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "android.arch.lifecycle:common-java8:$lifecycleVersion"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

    // DataStore
    implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"

    // Room
    implementation "androidx.room:room-runtime:$roomVersion"
    kapt "androidx.room:room-compiler:$roomVersion"
    implementation "androidx.room:room-ktx:$roomVersion"

    // Dagger Hilt
    implementation "com.google.dagger:hilt-android:$hiltVersion"
    kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"
    kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"
}

kapt {
    correctErrorTypes true
}
Run Code Online (Sandbox Code Playgroud)

这些是我的库+版本。希望这有帮助。我也给你完整的错误日志,所以你有,我有。

Executing tasks: [:app:assembleDebug] in project C:\Users\Nutzer\Desktop\IntelliJ_Projects\Application

> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
[TASKS]
> Task :app:stripDebugDebugSymbols NO-SOURCE
> Task :app:validateSigningDebug UP-TO-DATE           ^
> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 28s
21 actionable tasks: 2 executed, 19 up-to-date
Run Code Online (Sandbox Code Playgroud)

因为错误里面是提到的数据库,所以我也给出了它。

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)
Run Code Online (Sandbox Code Playgroud)

只是说您是否需要更多片段,但希望这些片段不会让您逃跑。

正如我所希望的,这里是我使用的 Dao:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.padder.application"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "androidx.core:core-ktx:$ktxVersion"
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$testExtJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragmentVersion"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"

    // Lifecycle + ViewModel & LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "android.arch.lifecycle:common-java8:$lifecycleVersion"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

    // DataStore
    implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"

    // Room
    implementation "androidx.room:room-runtime:$roomVersion"
    kapt "androidx.room:room-compiler:$roomVersion"
    implementation "androidx.room:room-ktx:$roomVersion"

    // Dagger Hilt
    implementation "com.google.dagger:hilt-android:$hiltVersion"
    kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"
    kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"
}

kapt {
    correctErrorTypes true
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题。你能帮我消除这个错误吗?我到底做错了什么,或者我能做些什么来处理它。
谢谢你!

小智 5

如果某个库用于kapt注释处理,请确保该库及其注释处理器版本相同。

对我来说,以下原因导致了错误(版本不匹配):

implementation 'com.google.dagger:dagger:2.38.1'
kapt 'com.google.dagger:dagger-compiler:2.34.1'
Run Code Online (Sandbox Code Playgroud)

这有效(注意版本是相同的):

implementation 'com.google.dagger:dagger:2.38.1'
kapt 'com.google.dagger:dagger-compiler:2.38.1'
Run Code Online (Sandbox Code Playgroud)


Pad*_*der -1

和有同样问题的用户,

收到的错误是因为更新了库并且是由视图持有者内的注释引起的。

@ViewModelInject 已被废弃,需要用 @Inject 替换,并且该类需要用 @HiltViewModel 进行注释。

例如:
旧:已弃用

class MainViewModel @ViewModelInject constructor() : ViewModel() { ... }
Run Code Online (Sandbox Code Playgroud)

新的:

@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() { ... }
Run Code Online (Sandbox Code Playgroud)

现在一切又恢复正常了:))

祝你今天过得愉快