Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sources for Dagger 1 -> 2 migration process
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}
public class App extends Application {
private AppComponent component;
@Inject
AnalyticsManager analyticsManager;
@Override
public void onCreate() {
super.onCreate();
setupGraph();
analyticsManager.registerAppEnter();
}
private void setupGraph() {
component = Dagger_AppComponent.builder()
.appModule(new AppModule(this))
.build();
component.inject(this);
}
public AppComponent component() {
return component;
}
public static App get(Context context) {
return (App) context.getApplicationContext();
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.dagger:dagger:2.0-SNAPSHOT'
apt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
@Singleton
@Component(
modules = {
AppModule.class,
DomainModule.class,
InteractorsModule.class
}
)
public interface AppComponent {
void inject(App app);
AnalyticsManager getAnalyticsManager();
LoginInteractor getLoginInteractor();
FindItemsInteractor getFindItemsInteractor();
}
@Module(
injects = {
App.class
},
includes = {
DomainModule.class,
InteractorsModule.class
}
)
public class AppModule {
private App app;
public AppModule(App app) {
this.app = app;
}
@Provides public Application provideApplication() {
return app;
}
}
@Module
public class AppModule {
private App app;
public AppModule(App app) {
this.app = app;
}
@Provides public Application provideApplication() {
return app;
}
}
buildscript {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
allprojects {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
@ActivityScope
@Component(
dependencies = AppComponent.class,
modules = LoginModule.class
)
public interface LoginComponent {
void inject(LoginActivity activity);
LoginPresenter getLoginPresenter();
}
@ActivityScope
@Component(
dependencies = AppComponent.class,
modules = MainModule.class
)
public interface MainComponent {
void inject(MainActivity activity);
MainPresenter getLoginPresenter();
}
protected void setupComponent(AppComponent appComponent) {
Dagger_MainComponent.builder()
.appComponent(appComponent)
.mainModule(new MainModule(this))
.build()
.inject(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.