Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dagger2 has been released!
Search
Masaki Ogata
April 15, 2015
Programming
3
3.2k
Dagger2 has been released!
Migrated story from Dagger1 to Dagger2 in Qiitanium App
Masaki Ogata
April 15, 2015
Tweet
Share
More Decks by Masaki Ogata
See All by Masaki Ogata
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
170
AbemaTVを支えるアプリの優しさ / abematv_devcon_2017
ogaclejapan
3
6.4k
Architecture Components - Lifecycle library
ogaclejapan
3
2.3k
How to keep data between orientation changes
ogaclejapan
7
2.5k
Your app name
ogaclejapan
1
2.8k
Flux de Relax :)
ogaclejapan
13
61k
Other Decks in Programming
See All in Programming
Recoilを剥がしている話
kirik
5
6.8k
Haze - Real time background blurring
chrisbanes
1
510
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
280
testcontainers のススメ
sgash708
1
120
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
140
Beyond ORM
77web
7
900
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
150
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
500
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
190
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Building Adaptive Systems
keathley
38
2.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Navigating Team Friction
lara
183
15k
For a Future-Friendly Web
brad_frost
175
9.4k
4 Signs Your Business is Dying
shpigford
181
21k
How GitHub (no longer) Works
holman
311
140k
Docker and Python
trallard
42
3.1k
Making the Leap to Tech Lead
cromwellryan
133
9k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Transcript
Dagger 2.0 has been released! at potatotips #16
About me Job $> Android Engineer v0.1 GitHub $> https://github.com/ogaclejapan
Company $> CyberAgent, Inc. Name $> Masaki Ogata (@ogaclejapan)
Qiitanium
http://github.com/ogaclejapan/qiitanium ɾRxJava ɾClean Architecture + MVVMʢࣗশʣ ɾDagger ɾOkHttp
http://github.com/ogaclejapan/qiitanium ɾ ɾ ɾDagger ɾ
http://github.com/ogaclejapan/qiitanium ɾ ɾ ɾDagger 1.x ɾ Dagger 2.0 Migrated!
About Dagger
About Dagger Let me ask Wasabeef !! https://speakerdeck.com/wasabeef/dagger at potatotips
#9
Dagger2.0 Overview
100% Proguard friendly @Generated("dagger.internal.codegen.ComponentProcessor") public final class AppModule_ProvideApplicationFactory implements Factory<Application>
{ private final AppModule module; @Override public Application get() { Application provided = module.provideApplication(); if (provided == null) { throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method"); } return provided; } }
100% Proguard friendly ɾ θϩϦϑϨΫγϣϯ ɾ ࣗಈੜίʔυσόοάτϨʔε͕Մೳ ɾύϑΥʔϚϯε্͕
@Component Dagger1: FeatureModule ObjectGraph SubModule1 SubModule2 SubModule3 load Includes AppModule
plus SubGraph
@Component Dagger1: @Module(includes={Module1.class, …}, injects={…}) public class AppModule { @Provided
public Hoge provideHoge() { return new Hoge(); } } ObjectGraph graph = ObjectGraph.create(new Object[] { new AppModule()}); graph.inject(this); ObjectGraph sub = graph.plus(new FeatureModule());
@Component Dagger2: AppComponent FeatureComponent Module1 Module2 Module3 modules FeatureModule AppModule
modules dependencies
@Component Dagger2: @Component(modules={ AppModule.class, …}) public interface AppComponent { public
void inject(Application app); } @Generated(“…”) public class DaggerAppComponent { //… } Annotation Processor AppComponent component = DaggerAppComponent.builder() .appModule(…).build(); component.inject(this);
Migrate Dagger 1.x to 2.0 (ͦͷұ)
Scope @Module public class Module1 { @Singleton @Provided public Hoge
provideHoge() { return new Hoge(); } } @Component(modules = { Module1.class }) public interface AppComponent { //… }
Error: AppComponent (unscoped) may not reference scoped bindings: @Singleton @Provides
Hoge Compile Error…
Fix @Module public class @Provided public return new } }
@Component(modules = { public interface AppComponent { //… } @Singleton
Custom Scope Component୯ҐͰΠϯελϯεΛཧ͍ͯ͠Δ @Generated(“…”) public final class DaggerAppComponent { //…
private void initialize(final Builder builder) { this.…Provider = ScopedProvider.create(…); } //… } ScopedProviderͰSingleton੍ޚ͕͞Ε͍ͯΔ
Custom Scope @Scope @Retention(RetentionPolicy.RUNTIME) public @interface PerActivity { } @Module(modules={…})
public class ActivityModule { @PerActivity @Provided public Hoge provideHoge() { return new Hoge(); } } Dagger2ͰScopeΛಠࣗʹఆٛͯ͠͏
Migrate Dagger 1.x to 2.0 (ͦͷ್)
Override Module Dagger1: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
{ @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
{ @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
extends AppModule { @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule
extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { AppModule appModule() { return new DebugAppModule(); } }
Override Module Dagger2: src/main/java/.. public class MyApplication extends Application {
@Inject Hoge mHoge; @Override public void onCreate() { AppComponent component = DaggerAppComponent.builder() .appModule(Modules.appModule()) .build(); component.inject(this); System.out.println(mHoge instanceof DebugHoge); //true } }
Let’s enjoy Dagger2 !!
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ m(_ _)m