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
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Arm移行タイムアタック
qnighy
0
300
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
最新TCAキャッチアップ
0si43
0
140
ヤプリ新卒SREの オンボーディング
masaki12
0
130
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Designing for Performance
lara
604
68k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Teambox: Starting and Learning
jrom
133
8.8k
Agile that works and the tools we love
rasmusluckow
327
21k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Thoughts on Productivity
jonyablonski
67
4.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
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