Search code examples
javaandroidandroid-studioandroid-activitydagger-hilt

Hilt :- Not able to use the @AndroidEntryPoint in the AppCompatActivity getting DefaultV iewModelFactories exceptions?


I want to use the Hilt in my project for getting the helper class object as the common for my activity. I have tried the below lines of code for it

Manifest file entry

 <application
        android:name=".application.AppController"
        android:allowBackup="false"
   

My AppController class

@HiltAndroidApp
public class AppController extends Application implements Configuration.Provider {
}

Please check my gradles file for it

implementation "androidx.activity:activity-ktx:1.2.3"

implementation 'androidx.fragment:fragment-ktx:1.3.4'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
implementation 'com.google.dagger:hilt-android:2.36'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
kapt 'com.google.dagger:hilt-android-compiler:2.31.2-alpha'

Please check my classPath entry below

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

Now please check my NVModule Module class in which i have used the Hilt

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class NVModule {

    @Provides
    fun provideDeviceDiagnosticHelper(): DeviceDiagnosticHelper{
        return DeviceDiagnosticHelper()
    }
}

Now please check the Activity class in which i have injected the Hilt object

@AndroidEntryPoint
public class AutomaticTestActivity extends AppCompatActivity {


  private AutomaticTestPresenter automaticTestPresenter;

  private AutomaticTestView automaticTestView;

  
  ActivityDeviceDiagnosticBinding mBinding;

  @Inject
  DeviceDiagnosticHelper deviceDiagnosticHelper;
}

But I am getting the following exception while Running the app please check it once

Exception:-

/home/..../com/my/app/activity/Hilt_AutomaticTestActivity.java:70: error: method getActivityFactory in class DefaultViewModelFactories cannot be applied to given types; return DefaultViewModelFactories.getActivityFactory(this); ^ required: ComponentActivity,Factory found: Hilt_AutomaticTestActivity reason: actual and formal argument lists differ in length

Please help me to short of from this problem


Solution

  • Seems to be version mismatch. Use following versions:

    // hilt
    implementation 'com.google.dagger:hilt-android:2.36'
    kapt 'com.google.dagger:hilt-android-compiler:2.36'
    kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.36'
    kaptTest 'com.google.dagger:hilt-android-compiler:2.36'
    // implementation "androidx.hilt:hilt-navigation:1.0.0"
    // implementation "androidx.hilt:hilt-navigation-fragment:1.0.0"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    

    And

    apply plugin: 'dagger.hilt.android.plugin'
    

    And

    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.36'