Reputation: 2779
I changed my Kotlin version from 1.6.10 to 1.7.0.
from this
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10'
upgrated to
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.0'
But Hilt throws an error. My Hilt version is 2.42. Is there a way to fix this without downgrading again? It works fine in Kotlin 1.6.10 and Hilt 2.42. But I want to use it by upgrading my kotlin version.
Upvotes: 29
Views: 31359
Reputation: 111
You must upgrade Hilt to latest version (actually v2.50)
Follow steps for install Dagger:
Add dagger.hilt in your build.gradle.kts (Project:NameProject):
plugins {
id("com.android.application") version "8.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
id("com.google.dagger.hilt.android") version "2.50" apply false
}
Add dagger.hilt in your build.gradle.kts (Module:App):
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
kotlin("kapt")
id("com.google.dagger.hilt.android")
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.dagger:hilt-android:2.50")
kapt("com.google.dagger:hilt-android-compiler:2.50")
}
kapt {
correctErrorTypes = true
}
Create Application in your MainActivity package:
package com.example.kotlinfundamentals
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class MyApplication : Application() {
}
Add Application name in Manifest file:
<application
android:name=".MyApplication">
</application>
Create class for example User class:
package com.example.kotlinfundamentals
data class User(
val id: Int,
val lastname: String,
val firstname: String,
val email: String
)
Create Hilt modules to provide instances of concrete types:
package com.example.kotlinfundamentals
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
object UserModule {
@Provides
fun provideUser(): User {
// Implémentez la logique pour créer et retourner un User
// Vous pouvez utiliser des injections de dépendances ici
return User(1, "John", "Doe", "[email protected]")
}
}
Inject dependency in your Activity:
package com.example.kotlinfundamentals
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var user: User
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val userTxtview: TextView = findViewById(R.id.txtview);
// Accéder aux attributs de User
val userId = user.id
val userFirstName = user.firstname
val userLastName = user.lastname
val email = user.email
userTxtview.text = userFirstName
} }
I hope, I helped someone
Best Regards
Upvotes: 2
Reputation: 16235
In my case it turned out that the Hilt version in the implementation and the classpath were different, making them the same solved the issue. A good practice should be using a version variable.
They were like this: In the build.gradle file of the app module
implementation "com.google.dagger:hilt-android:2.39.1"
In the build.gradle file of the project
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1'
Upvotes: 4
Reputation: 2779
problem solved when i added it like this
plugins {
id 'androidx.navigation.safeargs' version '2.4.1'
id 'dagger.hilt.android.plugin'
id "org.jetbrains.kotlin.plugin.parcelize" version "1.6.0-M1"
id 'com.android.library'
id 'org.jetbrains.kotlin.android' version '1.7.0'
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'dagger.hilt.android.plugin') {
useModule("com.google.dagger:hilt-android-gradle-plugin:2.42")
}
if (requested.id.id == 'com.google.gms.google-services') {
useModule("com.google.gms:google-services:4.3.10")
}
}
}
Upvotes: 0
Reputation: 1109
In my case that build error was happening after adding any new module to the project.
For some reason Android Studio (2021.2.1) is changing the
org.jetbrains.kotlin:kotlin-gradle-plugin
version in the main build.gradle file after the module is added, upgrading its version to the latest one. This brings up the mentioned issue. Just revert this change, if that is the case.
Upvotes: 1
Reputation: 6474
Dagger/Hilt version 2.43.2 appears to have fixed this issue.
See https://github.com/google/dagger/releases/tag/dagger-2.43.2
Upvotes: 24
Reputation: 2844
You can add kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2"
and the problem will go away, however if you are using Jetpack Compose then you will have to downgrade your Kotlin version to 1.6.10
as Compose compiler is not compatible with Kotlin 1.7.0
as of yet.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.10' apply false
id 'com.google.dagger.hilt.android' version '2.42' apply false
}
Upvotes: 13
Reputation: 923
it is saying that u have different verisons of plugin and dependencies
In your project level build.gradle file or Settings.gradle file check for kotlin version you haven't updated it while u have updated your dependency, just change the kotlin version to 1.7.0
Upvotes: 1
Reputation: 286
You may change your kotlin gradle plugin to the same version:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
Here's my components version and it works:
hilt/hilt gradle plugin:2.42
dagger2:2.35.1
kotlin/kotlin gradle plugin:1.6.21
Upvotes: 2