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
簡単高速なDIライブラリToothpick
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
zaki50
January 26, 2018
Technology
240
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
簡単高速なDIライブラリToothpick
zaki50
January 26, 2018
More Decks by zaki50
See All by zaki50
I/O Extended 2018 @Tokyo Android1
zaki50
3
1.3k
Android Studio 3.0 profilerハンズオン
zaki50
2
800
Realm Japan meetup #23_Java
zaki50
0
760
Realm Japan meetup #22_Java
zaki50
1
580
Realm World Tour Osaka Recent Java Updates
zaki50
0
500
Realm World Tour - ErrorProne in Realm Java
zaki50
2
500
Realm World Tour Tokyo Recent Java Updates
zaki50
2
810
Realm World Tour - ErrorProne in Realm Java
zaki50
1
790
オフラインファーストなアプリケーション開発
zaki50
1
2.7k
Other Decks in Technology
See All in Technology
変化の早いClaude Codeを 書籍に落とし込む
oikon48
7
1.3k
바이브풀 라이프
arawn
0
100
[ChatGPT Work LT]事務作業が苦手な人のための バックオフィスの「半」自動化
chimaki_iot
0
250
ガバメント AI 源内を地方自治体は活用できるのか可能性と課題、期待について
takeda_h
1
360
Breaking the Seal: Static Deobfuscation of Compiled V8 JavaScript Bytecode Malware
hshrzd
0
660
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
12
8.4k
20260608_Codexの可能性_ノンプログラマー向け_大城追記
doradora09
PRO
0
780
つくって納得、つかって実感! 大規模言語モデルことはじめ ver2.0
recruitengineers
PRO
3
1.2k
Goでデータパイプラインを作ろう
sansantech
PRO
0
360
AIは実装を速くする。では、私たちは何を今作るべきか?-立場を越えてリリースに向き合ったチーム開発の実践 / 20260801 Hiromi Nakaya and Naoki Takahashi
shift_evolve
PRO
3
450
オートロックマンションなのに、各部屋は施錠なし!? 攻撃者が組織内ネットワークで大暴れする理由 / The Front Door Is Locked, but the Rooms Are Wide Open: Why Attackers Move Freely Inside Enterprise Networks
nttcom
1
1.6k
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
67k
Designing for Timeless Needs
cassininazir
1
430
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
230
Claude Code のすすめ
schroneko
67
230k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Transcript
؆୯ߴͳDIϥΠϒϥϦToothpick DroidKaigi Prelude YAMAZAKI Makoto (@zaki50)
Toothpick https://github.com/stephanenicolas/toothpick
ಛ • γϯϓϧͰ؆୯ • ͪΐͬͱ͚ͩྼΔ͚Ͳ΄΅ಉ͡ • ͍͔ͭ͘ͷ༻සͷ͍ػೳυϩοϓ Dagger2ͱ͘Βͯ https://github.com/stephanenicolas/toothpick/wiki/Benchmark
͍ํ • ToothpickΛґଘϥΠϒϥϦͱͯ͠Ճ • ϞδϡʔϧΛఆٛ • είʔϓΛఆٛ • είʔϓͰ༻͢ΔϞδϡʔϧΛࢦఆ •
ೖ
ϥΠϒϥϦՃ app/build.gradle: dependencies { implementation 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.1.1' kapt 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.1' }
Ϟδϡʔϧఆٛ ApplicationModule.kt class ApplicationModule(private val application: Application) : Module() {
init { bind() } private fun bind() { val repository = ModelRepository(application) bind(ModelRepository::class.java).toInstance(repository) bind(Foo::class.java).to(FooImpl::class.java) bind(Bar::class.java).toProvider(BarProvider::class.java) } }
࣮Ϋϥε FooImpl.kt class FooImpl @Inject constructor() : Foo { init
{ ... } } BarProvider.kt class BarProvider : Provider<Bar> { override fun get(): Bar = BarImpl() }
είʔϓ
ɹείʔϓͷ࡞Γํ MainActivity.kt class MyApplication : Application() { override fun onCreate()
{ super.onCreate() val scope = Toothpick.openScope(this) scope.installModules(ApplicationModule(this)) } override fun onTerminate() { super.onTerminate() Toothpick.closeScope(this) } }
ɹωετͨ͠είʔϓ MainActivity.kt class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) val scope = Toothpick.openScopes(application, this) scope.installModules(MainActivityModule(this)) } override fun onDestroy() { super.onDestroy() Toothpick.closeScope(this) } }
ɹগ͠ݡ͍είʔϓ MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var
activityId: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... activityId = getActivityId(savedInstanceState) scope = Toothpick.openScopes(application, activityId) } private fun getActivityId(savedInstanceState: Bundle?): String { return savedInstanceState?.getString("activity_id") ?: UUID.randomUUID().toString() } }
ɹগ͠ݡ͍είʔϓ(ଓ͖) MainActivity.kt class MainActivity : AppCompatActivity() { ... override fun
onSaveInstanceState(outState: Bundle?) { super.onSaveInstanceState(outState) outState?.putString("activity_id", activityId) } override fun onDestroy() { super.onDestroy() if (isFinishing) { Toothpick.closeScope(activityId) } } }
Ϟδϡʔϧ༻ MyApplication.kt class MyApplication : Application() { override fun onCreate()
{ super.onCreate() val scope = Toothpick.openScope(this) scope.installModules(ApplicationModule(this)) } override fun onTerminate() { super.onTerminate() Toothpick.closeScope(this) } }
ೖ MainActivity.kt class MainActivity : AppCompatActivity() { @Inject lateinit var
repo: ModelRepository @Inject lateinit var foo: Foo override fun onCreate() { super.onCreate() val scope = Toothpick.openScopes(application, this) scope.installModules(ApplicationModule(this)) Toothpick.inject(this, scope) } }
γϯάϧτϯ BarImpl.kt @Singleton class BarImpl @Inject constructor() : Bar {
init { ... } }
Android༻ศརϞδϡʔϧ app/build.gradle: dependencies { implementation 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.1.1' kapt 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.1' implementation 'com.github.stephanenicolas.toothpick:smoothie:1.1.1'
} val scope = Toothpick.openScope(this) scope.installModules(SmoothieApplicationModule(this)) scope.installModules(ApplicationModule(this))
ϦϑϨΫγϣϯͷഉআ app/build.gradle: kapt { generateStubs = true arguments { arg('toothpick_registry_package_name',
"${android.defaultConfig.applicationId}.toothpick") } }
ϦϑϨΫγϣϯͷഉআ MyApplication.kt ͷ onCreateͰ Toothpick.setConfiguration( Configuration.forProduction().disableReflection()) MemberInjectorRegistryLocator.setRootRegistry( com.example.toothpick.MemberInjectorRegistry()) FactoryRegistryLocator.setRootRegistry( com.example.toothpick.FactoryRegistry())
·ͱΊ • ͱʹ͔͘Ϟδϡʔϧείʔϓͷఆٛ ͕؆୯ • ύϑΥʔϚϯεྑ͍ • ςετͷαϙʔτ͋Δ