Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Jetpack Compose さわってみた / tried writing code wit...
Search
kenken
August 15, 2019
Programming
940
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Jetpack Compose さわってみた / tried writing code with Jetpack Compose
kenken
August 15, 2019
More Decks by kenken
See All by kenken
Maintaining E2E Test Automation as We Transition from View to Compose
tkhs0604
0
2.7k
try! Swift macros
tkhs0604
1
790
Jetpack Compose ことはじめ / the beginning of Jetpack Compose
tkhs0604
1
3.4k
Other Decks in Programming
See All in Programming
残高管理から台帳サービスへの進化
artoy
1
200
Next.js: The final boss of Web Performance
guaca
0
260
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
260
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
270
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
230
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
310
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
sbt 2.0: takeoff
eed3si9n
0
160
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
エンジニアに許された特別な時間の終わり
watany
109
250k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
Believing is Seeing
oripsolob
1
240
Between Models and Reality
mayunak
4
500
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
440
The SEO Collaboration Effect
kristinabergwall1
1
590
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
270
Transcript
Jetpack Compose さわってみた @tkhs0604 #love_kotlin Kotlin愛好会 vol.14 @ Wantedly, Inc.
人生初登壇です
• 高橋 健太 ◦ kenken | @tkhs0604 ◦ https://tkhs0604.hatenablog.com •
Gunosy Inc. • SI営業→エンジニア(iOS/Android/Web)→Androidエンジニア • アカペラ ◦ 最近ORICON NEWSに少しだけ載りました 自己紹介
• 開催日:2019/8/24(土) • 場 所:東京コンファレンスセンター品川 5F • (LT枠ですが) 登壇します Kotlin Fest
2019
• 自分のことを覚えて帰っていただく← • Jetpack Composeの使い方を完全に理解していただく 本日のゴール
• Jetpack Compose とは ◦ 宣言的UIとは • UI生成までの流れ • コンポーネントの作成方法
◦ @Composable ◦ @Model • まとめ • さらに知りたい方へ アジェンダ
Jetpack Compose とは
• Google I/O 2019 • Android Jetpack • Kotlin •
宣言的UI ◦ cf. Flutter、React Native、Litho、Vue.js • Pre-alpha版 (2019/8/12時点) ◦ ⚠大きく変更が入る可能性があります Jetpack Compose とは
• Concise and Idiomatic Kotlin ◦ Kotlinにより簡潔に記述可能 • Declarative ◦
フレームワークによる描画の最適化 • Compatible ◦ 既存のView (Android API)と併用可能 • Enable Beautiful Apps ◦ Material Designやアニメーションが利用可能 • Accelerate Development ◦ Apply Changes、Live Previewの活用 → 未だなさそう? コアコンセプト
• Concise and Idiomatic Kotlin ◦ Kotlinにより簡潔に記述可能 • Declarative ◦
フレームワークによる描画の最適化 • Compatible ◦ 既存のView (Android API)と併用可能 • Enable Beautiful Apps ◦ Material Designやアニメーションが利用可能 • Accelerate Development ◦ Apply Changes、Live Previewの活用 → 未だなさそう? Jetpack Compose の コアコンセプト 宣言的?
宣言的UIとは 宣言的 命令的 What (何をするか)を記述 How (どのようにするか)を記述 遅延評価 即時評価 //
android.widget.Button Button(this).apply { text = "button" setBackgroundColor(Color.BLUE) setOnClickListener { ... } } // androidx.ui.material.Button Button( text = "button", color = +themeColor { Color.Blue }, onClick = { … } )
UI生成までの流れ
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } }
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } } • Activityの拡張関数 • Root ViewとなるFrameLayoutの準備 • 引数で受け取ったコンポーネントを Root Viewの直下へ追加
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } } • 各種リソースへアクセスするための設定 ◦ Context ◦ Density ◦ FocusManager ◦ TextInputService • 引数で受け取ったコンポーネントを AndroidCraneViewへ追加
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } } • コンポーネントのThemeの設定 ◦ TextStyleやColorの設定も関数で行う
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } } • コンポーネントのAlignの設定 ◦ MarginやPaddingの設定も関数で行う
Hello World class SampleActivity : Activity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { Text( text = "Hello World", style = +themeTextStyle { h3 } ) } } } } } • Android APIでいうところのTextView +…?
コンポーネントの作成方法
コンポーネントの作成 • @Composableを関数に付与することでコンポーネントとして認識 される • コンポーネントが満たさなければならない条件 ◦ 引数で与えられる値以外を操作しない ◦ 副作用が生じるようなパラメータは引数で受け取る
コンポーネントの作成 @Composable fun CounterWidget() { Column { Text( text =
"Count: 0", style = +themeTextStyle { h5 } ) Button( text = "+" ) } }
コンポーネントの作成 class SampleActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) setContent { CraneWrapper { MaterialTheme { Center { CounterWidget() } } } } }
• モデルクラスを作成する ◦ @Modelをクラスに付与する • モデルクラスを利用する ◦ コンポーネント内で+state関数によって状態管理用の Stateオブジェクトを生成する •
Stateオブジェクトのプロパティが更新されると、 フレームワークによってUIが再描画される 状態を持ったコンポーネントの作成
状態を持ったコンポーネントの作成 @Model data class CounterModel( var count: Int ) @Composable
fun CounterWidget() { Column { val counter by +state { CounterModel(0) } Text( text = "Count: ${counter.count}", style = +themeTextStyle { h5 } ) Button( text = "+", onClick = { counter.count++ } ) } }
状態を持ったコンポーネントの作成 @Model data class CounterModel( var count: Int ) @Composable
fun CounterWidget() { Column { val counter by +state { CounterModel(0) } Text( text = "Count: ${counter.count}", style = +themeTextStyle { h5 } ) Button( text = "+", onClick = { counter.count++ } ) } } • プリミティブ型、String型の場合は val counter = +state { 0 } で呼び出し可能
状態を持ったコンポーネントの作成 @Model data class CounterModel( var count: Int ) @Composable
fun CounterWidget() { Column { val counter by +state { CounterModel(0) } Text( text = "Count: ${counter.count}", style = +themeTextStyle { h5 } ) Button( text = "+", onClick = { counter.count++ } ) } } • @Modelの付与されたクラスでないと UIの再描画はされない
まとめ
• まだまだ資料は全然少ない • 開発環境も整っていない (AOSPを直接DLするしか現状ない) • 独特の記法(+state関数など)がところどころあるので、 逐一実装を追って理解するのがしんどい • 今までkt/javaファイル、layout.xml、attrs.xml、drawable.xmlに書
いていたことを集約できるので、開発が進めば便利になりそうな気 はする • まだあわてるような時間じゃない 所感
さらに知りたい方へ
Kotlin Community • Kotlin Slackへの参加申請 (#composeチャンネル) ◦ https://surveys.jetbrains.com/s3/kotlin-slack-sign-up
Thank you very much for listening.