How to access injected properties in attachBaseContext using Hilt? - android

For the sake of changing application's default Locale, I have to get access of my WrapContext class in attachBaseContext method inside the Activity:
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
#Inject lateinit var wrapper: WrapContext
.
.
.
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(wrapper.setLocale(newBase!!))
}
}
But as you can imagine I get nullPointerException because the field is injected after attachBaseContext is called.
Here is the WrapContext class:
#Singleton
class WrapContext #Inject constructor() {
fun setLocale(context: Context): Context {
return setLocale(context, language)
}
.
.
.
}
I also tried to inject WrapContext inside MyApp class so the field should be initialized when calling it inside Activity.
#HiltAndroidApp
class MyApp : Application() {
#Inject lateinit var wrapper: WrapContext
}
attachBaseContext inside activity:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext((applicationContext as MyApp).wrapper.setLocale(newBase!!))
}
But I still get the same error. I debugged the code and I found that applicationContext is Null in the method.
I searched online and I found the same issue someone had with dagger here. But there is no accepted answer which can maybe give me some sights to do it in hilt.
Is there a way to get this WrapContext class in the attachBaseContext method inside activity?

You can use an entry point to obtain dependencies from ApplicationComponent as soon as you have a Context attached to your application. Fortunately, such a context is passed into attachBaseContext:
#EntryPoint
#InstallIn(ApplicationComponent::class)
interface WrapperEntryPoint {
val wrapper: WrapContext
}
override fun attachBaseContext(newBase: Context) {
val wrapper = EntryPointAccessors.fromApplication(newBase, WrapperEntryPoint::class).wrapper
super.attachBaseContext(wrapper.setLocale(newBase))
}

Related

I couldn't understand this specific line while injecting Activity using Dagger - (applicationContext as MyApplication).appComponent.inject(this)

The Dagger Component
#Component
public interface ApplicationComponent {
void inject(LoginActivity loginActivity);
}
Application class
class MyApplication: Application() {
val appComponent = DaggerApplicationComponent.create()
}
Activity
class LoginActivity: Activity() {
#Inject lateinit var loginViewModel: LoginViewModel
}
override fun onCreate(savedInstanceState: Bundle?) {
(applicationContext as MyApplication).appComponent.inject(this)
super.onCreate(savedInstanceState)
}
In my activity's onCreate() , I'm casting my applicationContext to Application class and then accessing the variable inside it. Can someone please elaborate on how does it work and why the casting is needed.
Context is an abstract class with multiple implementations. Your component is in the MyApplication class and you have the following hierarchy:
Context
|
ContextWrapper
|
Application
|
MyApplication
As you can see here you get a Context:
#Override
public Context getApplicationContext() {
return mBase.getApplicationContext();
}
So you need to cast to have the implementation that you need with the corresponding accessible variable in your case: the component.
You should check the Context hierarchy:
https://developer.android.com/reference/android/content/Context
Also please check this article:
https://medium.com/#ali.muzaffar/which-context-should-i-use-in-android-e3133d00772c
What is 'Context' on Android?

Android Kotlin: Hilt how to get object of Application class?

Here i created application class dependency. now i want to access
#Module
#InstallIn(SingletonComponent::class)
class MyApplicationModule {
#Provides
fun providesMainApplicationInstance(application: MainApplication): MainApplication =
application
}
My application class is:
#HiltAndroidApp
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
}
fun printData(){
Log.d("Test","Awesome print data")
}
}
i want to call printData() function from the activity. i have used field injection to access application class but it gives error...
#Inject lateinit var mainApplication: MainApplication
ERROR
[Dagger/DependencyCycle] Found a dependency cycle:
MainApplication is injected at MyApplicationModule.providesMainApplicationInstance(application)
This should work:
#Module
#InstallIn(SingletonComponent::class)
class MyApplicationModule {
#Provides
fun providesMainApplicationInstance(#ApplicationContext context: Context): MainApplication {
return context as MainApplication
}
}
Hilt can inject application context. You need to simply cast it.
Let me know if it's good.
Hilt auto generate ApplicationContextModule.java, which provides both Context and Application.

Hilt field injection in the super Fragment or ViewModel

I'm using Dagger-Hilt for dependency injection in my Android project, now I have this situation where I have a base abstract Fragment
BaseViewModel.kt
abstract class BaseViewModel constructor(
val api: FakeApi,
) : ViewModel() {
//...
}
Here, I have a dependency which is FakeApi. What I'm trying to do is to inject the FakeApi into the BaseViewModel to be available in the BaseViewModel and all its children.
The first approach I tried is using the constructor injection and inject it to the child and pass it to the super using the constructor.
TaskViewModel.kt
#HiltViewModel
class TaskViewModel #Inject constructor(
api: FakeApi
) : BaseViewModel(api){
}
This approach works fine, but I don't need to pass the dependency from the child to the super class, I need the FakeApi to be automatically injected in the BaseViewModel without having to pass it as I have three levels of abstraction (There is another class inheriting from the TaskViewModel) So I have to pass it two times.
The second approach was to use the field injection as follows
BaseViewModel.kt
abstract class BaseViewModel: ViewModel() {
#Inject
lateinit var api: FakeApi
//...
}
TaskViewModel.kt
#HiltViewModel
class TaskViewModel #Inject constructor(): BaseViewModel() {
}
This approach didn't work for me and the FakeApi wasn't injected and I've got an Exception
kotlin.UninitializedPropertyAccessException: lateinit property api has not been initialized
My questions are
Why field injection doesn't work for me?
Is there any way to use constructor injection for the super class instead of passing the dependency from the child?
Thanks to this Github Issue I figured out that the problem is that you can't use the field injected properties during the ViewModel constructor initialization, but you still use it after the constructor -including all the properties direct initialization- has been initialized.
Dagger firstly completes the constructor injection process then the field injection process takes place. that's why you can't use the field injection before the constructor injection is completed.
❌ Wrong use
abstract class BaseViewModel : ViewModel() {
#Inject
protected lateinit var fakeApi: FakeApi
val temp = fakeApi.doSomething() // Don't use it in direct property declaration
init {
fakeApi.doSomething() // Don't use it in the init block
}
}
✔️ Right use
abstract class BaseViewModel : ViewModel() {
#Inject
protected lateinit var fakeApi: FakeApi
val temp: Any
get() = fakeApi.doSomething() // Use property getter
fun doSomething(){
fakeApi.doSomething() // Use it after constructor initialization
}
}
Or you can use the by lazy to declare your properties.
I tested and I see that field injection in base class still work with Hilt 2.35. I can not get the error like you so maybe you can try to change the Hilt version or check how you provide FakeApi
abstract class BaseViewModel : ViewModel() {
#Inject
protected lateinit var fakeApi: FakeApi
}
FakeApi
// Inject constructor also working
class FakeApi {
fun doSomeThing() {
Log.i("TAG", "do something")
}
}
MainViewModel
#HiltViewModel
class MainViewModel #Inject constructor() : BaseViewModel() {
// from activity, when I call this function, the logcat print normally
fun doSomeThing() {
fakeApi.doSomeThing()
}
}
AppModule
#Module
#InstallIn(SingletonComponent::class)
class AppModule {
#Provides
fun provideAPI(
): FakeApi {
return FakeApi()
}
}
https://github.com/PhanVanLinh/AndroidHiltInjectInBaseClass
After many searches on the Internet, I think the best solution is to not use initializer blocks init { ... } on the ViewModel, and instead create a function fun initialize() { ... } that will be called on the Fragment.
BaseViewModel.kt
#HiltViewModel
open class BaseViewModel #Inject constructor() : ViewModel() {
#Inject
protected lateinit var localUserRepository: LocalUserRepository
}
OnboardingViewModel.kt
#HiltViewModel
class OnboardingViewModel #Inject constructor() : BaseViewModel() {
// Warning: don't use "init {}", the app will crash because of BaseViewModel
// injected properties not initialized
fun initialize() {
if (localUserRepository.isLoggedIn()) {
navigateToHomeScreen()
}
}
}
OnBoardingFragment.kt
#AndroidEntryPoint
class OnBoardingFragment() {
override val viewModel: OnboardingViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.initialize()
}
}
Sources:
https://github.com/google/dagger/issues/2507
the answers on this question

Dagger-2 "lateinit property application has not been initialized"

I am trying to inject application context in a class which is giving
”lateinit property application has not been initialized"
exception.
CoreModule.kt
#Module
open class CoreModule {
#Singleton
#Provides
fun provideRealmHelper(): RealmHelper {
return RealmHelper()
}
}
MyApplication.kt
open class MyApplication : MultiDexApplication(), HasActivityInjector {
#Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
val log = LoggerFactory.getLogger(this.javaClass)!!
companion object {
var application: MyApplication? = null
fun getInstance(): MyApplication {
return application!!
}
}
override fun onCreate() {
try {
super.onCreate()
application = this
DaggerAppComponent.builder().application(this).build().inject(this)
} catch (e: Exception) {
log.error("Exception in Application", e)
Thread.setDefaultUncaughtExceptionHandler(GlobalExceptionHandler())
}
}
override fun activityInjector() = dispatchingAndroidInjector
}
AppComponent.kt
#Singleton
#Component(modules = [AndroidSupportInjectionModule::class,CoreModule::class])
interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance
fun application(application: MyApplication): Builder
fun build(): AppComponent
}
fun inject(myApplication: MyApplication)
fun inject(realmHelper: RealmHelper)
}
//I need application context in this class. I am inject applicationContext here.
Is injecting is correct way to do or I should use constructor injection?
RealmHelper.kt
class RealmHelper #Inject constructor() {
//need application context here but getting "lateinit property application has not been initialized
#Inject
lateinit var application: MyApplication
init {
Realm.init(application) // null application
}
}
Note: MyApplication is added to AndoridManifest.xml
The problem is you annotated your field but not injected. You can inject field like you did in application class : DaggerAppComponent.builder().application(this).build().inject(this)
or you can move your application field to RealmHelper constructor and in core module you need to write a provide function to return application. If you want to see an example I have an applicaton. https://github.com/volkansahin45/Moneycim
I need application context in this class. I am inject
applicationContext here. Is injecting is correct way to do or I should
use constructor injection?
Always favor constructor injection over field injection if possible.
Your CoreModule is not needed. The code below is enough.
#Singleton
class RealmHelper #Inject constructor(private val application: MyApplication) {
//Your implementation
}
fun inject(realmHelper: RealmHelper) in your Component is also unnecessary.
Removing those lines should fix it, I quickly threw together a demo project just to test it to make sure. Here is a quick gist with the code.
this might be too late but this may help other developer..
#set:Inject
internal var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>? = null
use this instead of
#Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
and in case you get same error elsewhere, do the same. Use #set:Inject instead of #Inject and use internal instead of lateinit.
This worked for me like charm.

How to inject a LifecycleOwner in Android using Dagger2?

I happen to have an Android lifecycle aware component with the following interface:
class MyLifecycleAwareComponent #Inject constructor(
private val: DependencyOne,
private val: DependencyTwo
) {
fun bindToLifecycleOwner(lifecycleOwner: LifecycleOwner) {
...
}
...
}
All Dagger specific components and modules are configured correctly and have been working great so far.
In each activity when I need to use the component I do the following:
class MyActivity: AppCompatActivity() {
#Inject
lateinit var component: MyLifecycleAwareComponent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
component.bindToLifecycleOwner(this)
...
}
}
Now I want to get rid of bindLifecycleOwner and denote my component like this:
class MyLifecycleAwareComponent #Inject constructor(
private val: DependencyOne,
private val: DependencyTwo,
private val: LifecycleOwner
) {
...
}
And provide the lifecycleOwner within the scope of individual activities (which implement the interface by extending AppCompatActivity).
Is there any way to do it with Dagger?
You may bind your Activity to LifecycleOwner from your ActivityModule:
#Module
abstract class ActivityModule {
...
#Binds
#ActivityScope
abstract fun bindLifecycleOwner(activity: AppCompatActivity): LifecycleOwner
...
}

Categories

Resources