'Inject context with Hilt: this field leaks a context object
I'm using Hilt to inject context and other dependencies into my HomeViewModel class; Everything is working properly but I'm getting this warning. How can I prevent from leakings?
This is my HomeFragment (where I inject and use the HomeViewModel class):
@AndroidEntryPoint
class HomeFragment : Fragment() {
private val viewModel: HomeViewModel by viewModels()
....
}
This is the warning:
class HomeViewModel @ViewModelInject constructor(
@ApplicationContext val context: Context,
private val locationAPI: LocationAPI,
private val imagesAPI: ImagesAPI
) :
ViewModel() {
...
}
I'm using:
//Hilt DI
implementation "com.google.dagger:hilt-android:2.30.1-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.30.1-alpha"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02"
Thanks!
-- Edited, as suggested, after the first given answer:
The Home Fragment now is:
@HiltViewModel
class DetailsViewModel @Inject constructor(
@ApplicationContext val context: Context,
private val locationDetailsAPI: LocationAPI) :
ViewModel() {
...
}
Dependencies updated to:
//Hilt DI
implementation "com.google.dagger:hilt-android:2.31-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.30.1-alpha"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha03"
And I'm still getting this leaking error.
Any ideias?
Solution 1:[1]
I don't get this warning, and I inject a context the same way.
Try updating to 2.31.2-alpha for hilt and 1.0.0-alpha03 for hilt-androidx
There are a few breaking changes. You will need to annotate your view models with @HiltViewModel, use @Inject instead of @ViewModelInject. And you will need to replace any references to ApplicationComponent with SingletonComponent.
Solution 2:[2]
After I faced that warning
I decided to profile memory to be guaranteed that the approach causes a memory leak, but what I found is quite interesting
yeah, there is no leak it's just a warning so don't care about it anymore, happy coding ;)
Solution 3:[3]
Your Kotlin plugin version is 1.4.30-release-Studio4.1-1?
If so, downgrade kotlin plugin version.
In 1.4.30-release-Studio4.1-1 version,
if viewmodel has context as instance variable, it causes warning.
Solution 4:[4]
Inject application instead:
@Inject constructor(private val application: Application)
Then you can get the app context like this:
application.applicationContext
Solution 5:[5]
I had the same problem after updating Hilt dependencies.
It's right to use @HiltViewModel
with @Inject
before the constructor.
Regarding the leak due to the Context, just remove it from the ViewModel constructor, and pass the Context as method parameter of your functions, like this:
@HiltViewModel
class HomeViewModel @Inject constructor(
private val locationDetailsAPI: LocationAPI,
private val imagesAPI: ImagesAPI
) : ViewModel() {
fun getImages(context: Context) {
// do something
}
}
Solution 6:[6]
it should be safe here, There isn't really a leak here in the context constructor, it is just the lint check doesn't know that is the application context, and not some other context. and I think I think it'd be safe to suppress that warning too. to inspect the memory memory usage with Memory Profiler, read this documentation: https://developer.android.com/studio/profile/memory-profiler
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mikeBlack |
Solution 2 | |
Solution 3 | |
Solution 4 | |
Solution 5 | |
Solution 6 | Amado |