在执行任务“:app:kaptDebugKotlin”时出现了java.lang.reflect.InvocationTargetException(没有错误消息)。

7

我遇到了以下错误,但找不到解决方法。

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

我无法确定上一次我做了什么,可能是某个库引起了这个错误,而我已经更新了它。

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
}

这是我的库及其版本。希望这有所帮助。我也会提供完整的错误日志,这样你就拥有了和我一样的信息。
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
}

如果您需要更多的片段,请告诉我,但希望这些片段不会太多,让你感到困扰。

按照要求,这是我使用的 Dao:

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 类的代码吗? - rahat
这就是整个数据库类。我没有添加回调。 - Padder
你能把“Dao”接口的代码粘贴到问题中吗? - rahat
道成功添加。 - Padder
我在使用Android的Room codelab时遇到了同样的问题,所以这与Dagger无关,对我来说,问题是在Dao函数中标记为@Delete的参数类型错误(我将id放错成了item)。我没有看出你的代码有什么问题,但我猜测错误是由于代码生成器无法将注释应用到函数上造成的。我不太懂SQL,但我对你的“ORDER BY like=1 DESC”表示怀疑。 - alr3000
你好 alr3000,哇,谢谢你提供这些信息。所有的 SQL 语句都非常好用,包括所有的排序语句。为了找到这个错误,我注释掉了每一个 SQL 语句,但是问题还没有解决。 :( - Padder
5个回答

5

如果某个库使用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'

1
在我的情况下,这是一个与使用kapt添加的依赖项相关的问题。我将它们删除了,现在它可以工作了。而且,在我的情况下,这些依赖项并不是必需的,幸运的是。 - Andracchi

3

我使用compileSdkVersion 31时出现了kapt错误,但当我改成30时,错误就消失了。


android { compileSdkVersion 30 buildToolsVersion "30.0.0" - Wai Yan

3

也许很多人已经知道了这个技巧,但是由于Android Studio错误信息的模糊不清,我直到最近才发现它的妙用。

在Android Studio中打开终端窗口,然后在提示符下键入以下命令:

./gradlew build -stacktrace

从返回的堆栈跟踪信息中查找问题所在,并精确地确定错误。诶呀!问题解决了!


1

我通过在 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)
}

-1

对于遇到相同问题的用户,

出现错误是因为更新了库,并且在viewholder内部有一个注释引起的。

@ViewModelInject已经过时,需要用@Inject进行替换,并且该类需要用@HiltViewModel进行注释。

例如:
旧代码:已过时

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

新的:

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

现在一切都正常工作了:))

祝你有美好的一天


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接