I hope to dependency injection MediaRecorder
object with Hilt in Android Studio.
But the Code A get the error 'lateinit' modifier is not allowed on properties of nullable types .
And the Code B get the error Dagger does not support injection into private fields .
The Code C can be compiled, but it's not what I need. I need to define mRecorder
as nullable, so I can use mRecorder = null
to release it.
How can I fix the Code A?
Code A
@HiltViewModel
class SoundViewModel @Inject constructor(): ViewModel()
{
@Inject lateinit var mRecorder: MediaRecorder?
}
@InstallIn(SingletonComponent::class)
@Module
object ObjectModule {
@Provides
@Singleton
fun provideMediaRecorder(): MediaRecorder {
return MediaRecorder()
}
}
Code B
@HiltViewModel
class SoundViewModel @Inject constructor(): ViewModel()
{
@Inject var mRecorder: MediaRecorder? =null
}
...
Code C
@HiltViewModel
class SoundViewModel @Inject constructor(): ViewModel()
{
@Inject lateinit var mRecorder: MediaRecorder
}
...