Hilt Activity must be attached to an @AndroidEntryPoint Application
Asked Answered
T

8

33

The app crashes as soon as it gets installed and throws the weird error above. I have annotated the activity as shown below as well as its child fragments.

@AndroidEntryPoint
    class HomeActivity : AppCompatActivity() {
        companion object{
           lateinit var currentUser: User
        }
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(R.layout.activity_home)
            val navController = Navigation.findNavController(this, R.id.home_nav)
            val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navigation)
            bottomNavigationView.setupWithNavController(navController)
            fetchCurrentUser()
        }

Also attaching the Application class which is mandatory for every app using Hilt as per the documentation

@HiltAndroidApp
class CoreApplication:Application()

and the logcat of the crash

Caused by: java.lang.IllegalStateException: Hilt Activity must be attached to an @AndroidEntryPoint Application. Found: class androidx.multidex.MultiDexApplication
        at dagger.hilt.android.internal.managers.ActivityComponentManager.createComponent(ActivityComponentManager.java:82)
        at dagger.hilt.android.internal.managers.ActivityComponentManager.generatedComponent(ActivityComponentManager.java:65)
        at com.example.vcare.home.Hilt_HomeActivity.generatedComponent(Hilt_HomeActivity.java:43)
        at com.example.vcare.home.Hilt_HomeActivity.inject(Hilt_HomeActivity.java:62)
        at com.example.vcare.home.Hilt_HomeActivity.onCreate(Hilt_HomeActivity.java:37)
        at com.example.vcare.home.HomeActivity.onCreate(HomeActivity.kt:27)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
Theologize answered 22/9, 2020 at 8:43 Comment(6)
Have you declared your App in the AndroidManifest.xml? Please write "anroid:name =.CoreApplication" in your XML.Sherleysherline
@Sherleysherline works fine now,all thanks to youTheologize
Glad I could help, would be nice If you could accept the answer then.Sherleysherline
ofc man,thanks againTheologize
I have same issue on a multimodule architecture in the feature module when i use @androidEntryPoint on a feature Activity . Any solution?Misbeliever
Hi, I;m also facing the same issue over multimodule, app and presentation module app contains Application class with @HiltAndroidApp and presentation has Launhcer Main Activity, in manifest of app module mentioned Applicaiton, in presentation manifest added launcher but no application class is empty. how to do so?Thadeus
S
60

The solution to this problem was: Declare android:name = ".CoreApplication" in your AndroidManifest.xml file, in the <application .../>tag.

Sherleysherline answered 22/9, 2020 at 13:13 Comment(3)
Perfect SolutionMejias
The above code should be added to the <application tag in manifest.Mote
To add to this, make sure your test package name in your test manifest under src/androidTest differs from the one in your src/mainEnnius
B
6

Declare your Application class in manifest by adding the following:

   <application
        android:name=".CoreApplication"
        ....
   />
Boss answered 16/5, 2021 at 19:46 Comment(0)
E
6

In my case, I faced an error with implementing a repository into the ViewModel class.

I followed some fixes to resolve the errors.

  1. Adding entry point to activity class (AndroidEntryPoint)
  2. Adding entry point to application class (HiltAndroidApp)
  3. Adding name to manifest application android:name=".main.BaseApplication"
  4. Adding @Inject to repository class

Now issue with Hilt with MVVM and clean architecture are resolved!

Eliga answered 4/10, 2021 at 3:39 Comment(0)
B
3

For me the solution was slightly different: in my main module I only needed Hilt for the Application class, so I only added the hilt-android dependency:

implementation 'com.google.dagger:hilt-android:2.33-beta'

Which resulted in the above error message.

What I forgot was to also add the compile dependency to the app module:

kapt 'com.google.dagger:hilt-compiler:2.33-beta'

This solved the problem, as I was missing the generated class.

Baptism answered 1/3, 2021 at 16:59 Comment(0)
H
0

Added field inside Manifest Application tag like below

    <application
...
    android:name=".core.HiltApplication"
    >
    </application>
Howells answered 3/4, 2022 at 9:32 Comment(0)
F
0

I want to add @Merlin jaykumar answer i missed the below annotation on View model

@HiltViewModel

Example:-

@HiltViewModel
class AuthenticationViewModel @Inject constructor(
    private val authUseCases: AuthenticationUseCases
) : ViewModel() {
Franzen answered 16/9, 2023 at 16:1 Comment(0)
T
0

People who are working on Hilt with Multimodular architecture and still facing the issue here's the resolution

These were my modules

app, core (data, domain), presentation

dependency

app -> core, presentation
core -> data, domain
presentation -> core

Now I thought the domain already has hilt in gradle so I can just convert implementation into api so app module can still use hilt but I found out This was not working and giving me the above issue.

So This was my way of solving the issue

app>Manifest.xml

<application
        android:name=".Application"
        android:allowBackup="false" // Important
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        tools:targetApi="31" >
    </application>

app> build.gradle

plugins {
    ...
    id("kotlin-kapt")
    id("com.google.devtools.ksp")
    id("com.google.dagger.hilt.android")
}

dependencies {

    //hilt
    implementation("com.google.dagger:hilt-android:2.50")
    ksp("com.google.dagger:hilt-android-compiler:2.50")
    ksp("androidx.hilt:hilt-compiler:1.1.0")
    implementation("androidx.hilt:hilt-navigation-fragment:1.1.0")

}

app> Application.kt

@HiltAndroidApp
class Application : Application() {
}

top level build.gradle

plugins {
    id("com.google.dagger.hilt.android") version "2.50" apply false
    id("com.google.devtools.ksp") version "1.9.0-1.0.13" apply false
    }

Presentation > MainActivity.kt

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel: MyViewModel by viewModels()
    }

Presentation> MyViewModel

@HiltViewModel
class MyViewModel @Inject constructor(
    private val getDataUseCase: GetDataUseCase
) : ViewModel() {}

And inside

core>hiltModule.kt

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

    @Provides
    @Singleton
    fun providesMyRepository(): MyRepository {
        return MyRepositoryImpl()
    }

    @Provides
    @Singleton
    fun providesGetDataUseCase(myRepository: MyRepository): GetDataUseCase {
        return GetDataUseCase(myRepository)
    }
}
Thadeus answered 15/1 at 18:25 Comment(0)
S
0
<application
    android:name="package.YourApplicationClass"  <--------
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="xyz"
    android:screenOrientation="landscape"
    android:theme="@style/AppTheme"> 

I've added this declearation to manifest and solved my problem.

Shire answered 20/3 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.