构建项目时添加 Hilt 时出现错误。

huangapple go评论51阅读模式
英文:

Build error during adding Hilt to the project

问题

我试图为我的MVVM项目添加Hilt支持。我正在使用这个教程。但是我遇到了这样的错误:

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)

我的build.gradle(app):

buildscript {
    ext.kotlin_version = '1.3.71'
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'org.jetbrains.kotlin.android'
}

apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlinx-serialization'

android {
    // ...
    compileSdkVersion 33
    // ...

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }
}

dependencies {
    // ...
    implementation "com.google.dagger:hilt-android:2.45"
    kapt "com.google.dagger:hilt-android-compiler:2.38.1"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
}

和项目的build.gradle:

buildscript {
    // ...

    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20'
        classpath 'com.google.gms:google-services:4.3.15'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.41"
    }
}

// ...

我有这样的VM:

@HiltViewModel
class AppViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel() {
    // ...
}

带有存储库的:

class MainRepository @Inject constructor(private val retrofitService: RetrofitService) {
    // ...
}

和这样的提供者:

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {

    @Provides
    fun provideWeatherService(@ApplicationContext context: Context): RetrofitService {
        // ...
    }
}

而且我还在我的片段中添加了:

@AndroidEntryPoint
class PersonalPage : Fragment(R.layout.fragment_personal_page) {
    // ...
}

但我不确定问题是否在添加类注解附近。所以除了这些提供者之外,我需要添加什么来获取可构建和可工作的项目?

更新

在日志中找到了这样的错误:

error: [Hilt]
  Unsupported metadata version. Check that your Kotlin version is >= 1.0: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0

但在哪里是Kotlin版本?

英文:

I'm trying to add Hilt support for my MVVM project. I'm using this tutorial. But I got such error:

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)

my build.gradle (app):

buildscript {
    ext.kotlin_version = '1.3.71'
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'org.jetbrains.kotlin.android'
}

apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlinx-serialization'


android {
 ...


    compileSdkVersion 33
 ....



    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }
}


dependencies {
....
    implementation "com.google.dagger:hilt-android:2.45"
    kapt "com.google.dagger:hilt-android-compiler:2.38.1"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
}

and build.gradle for project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

...

    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20'
        classpath 'com.google.gms:google-services:4.3.15'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.41"
    }
}

...

I have such VM:

@HiltViewModel
class AppViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel() {

...
}

with repository:

class MainRepository @Inject constructor(private val retrofitService: RetrofitService) {
...
}

and such providers:

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {

    @Provides
    fun provideWeatherService(@ApplicationContext context: Context): RetrofitService {
        val client = OkHttpClient.Builder()
            .addInterceptor(AppInterceptor(context))
            .connectTimeout(100, TimeUnit.SECONDS)
            .dispatcher(Dispatcher().apply {
                maxRequests = 1
            })
            .readTimeout(100, TimeUnit.SECONDS).build()


        client.dispatcher.cancelAll()

        val retrofit = Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

       return retrofit.create(APIService::class.java)
    }
}

and also I added to my fragment:

@AndroidEntryPoint
class PersonalPage : Fragment(R.layout.fragment_personal_page) {
...

But I'm not sure that the problem is near adding class annotations. So what I need to add except these providers for getting buildable and workable project?

UPDATE

Found in logs such error -

error: [Hilt]
  Unsupported metadata version. Check that your Kotlin version is >= 1.0: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0

but where is kotlin version is?

答案1

得分: 0

最终,经过大量的谷歌搜索,我发现在某些 Hilt 版本中,与 Kotlin 版本可能会出现此问题。对我来说,最佳的依赖配置是:

dependencies {
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    kapt("org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0")
    implementation 'com.google.dagger:hilt-android:2.38.1'
    kapt 'com.google.dagger:hilt-compiler:2.38.1'

    .... 
}

并且需要添加到类路径中:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"

然后一切都开始正常工作。

英文:

So finally after huge googling I found that in some Hilt versions such problem can happen with Kotlin versions. For me the best configuration with dependencies is:

dependencies {
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
kapt("org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0")
implementation 'com.google.dagger:hilt-android:2.38.1'
kapt 'com.google.dagger:hilt-compiler:2.38.1'

.... 
}

with adding to classpath:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"

and all started working well

huangapple
  • 本文由 发表于 2023年4月19日 21:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定