Skip to content

Instantly share code, notes, and snippets.

@sarkerrabi
Last active March 8, 2021 11:14
Show Gist options
  • Save sarkerrabi/5d1f31435b6fb5d1cdef7db86a8b0001 to your computer and use it in GitHub Desktop.
Save sarkerrabi/5d1f31435b6fb5d1cdef7db86a8b0001 to your computer and use it in GitHub Desktop.
#Dagger2 Links
https://github.com/google/dagger
#installers
def dagger_version = 2.29
api "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
api "com.google.dagger:dagger-android:$dagger_version"
api "com.google.dagger:dagger-android-support:$dagger_version" // if you use the support libraries
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
#1st
need to create a BaseApplication.class
```
public class BaseApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent
.builder()
.application(this)
.build();
}
```
#2nd
need to create an interface AppComponent.java
```
@Component(
modules = {
AndroidSupportInjectionModule.class
}
)
public interface AppComponent extends AndroidInjector<BaseApplication> {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
}
```
#3rd
Need to create AppModule
for accessing objects in everywhere in the application.
Example: Glide
```
@Module
public class AppModule {
@Provides
static RequestOptions provideRequestOptions() {
return RequestOptions
.placeholderOf(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background);
}
@Provides
static RequestManager provideGlideInstance(Application application, RequestOptions requestOptions) {
return Glide.with(application)
.setDefaultRequestOptions(requestOptions);
}
}
```
From activity
just Inject and use
```
@Inject
RequestManager requestManager;
public void setLogo(){
requestManager
.load(R.drawable.loader)
.into((ImageView)findViewById(R.id.login_logo));
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment