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
KSPを使ってコード生成
Search
Takuji Nishibayashi
December 05, 2023
Technology
470
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
KSPを使ってコード生成
Takuji Nishibayashi
December 05, 2023
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
compose-hot-reload を試そうとした話
takuji31
0
150
CameraX使ってみた
takuji31
0
310
kotlinx.datetime 使ってみた
takuji31
0
1.1k
HiltのCustom Componentについて
takuji31
0
380
java.timeをAndroidで使う
takuji31
0
200
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
3.2k
kotlinx.serialization
takuji31
0
690
kanmoba-returns-02.pdf
takuji31
0
290
AndroidXとKotlin Coroutines
takuji31
0
440
Other Decks in Technology
See All in Technology
AAIFに入ってみた ~内から見えるコミュニティ動向~
sato4
0
190
RAG を使わないという選択肢
tatsutaka
1
220
2026TECHFRESH畢業分享會 - Lightning Talk - 打造精準高效的 MCP 設計模式與測試實務
line_developers_tw
PRO
0
960
スキルと MCP ツール、責務をどう分けるか? AI が迷わないインターフェース設計の戦略
cdataj
1
1k
FDE という解 ― 暗黙知と明示知をつなぐ、伴走型エンジニアリング ―
otanet
0
150
2026TECHFRESH畢業分享會 - 原生還是跨平台? App 開發踩坑實錄
line_developers_tw
PRO
0
970
非エンジニアがClaudeと挑んだ「1ヶ月間プロダクト30本ノック」
askokc
0
460
AIエージェントが名古屋の猛暑からあなたを守る
happysamurai294
0
110
入門!AWS Blocks
ysuzuki
1
110
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
130
2026 TECHFRESH 畢業分享會 - AI-Native 重塑軟體工程與虛擬講師
line_developers_tw
PRO
0
960
FinOps × AIエージェントで実現する コストインシデントの自動調査
oasis1994liveforever
0
130
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Building an army of robots
kneath
306
46k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
Why Our Code Smells
bkeepers
PRO
340
58k
WENDY [Excerpt]
tessaabrams
11
38k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
200
Visualization
eitanlees
152
17k
Optimising Largest Contentful Paint
csswizardry
37
3.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Darren the Foodie - Storyboard
khoart
PRO
3
3.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
Transcript
KSP を使ってコード生成 関西モバイルアプリ研究会 A @takuji31
自己紹介 西林 拓志(にしばやし たくじ) Twitter/GitHub takuji31 株式会社はてな Android アプリケーションエンジ ニア
Android (2009〜) Kotlin (2014〜) 2
KSP 使ってますか? 3
今日は KSP のプロセッサーの作り方(≠ 使い方)について話 します 4
KSP? 5
Kotlin Symbol Processor 6
google/ksp 7
コードにつけられたアノテーションを処理する 8
Kotlin friendly 9
Incremental proccessing 10
KMP 対応 11
Supported libs Dagger / Hilt (alpha) Room Moshi Glide etc.
12
プロセッサーの 作り方 13
source code // これを @SimpleGeneration interface Hoge // こうだ abstract
class AbstractHoge: Hoge 14
build.gradle.kts // ... dependencies { // KSP のAPI implementation("com.google.devtools.ksp:symbol-processing-api:1.9.21-1.0.15") //
KotlinPoet implementation("com.squareup:kotlinpoet:1.15.3") // KotlinPoet のKSP 用拡張 implementation("com.squareup:kotlinpoet-ksp:1.15.3") } 15
SymbolProcessor class ExampleSymbolProcessor( private val codeGenerator: CodeGenerator, private val logger:
KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver): List<KSAnnotated> { resolver .getSymbolsWithAnnotation(SimpleGeneration::class.qualifiedName!!) .filterIsInstance<KSClassDeclaration>() .forEach { it.accept(SimpleGenerationVisitor(codeGenerator, logger), Unit) } return emptyList() } } 16
Visitor class SimpleGenerationVisitor( private val codeGenerator: CodeGenerator, private val logger:
KSPLogger ) : KSVisitorVoid() { override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { if (classDeclaration.classKind != ClassKind.INTERFACE) { logger.error("Only interface allowed", classDeclaration) return } val packageName = classDeclaration.packageName.asString() val className = ClassName(packageName, "Abstract" + classDeclaration.simpleName.asString()) val typeSpec = TypeSpec.classBuilder(className) .addModifiers(KModifier.ABSTRACT) .addSuperinterface(classDeclaration.toClassName()) FileSpec.builder(packageName, className.simpleName) .addType(typeSpec.build()) .build() .writeTo( codeGenerator, Dependencies( aggregating = false, classDeclaration.containingFile!! ) ) } } 17
SymbolProcessorProvider class ExampleSymbolProcessorProvider : SymbolProcessorProvider { override fun create(environment: SymbolProcessorEnvironment):
SymbolProcessor { return ExampleSymbolProcessor(environment.codeGenerator, environment.logger) } } 18
Service Provider // resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider jp.takuji31.kotlinfest2022.compiler.ExampleSymbolProcessorProvider 19
Incremental Processing 20
生成するファイルの依存を定義すれば Processor 側で適 切に処理される 21
Isolated 22
1:N 23
Isolated Dependencies(aggregating = false, classDeclaration.containingFile!!) 24
Aggregated 25
N:1 26
Aggregated (集約) val dependencies: Array<KSAnnotated> = // ... Dependencies(aggregating =
true, *dependencies.mapNotNull { it.containingFile }) 27
依存をちゃんと指定しないと「なぜかコード生成されな い」みたいな事態になる 28
テスト 29
tschuchortdev/kotlin-compile-testing 30
テストコード val source = SourceFile.kotlin( "ExampleClass.kt", """ package jp.takuji31.kotlinfest2022.compiler import
jp.takuji31.kotlinfest2022.compiler.annotation.SimpleGeneration @SimpleGeneration interface SimpleInterface { fun printHelloWorld() } """.trimIndent() ) 31
テストコード val compilation = KotlinCompilation().apply { sources = listOf(source) inheritClassPath
= true symbolProcessorProviders = listOf(ExampleSymbolProcessorProvider()) kspWithCompilation = true } val result = compilation.compile() assertThat(result.exitCode) .isEqualTo(KotlinCompilation.ExitCode.OK) 32
ドキュメント/サンプルコード https://kotlinlang.org/docs/ksp-overview.html 公式ドキュメント https://github.com/google/ksp/tree/main/examples/playground 公式サンプル https://github.com/takuji31/kotlinfest2022-ksp-example 今回のスライドに出てきたソースコード https://github.com/takuji31/navigation-compose-screen 複雑な例 https://speakerdeck.com/takuji31/kotlin-symbol-processing-api-ksp-woshi-tute-
kotlin-a-purikesiyonnokai-fa-woxiao-lu-hua-suru Kotlin Fest 2022 で発表した時のスライド もう少し踏み込んだ話はこちら 33
Enjoy KSP Life! 34