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
AbemaTVを支えるアプリの優しさ / abematv_devcon_2017
ogaclejapan
3
6.4k
Architecture Components - Lifecycle library
ogaclejapan
2
2.2k
How to keep data between orientation changes
ogaclejapan
7
2.4k
Your app name
ogaclejapan
1
2.8k
Flux de Relax :)
ogaclejapan
13
61k
Other Decks in Programming
See All in Programming
Remix on Hono on Cloudflare Workers
yusukebe
1
300
みんなでプロポーザルを書いてみた
yuriko1211
0
280
イベント駆動で成長して委員会
happymana
1
330
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
CSC509 Lecture 12
javiergs
PRO
0
160
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Click-free releases & the making of a CLI app
oheyadam
2
120
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Featured
See All Featured
What's new in Ruby 2.0
geeforr
343
31k
Writing Fast Ruby
sferik
627
61k
Facilitating Awesome Meetings
lara
50
6.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Optimizing for Happiness
mojombo
376
70k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Scaling GitHub
holman
458
140k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
900
A Philosophy of Restraint
colly
203
16k
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