【问题标题】:java.lang.reflect.InvocationTargetException (no error message) for task ':app:kaptDebugKotlin'任务':app:kaptDebugKotlin'的java.lang.reflect.InvocationTargetException(无错误消息)
【发布时间】:2021-04-30 06:22:41
【问题描述】:

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

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

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

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
}

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

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

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

package com.padder.application.data

import androidx.room.Database
import androidx.room.RoomDatabase
import javax.inject.Inject

@Database(entities = [Table1::class , Table2::class], version = 1) //exportSchema = false  is not working
abstract class Database : RoomDatabase() {

    abstract fun dao(): Dao
}

只是说如果你需要更多的剪辑,但希望这些不会有太多你不会逃跑的sn-ps。

这里是我使用的道:

package com.padder.application.data

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Dao
interface Dao {
    /**
     * SQL-Sort
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(item: Item)

    @Update
    suspend fun update(item: item)

    @Delete
    suspend fun delete(item: Item)

    // Searchqueries LIST-Fragment
    fun getItemList(sortState: SortSpinnerState, item: String, search: String) : Flow<List<Item>> =
        when (sortState) {
            SortSpinnerState.BY_NAME -> getItemListSortName(item, search)
            SortSpinnerState.BY_TASTE -> getItemListSortTaste(item, search)
            SortSpinnerState.BY_FAV -> getItemListSortFav(item, search)
            SortSpinnerState.BY_LIKE -> getItemListSortLike(item, search)
        }

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE marke = :marke AND name LIKE '%' || :search || '%' ORDER BY name ASC")
    fun getItemListSortName(marke: String, search: String): Flow<List<Item>>

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE marke = :marke AND name LIKE '%' || :search || '%' ORDER BY taste ASC")
    fun getItemListSortTaste(marke: String, search: String): Flow<List<Item>>

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE marke = :marke AND name LIKE '%' || :search || '%' ORDER BY fav = 1 DESC, name ASC")
    fun getItemListSortFav(marke: String, search: String): Flow<List<Item>>

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE marke = :marke AND name LIKE '%' || :search || '%' ORDER BY like = 1 DESC, like = 2 DESC, like = 3 ASC, name ASC")
    fun getItemListSortLike(marke: String, search: String): Flow<List<Item>>

    // Searchqueries HOME-Fragment
    fun getItemListHome(state: SearchByState, search: String) : Flow<List<Item>> =
        when (state) {
            SearchByState.BY_NAME -> getItemListHomeName(search)
            SearchByState.BY_TASTE -> getItemListHomeTaste(search)
            SearchByState.NO_SEARCH -> getItemListHomeNone()
        }

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE name LIKE '%' || :search || '%' ORDER BY name ASC")
    fun getItemListHomeName(search: String): Flow<List<Item>>

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE geschmack LIKE '%' || :search || '%' ORDER BY taste ASC")
    fun getItemListHomeTaste(search: String): Flow<List<Item>>

    @Query("SELECT nr, name, taste, marke, mag, fav FROM tabak WHERE nr = 0 ORDER BY taste ASC")
    fun getItemListHomeNone(): Flow<List<Item>>

    /**
     * SQL-Marke
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(marke: Marke)

    @Update
    suspend fun update(marke: Marke)

    @Delete
    suspend fun delete(marke: Marke)

    @Query("SELECT marke FROM marke ORDER BY nr")
    suspend fun getMarkeListe(): List<String>

    @Query("SELECT nr FROM marke WHERE marke = :marke")
    suspend fun getMarkeNr(marke: String): Int
}

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

【问题讨论】:

  • 你能添加更多Database类的代码
  • 这就是数据库类的全部内容。我没有添加回调。
  • 可以在问题中粘贴Dao接口代码吗?
  • 道成功添加
  • 我在使用 Android 的 Room codelab 时遇到了同样的问题,因此它与 Dagger 无关,在我的情况下,它在标记为 @Delete 的 Dao 函数中具有错误的参数类型(我把id而不是item)。我看不出你的有什么问题,但我猜这个错误是由于代码生成器无法将注释应用于函数。我不太了解 SQL,但我怀疑您的“ORDER BY like=1 DESC”

标签: android kotlin gradle android-library kapt


【解决方案1】:

当我使用 compileSdkVersion 31 而不是 30 时显示 kapt 错误

当我改为30时,错误消失

【讨论】:

  • android { compileSdkVersion 30 buildToolsVersion "30.0.0"
【解决方案2】:

和遇到同样问题的用户,

得到的错误是因为更新了库并且是由 viewholder 内的注释引起的。

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

例如:
旧:已弃用

class MainViewModel @ViewModelInject constructor() : ViewModel() { ... }

新:

@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() { ... }

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

祝你有美好的一天

【讨论】:

    【解决方案3】:

    我通过在 Hilt 模块中添加缺少的注释来修复它

    @Module //It was missing 
    @InstallIn(SingletonComponent::class) //It was missing 
    object NetworkModule {
    
       @Singleton
       @Provides
       fun getRetrofitApi(): ApiInterface = Retrofit.Builder()
           .baseUrl(BASE_URL)
           .addConverterFactory(GsonConverterFactory.create())
           .build()
           .create(ApiInterface::class.java)
    }
    

    【讨论】:

      【解决方案4】:

      如果某些库使用kapt 进行注释处理,请确保该库及其注释处理器版本相同。

      对我来说,以下是导致错误(版本不匹配)

      implementation 'com.google.dagger:dagger:2.38.1'
      kapt 'com.google.dagger:dagger-compiler:2.34.1'
      

      这可行(注意版本相同)

      implementation 'com.google.dagger:dagger:2.38.1'
      kapt 'com.google.dagger:dagger-compiler:2.38.1'
      

      【讨论】:

        猜你喜欢
        • 2021-08-21
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 2023-04-03
        • 1970-01-01
        相关资源
        最近更新 更多