dagger-2

Dagger 2 Inject Context in Kotlin Object

岁酱吖の 提交于 2020-01-16 07:49:08
问题 I am trying to inject Context using Dagger 2. AppComponent.kt: @Singleton @Component( modules = [ AppModule::class ] ) interface AppComponent { fun context(): Context } AppModule.kt: @Module class AppModule(private val application: Application) { @Provides @Singleton fun providesApplicationContext(): Context = application } MainApp.kt: class MainApp : Application() { lateinit var appComponent: AppComponent override fun onCreate() { super.onCreate() appComponent = initDagger() } private fun

Share a Dagger 2 component between two different flavors

我怕爱的太早我们不能终老 提交于 2020-01-16 01:04:28
问题 I have two flavors: pro and free. In each of these, I have a MainActivity, but I want the MainComponent and MainModule to be the same for both, so MainComponent and MainModule are both in src/java/main, but I get an error in my MainComponent due to not having an import of MainActivity of both flavors. Here, let me make it clear: Here's what the MainComponent in src/java/main looks like: import com.xxx.myapp.di.modules.MainModule; import com.xxx.myapp.free.MainActivity; import com.xxx.myapp

Internal dependency injection using Dagger2

本小妞迷上赌 提交于 2020-01-15 12:45:30
问题 I want to use Dagger2. Say I have the following dependencies: Class A depends on class B Class B depends on class C I tried to create a Module that provides B and C, and a Component that provides A, however only B is injected into A, and the reference to C in B remains null. What is the classes structure I need to implement using dagger? 回答1: You can either use constructor injection or field injection; and either constructor-inject or module-inject. Constructor-@Inject might be buggy, because

Internal dependency injection using Dagger2

瘦欲@ 提交于 2020-01-15 12:45:29
问题 I want to use Dagger2. Say I have the following dependencies: Class A depends on class B Class B depends on class C I tried to create a Module that provides B and C, and a Component that provides A, however only B is injected into A, and the reference to C in B remains null. What is the classes structure I need to implement using dagger? 回答1: You can either use constructor injection or field injection; and either constructor-inject or module-inject. Constructor-@Inject might be buggy, because

How to inject activity into another class using dagger.android?

坚强是说给别人听的谎言 提交于 2020-01-15 08:13:31
问题 I'm using the new dagger.android method to inject activities, but I wonder what to do if you want to inject Activity to a class? Maybe a Navigator class which needs the current activity: package com.abydos.messenger.ui import android.app.Activity import com.abydos.messenger.ui.register.RegisterActivity import org.jetbrains.anko.startActivity import javax.inject.Inject class Navigator @Inject constructor(private val activity: Activity) { fun register() { activity.startActivity<RegisterActivity

Inject property into ViewModel using Dagger 2

孤者浪人 提交于 2020-01-15 05:55:50
问题 I try to learn how to use Dagger 2. Please help with follow exception: Exception: UninitializedPropertyAccessException: lateinit property trips has not been initialized MainActivityViewModel: class MainActivityViewModel : ViewModel() { private lateinit var tripsLiveData: MutableLiveData<List<Trip>> @Inject lateinit var trips : List<Trip> fun getTrips() : LiveData<List<Trip>> { if (!::tripsLiveData.isInitialized){ tripsLiveData = MutableLiveData() tripsLiveData.value = trips } return

Dagger 2 Subcomponents for Encapsulation

不想你离开。 提交于 2020-01-15 05:33:11
问题 How do I add a Subcomponent to a Module with an argument constructor? Adding code here in addition to providing a github link: ExampleApplication.java public class ExampleApplication extends DaggerApplication { @Inject Database database; @Override public void onCreate() { super.onCreate(); Timber.plant(new Timber.DebugTree()); Timber.i(database.name()); } @Override protected AndroidInjector<? extends DaggerApplication> applicationInjector() { return DaggerApplicationComponent .builder()

How to implement Dagger for worker classes in Dagger 2.16 ?('android.arch.work:work-runtime')

怎甘沉沦 提交于 2020-01-14 04:36:41
问题 How to implement Dagger for worker classes in Dagger 2.16? public class SyncWorker extends Worker { @Inject ISettingRepository settingRepository; @NonNull @Override public Result doWork() { sync(); return Result.SUCCESS; } private void sync() { } } my AppComponent @Singleton @Component(modules = { AndroidSupportInjectionModule.class, BaseModule.class, ApiModule.class, UserDatabaseModule.class, SaleDatabaseModule.class, AppModule.class, ActivityBuilderModule.class }) public interface

Dagger 2: how to change provided dependencies at runtime

回眸只為那壹抹淺笑 提交于 2020-01-13 07:49:33
问题 In order to learn Dagger 2 i decided to rewrite my application but I'm stuck with finding the proper solution for the following problem. For the purpose of this example let's assume we have an interface called Mode : public interface Mode { Object1 obj1(); //some other methods providing objects for app } and two implementations: NormalMode and DemoMode . Mode is stored in singleton so it could be accessed from anywhere within application. public enum ModeManager { INSTANCE,; private Mode mode

PowerMock + Robolectric + Dagger2. Part I

荒凉一梦 提交于 2020-01-13 05:14:53
问题 This question was created from first part of PowerMock + Robolectric + Dagger2 So i'm a little bit again. Sorry. I test custom view class which contain: android ui elements some logic static methods callings dagger2 dependencies So i use next tools for testing Robolectric for UI elements mocking unit tests for logic testing PowerMock for static methods mocking Robolectric + PowerMock integration problem is known and solution is known - https://github.com/robolectric/robolectric/wiki/Using