I have a project which I use Dagger2 & kotlin, I imported "org.jetbrains.kotlin:kotlin-stdlib:1.3.50” in my build.gradle and I was getting this Error:
w: Runtime JAR files in the classpath should have the same version.
These files were found in the classpath:
w:
/Users/macbook/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre7/1.1.51/8b5933578dc55f32cfc1a25f1db6371e4161fb8f/kotlin-stdlib-jre7-1.1.51.jar:
kotlin-stdlib-jre7 is deprecated. Please use kotlin-stdlib-jdk7
instead
So therefore I changed from "kotlin-stdlib" to "kotlin-stdlib-jdk7" using:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50” and I am still getting this error.
I then made some research and found this URL: warning: Kotlin runtime JAR files in the classpath should have the same version
I found the usage of kotlin-reflect: "implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
I added it to my build.gradle BUT I still have the same errors
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7: 1.3.50"
implementation "org.jetbrains.kotlin:kotlin-reflect: 1.3.50"
This is my BaseApplication class below, which I am calling the Dagger components :
class BaseApplication : Application() {
companion object {
#JvmStatic lateinit var netComponent: NetComponent
#JvmStatic lateinit var exampleComponent: ExampleComponent
}
override fun onCreate() {
super.onCreate()
netComponent = DaggerNetComponent.builder()
.appModule(AppModule(this))
.netModule(NetModule())
.build()
exampleComponent = DaggerExampleComponent.builder()
.netComponent(netComponent)
.retrofitModule(RetrofitModule())
.exampleModule(ExampleModule(this))
.build()
}
}
Related
In my project, using dagger-Hilt
just by adding a new activity, it shows an error
:app:kaptDebugKotlin
[Hilt]
java.lang.reflect.InvocationTargetException (no error message)
but before adding a new activity, the whole project works fine
Activity :->
#AndroidEntryPoint
class SplashScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
}
}
my build.gradle
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath("com.google.dagger:hilt-android-gradle-plugin:2.38.1")
I have reproduced your issue. The real problem is with versions,your kotlin version is not compatible with hilt plugin version and hilt library version. Here are the few changes you need to do.
In project build.gradle, update hilt plugin version to 2.42
classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
In app build.gradle, update to latest versions like below.
implementation("com.google.dagger:hilt-android:2.42")
kapt("com.google.dagger:hilt-android-compiler:2.42")
kapt "androidx.hilt:hilt-compiler:1.0.0"
Hilt lifecycle module not required in latest hilt version.
You should remove the below line in build.gradle and remove all androidx.hilt.lifecycle.ViewModelInject imports in your project.Other wise it will throw error
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
I'm just starting with Jetpack Compose and Hilt. But I'm running into issues when I inject into a ViewModel.
The error that I get:
java.lang.RuntimeException: Cannot create an instance of class com.example.chaes.login.viewModel.SignUpViewModel
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.chaes.login.viewModel.SignUpViewModel> has no zero argument constructor
I can inject all fine in the Activity but not in the ViewModel. I've tried all solutions I could find.
My gradle files:
Project root level:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
classpath 'com.google.gms:google-services:4.3.8'
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.37'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Module level:
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
kapt{
correctErrorTypes true
}
dependencies {
...
// Hilt
implementation "com.google.dagger:hilt-android:2.37"
kapt "com.google.dagger:hilt-android-compiler:2.37"
...
}
My application file
#HiltAndroidApp
class BaseApplication: Application() {
}
My Module File:
#Module
#InstallIn(SingletonComponent::class)
object AuthRepoModule {
#Singleton
#Provides
fun provideAuthRepo(): FirebaseAuthRepo{
return FirebaseAuthRepo()
}
#Singleton
#Provides
fun provideRandomString(): String{
return "gejifeg"
}
}
The project is single activity with composable screens so the MainActivity:
#AndroidEntryPoint
class MainActivity : AppCompatActivity() { ... }
ViewModel:
#HiltViewModel
class SignUpViewModel #Inject constructor(
firebaseAuthRepo: FirebaseAuthRepo,
) : ViewModel() { ... }
Things I've tried:
Changing to ViewModelComponent instead of Singleton
changing to kapt "com.google.dagger:hilt-compiler:2.37" instead of kapt "com.google.dagger:hilt-android-compiler:2.37"
deleting the build folder and rebuilding the project
invalidating cache and restarting
Edit: Solution Found!
As mentioned by #sitatech in the comments, one needs to use hiltViewModel() instead of viewModel() to provide the viewModel to the composable.
In app module dependency add
...
// Hilt
implementation "com.google.dagger:hilt-android:2.37"
kapt "com.google.dagger:hilt-android-compiler:2.37"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha03"
...
Other portions seem ok to me.
As mentioned by #sitatech in the comments, I needed to use hiltViewModel() instead of viewModel() in the composable.
I have ViewModel
#HiltViewModel
class WControlViewModel #Inject constructor(
private val getProfile: GetProfile,
private val getHistory: GetHistory,
) : ViewModel() {...}
Activity
#AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val vModel: WControlViewModel by viewModels()
Application
#HiltAndroidApp
class WControlApp :Application()
But project not build, error :
return DefaultViewModelFactories.getActivityFactory(this);
^
required: ComponentActivity,Factory
found: Hilt_MainActivity
reason: actual and formal argument lists differ in length
I had the same issue, which I resolved by ensuring the hilt plugin version was upgraded. In my case I went from 2.33-beta to 2.35.1
So in the dependency in your project’s root build.gradle file
classpath "com.google.dagger:hilt-android-gradle-plugin:2.35.1"
You can find the latest version numbers here: https://mvnrepository.com/artifact/com.google.dagger/hilt-android-gradle-plugin
I had faced same issue so, I changed my project level dependency and app level dependency.
Try this
in App Level Gradle:
implementation "com.google.dagger:hilt-android:2.35.1"
kapt "com.google.dagger:hilt-android-compiler:2.35.1"
in Project Level Gradle:
classpath "com.google.dagger:hilt-android-gradle-plugin:2.35.1"
Happy Coding!!
I have implemented dependency injection in android before using dagger 2 but, recently, I have tried to use it in a new project but I get the following error:
error: cannot find symbol
import dagger.internal.InjectedFieldSignature;
^
symbol: class InjectedFieldSignature
location: package dagger.internal/location/to/App_MembersInjector.java:30: error: cannot find symbol
Here is my Application component:
#Singleton
#Component(
modules = [
(AndroidInjectionModule::class),
(VmModule::class),
(InjectorModule::class),
]
)
interface ApplicationComponent: AndroidInjector<Application> {
#Component.Builder
interface Builder{
#BindsInstance
fun application(application: App): Builder
fun build() : ApplicationComponent
}
fun inject(home: Home)
}
Then in my App class:
class App: Application(), HasAndroidInjector {
#Inject
lateinit var anAndroidInjector: DispatchingAndroidInjector<Any>
override fun onCreate() {
super.onCreate()
DaggerApplicationComponent.builder().application(this).build().inject(this)
}
override fun androidInjector(): AndroidInjector<Any> {
return anAndroidInjector
}
}
Then the injector module:
#Module
abstract class InjectorModule {
#ContributesAndroidInjector
abstract fun bindHomeActivity(): Home
}
The following is a small excerpt of my app Gradle to show the dagger version:
implementation 'com.google.dagger:dagger-android:2.24'
implementation 'com.google.dagger:dagger-android-support:2.24'
kapt 'com.google.dagger:dagger-android-processor:2.24'
kapt 'com.google.dagger:dagger-compiler:2.28'
If you have any clue, kindly let me know where the problem might be.
Your Dagger artifact versions don't match. Specifically, you are using dagger-compiler:2.28 to generate code, but including an dependency on Dagger 2.24 instead.
In the specific case of dagger.internal.InjectedFieldSignature, that class appears to have been introduced in Dagger version 2.25.3. Any later version of the Dagger compiler will expect that InjectedFieldSignature exists and can be used in generated code. However, since you're only including Dagger 2.24 in your project, the generated code ends up referring to a class that doesn't exist.
To fix this, make sure all of your Dagger dependencies use the same version.
I create simple application for Android with Dagger 2. It has one shared object and one module. The module is:
#Module
public class MyModule {
#Provides
#Singleton
public Hren providesHren() {
return new Hren();
}
}
This module works. But when I convert it to Kotlin, I get compile time error:
> Task :app:compileDebugJavaWithJavac
...../DaggerMyApplicationComponent.java:26: error: cannot find symbol
DoubleCheck.provider(MyModule_ProvidesHrenFactory.create(builder.myModule));
^
symbol: variable MyModule_ProvidesHrenFactory
location: class DaggerMyApplicationComponent
1 error
This module after conversion:
#Module
class MyModule {
#Provides
#Singleton
fun providesHren(): Hren {
return Hren()
}
}
What's wrong? Why I get this error? How to solve?
Ensure you have properly configured your project to support annotations with Kotlin.
In your build.gradle(Module:app) file, check that you have applied the following settings:
apply plugin: 'kotlin-kapt'
kapt "com.google.dagger:dagger-compiler:dagger_version"
kapt "com.google.dagger:dagger-android-processor:dagger_version"
Then, clean and rebuild your project:
./gradlew clean && ./gradlew build