Last active
May 14, 2020 01:26
-
-
Save gacordeiro/f02762cc9f83e3e96487135d47cd0c33 to your computer and use it in GitHub Desktop.
Dagger 2.11 initial setup example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dagger 2.11 initial setup example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile "com.google.dagger:dagger:2.11", | |
compile "com.google.dagger:dagger-android-support:2.11", | |
kapt "com.google.dagger:dagger-android-processor:2.11", | |
kapt "com.google.dagger:dagger-compiler:2.11", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module( | |
includes = { | |
ActivitiesBuilder.class, //this you have to create | |
AndroidInjectionModule.class, //this is imported on dependencies | |
... | |
} | |
) | |
public class CoreModule { ...} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
abstract class ActivitiesBuilder { | |
@ContributesAndroidInjector(modules = arrayOf(SomeFeatureModule::class)) | |
abstract fun someFeatureActivity(): SomeFeatureActivity | |
//For each new activity, just create one more module and put an abstract method here | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//If you are using MVP or any other architecture that needs a reverence to | |
//SomeFeatureActivity hidden behind an interface, you can do it in SomeFeatureModule | |
@Module | |
class SomeFeatureModule { | |
@Provides | |
fun provideView(view: SomeFeatureActivity): SomeFeatureView = view | |
@Provides | |
fun providePresenter(view: SomeFeatureView): SomeFeaturePresenter = SomeFeaturePresenter(view) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application implements HasActivityInjector { | |
@Inject DispatchingAndroidInjector<Activity> activityInjector; | |
protected CoreComponent coreComponent; | |
@Override public AndroidInjector<Activity> activityInjector() { | |
return activityInjector; | |
} | |
@Override public void onCreate() { | |
super.onCreate(); | |
coreComponent = DaggerCoreComponent.builder() | |
... //init any needed dependencies here | |
.build() | |
coreComponent.inject(this); //activityInjector has to be injected here by dagger | |
} | |
(...) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeFeatureActivity: AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) //this had to be called before super.onCreate | |
super.onCreate(savedInstanceState) | |
... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you need to inject fragments, you make your activity implement | |
HasFragmentInjector or HasSupportFragmentInjector similar to what you done on the application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment