Hilt: Cannot start service outside of application
See original GitHub issueKotlin version: 1.3.72 Hilt (android, compiler, and plugin) version: 2.28.3-alpha
I’m trying to run a service in the background. I am using an application just so I can leverage Hilt for DI, but it will have no UI.
When I tried to run:
adb shell am startservice test.testflight/.TestFlightService
I got:
Starting service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=test.testflight/.TestFlightService }
But my application crashed with:
07-30 23:41:01.169 3359-3359/test.testflight E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.testflight, PID: 3359
java.lang.RuntimeException: Unable to create service test.testflight.TestFlightService: java.lang.IllegalStateException: Hilt service must be attached to an @AndroidEntryPoint Application. Found: class android.app.Application
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2887)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Hilt service must be attached to an @AndroidEntryPoint Application. Found: class android.app.Application
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.managers.ServiceComponentManager.createComponent(ServiceComponentManager.java:65)
at dagger.hilt.android.internal.managers.ServiceComponentManager.generatedComponent(ServiceComponentManager.java:58)
at test.testflight.Hilt_TestFlightService.generatedComponent(Hilt_TestFlightService.java:54)
at test.testflight.Hilt_TestFlightService.inject(Hilt_TestFlightService.java:49)
at test.testflight.Hilt_TestFlightService.onCreate(Hilt_TestFlightService.java:28)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
My application class looks like:
package test.testflight
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
/**
* This is only used for Hilt graph creation. This will run as a service in the background without a UI.
*/
@HiltAndroidApp
class TestFlight : Application() {}
My service class looks like:
package tests.testflight
import android.app.Service
import android.content.Intent
import android.os.IBinder
import dagger.hilt.android.AndroidEntryPoint
import sharedfunctions.logging.LogManager
@AndroidEntryPoint
class TestFlightService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// always try to restart the service IF its killed by the OS
// (we should not run into this but just in case)
LogManager.d(TAG, "Test Flight Service started")
return START_STICKY
}
override fun onBind(intent: Intent): IBinder? {
return null
}
companion object {
private const val TAG = "TestFlightService"
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
android - Hilt integration crashes application ... - Stack Overflow
It is really silly solution but just comment out the injection annotation and injected fields, run the app then uncomment them and run...
Read more >Dependency injection with Hilt | Android Developers
Overview · Sending the user to another app · Getting a result from an activity · Allowing other apps to start your activity....
Read more >The practical guide – Part 4: Dependency injection with Hilt
How to fix this? One thing you can think of is by creating some class where you handle all the dependencies there (Like...
Read more >A Standard Way to Implement Dependency Injection in Android
Hilt Application is mandatory — achieved by adding the@HiltAndroidApp annotation into your Application class. 1 2 3 4, @HiltAndroidApp class HiltSampleApp : ...
Read more >All about Hilt. Get deeper understanding about Hilt.
Hilt is a dependency injection framework for Android that is built on top of ... such as Application, Activity, Fragment, Service, and View....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry, I’m not really sure. I think the first thing to figure out is why Android is not using your application class, which shouldn’t be related to Hilt. You might have luck asking on StackOverflow to see if anyone has run into similar issues.
I’m going to close this for now, but if you find more evidence that suggests this is related to Hilt we can reopen it.
@bcorso I pulled a bug dumb…I accidentally removed the
android:name
from theAndroidManifest
file without realizing it… -_-