๐Ÿ”ฅTIL๐Ÿ”ฅJetpack Compose

hyihyiยท2024๋…„ 3์›” 25์ผ
0

Compose

๋ชฉ๋ก ๋ณด๊ธฐ
2/4
post-thumbnail
post-custom-banner

1. Jetpack

1.1 Jetpack์ด๋ž€?

2018๋…„ 5์›” 8์ผ์— ๊ตฌ๊ธ€์ด ๋ฐœํ‘œํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋„๊ตฌ ๋ชจ์Œ์ง‘
Jetpack์ด ๋‚˜์˜ค๊ธฐ ์ „ Support library๊ฐ€ ์žˆ์—ˆ์ง€๋งŒ ๊ฐœ์„ ํ•ด์„œ ๋‹ค์‹œ ๋‚˜์˜จ ๊ฒƒ์ด Jetpack

1.2 Support library์˜ ๋ฌธ์ œ์ 

์ฐธ๊ณ ํ•œ ๋‹น๊ทผ๋งˆ์ผ“ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ

(1) ํŒจํ‚ค์ง€๋ช…์ด ๋ชจํ˜ธํ–ˆ๋‹ค.

com.android.support:support-v4
com.android.support:support-v13

  • support-v4๋ฅผ ๋ณด๊ณ  "๋ฒ„์ „ 4"๋ผ๊ณ  ์ฐฉ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์‚ฌ์‹ค์€ API ๋ ˆ๋ฒจ 4 ์ด์ƒ์ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

  • ๋˜‘๊ฐ™์ด support-v13๋„ API ๋ ˆ๋ฒจ 13 ์ด์ƒ์„ ์ง€์›ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

ํ•˜์ง€๋งŒ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ง€์›์„ ์ค‘๋‹จํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— API ๋ ˆ๋ฒจ์„ ์˜๋ฏธํ•˜๋Š” ๋„ค์ด๋ฐ์€ ๊ทœ์น™์„ฑ์„ ์ƒ์‹คํ–ˆ๋‹ค.

(2) ๋‹จ์ผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณต๋œ๋‹ค

6.5 ๋ฒ„์ „๋ถ€ํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ธฐ๋Šฅ ๋ณ„๋กœ ๋ชจ๋“ˆํ™”๋˜์—ˆ์ง€๋งŒ ๊ทธ ์ด์ „์—๋Š” 3๋งŒ ๊ฐœ์— ๊ฐ€๊นŒ์šด ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋‹จ์ผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜€๋‹ค.

๊ทธ๋ž˜์„œ ์•ฑ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ฑฐ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋งŽ์œผ๋ฉด '64K reference limit'์ด๋ผ๋Š” ์—๋Ÿฌ๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋œ๋‹ค.

์‚ฌ์ง„์ถœ์ฒ˜

ART ๋Ÿฐํƒ€์ž„ ๋ฐ ๋ฉ€ํ‹ฐ๋ฑ์Šค ์ง€์› ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋‚˜์˜ค๋ฉด์„œ ์ด ๋ฌธ์ œ๋Š” ๊ธฐ์ˆ ์ ์œผ๋กœ ์™„์ „ํžˆ ํ•ด๊ฒฐ๋์ง€๋งŒ, support-v4๋Š” ์ด๋ณด๋‹ค 2๋…„ ๋’ค์ธ 24.2.0 ๋ฒ„์ „์ด ์ถœ์‹œ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋‹จ์ผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณต๋๋‹ค.

์˜ˆ์‹œ๋กœ
support-core-ui์—๋Š” ViewPager๋ฟ๋งŒ ์•„๋‹ˆ๋ผ DrawerLayout, CursorAdapter, SwipeRefreshLayout, SlidingPaneLayout ๋“ฑ์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด ์ด๋Ÿฐ ๊ตฌ์กฐ๋กœ ์ธํ•ด ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์œ„์ ฏ ๊พธ๋Ÿฌ๋ฏธ๋“ค์„ Gradle ๋นŒ๋“œ ์‹œ์Šคํ…œ์œผ๋กœ ๋‚ด๋ ค๋ฐ›๊ณ , APK์— ํฌํ•จํ•ด์•ผํ•ด ๋น„ํšจ์œจ์ ์ด์—ˆ๋‹ค.

(3) ๋ฐ”์ด๋„ˆ๋ฆฌ ์ œ์•ฝ

์„œํฌํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŒจํ‚ค์ง€ ๋ณ„๋กœ ์˜์กด์„ฑ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„์ „์„ ์—…๊ทธ๋ ˆ์ด๋“œ ํ–ˆ์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์—ฐ๊ด€๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๋‹ค ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ ํ•ด์•ผ ํ–ˆ๋‹ค.

์ด๊ฑด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ฐฐํฌํ•˜๋Š” ์ž…์žฅ์—์„œ๋„ ๊ฝค๋‚˜ ์„ฑ๊ฐ€์‹œ๊ณ  ๋™์‹œ์— ์„œํฌํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ๊ฐ€ ๋ถ€๋‹ด์Šค๋Ÿฌ์šด ์ด์œ ์ด๊ธฐ๋„ ํ–ˆ๋‹ค.

Support library๋Š” ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๊ณ  ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์ถ”๊ฐ€ํ•œ JetPack์ด ๋‚˜์™”๋‹ค.

1.2 Jetpack ๊ตฌ์„ฑ์š”์†Œ

https://developer.android.com/jetpack/androidx/explorer?case=popular
์ž์„ธํ•œ ์„ค๋ช…๊ณผ ๋ชฉ๋ก์€ ์œ„ ๋งํฌ์— ๋“ค์–ด๊ฐ€์„œ ํ™•์ธํ•˜์ž

1.3 ๋ช…๋ นํ˜• VS ์„ ์–ธํ˜• UI (Declarative UI)

1) ๋ช…๋ นํ˜• : How

๊ฐœ๋ฐœ์ž๊ฐ€ ๋‹จ๊ณ„๋ณ„๋กœ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ž‘์—…์„ ๋ช…ํ™•ํžˆ ์ง€์ •

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 1. TextView๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
        val textView: TextView = findViewById(R.id.textView)
        val button: Button = findViewById(R.id.button)

        // 2. ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
        button.setOnClickListener {
            // 3. TextView์˜ ํ…์ŠคํŠธ๋ฅผ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
            textView.text = "Button Clicked"
        }
    }
}
  • ์„ค๋ช…
    • UI ์š”์†Œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฐธ์กฐ (findViewById ์‚ฌ์šฉ)
    • ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ TextView์˜ ํ…์ŠคํŠธ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ณ€๊ฒฝ
    • ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ "์–ด๋–ป๊ฒŒ" ํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝํ• ์ง€๋ฅผ ์ง์ ‘ ์ฝ”๋“œ๋กœ ๋ช…๋ น

๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ๋ช…์‹œํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. UI ์—…๋ฐ์ดํŠธ ๋“ฑ ๋ชจ๋“  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ๋ช…๋ น์–ด๋กœ ์ง์ ‘ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

2) ์„ ์–ธํ˜• : What

์ƒํƒœ๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ทธ ์ƒํƒœ์— ๋”ฐ๋ผ UI๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐฑ์‹ 

@Composable
fun MyComposable() {
    // 1. ์ƒํƒœ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
    var text by remember { mutableStateOf("Hello, World!") }

    // 2. ์ƒํƒœ์— ๋”ฐ๋ผ UI๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐฑ์‹ ๋˜๋„๋ก ์ปดํฌ์ €๋ธ”์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
    Column {
        // 3. Text ์ปดํฌ์ €๋ธ”์€ ์ƒํƒœ ๊ฐ’์— ๋”ฐ๋ผ UI๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
        Text(text = text)
        
        // 4. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ , UI๋Š” ์ž๋™์œผ๋กœ ๊ฐฑ์‹ ๋ฉ๋‹ˆ๋‹ค.
        Button(onClick = { text = "Button Clicked" }) {
            Text("Click Me")
        }
    }
}

@Preview
@Composable
fun PreviewMyComposable() {
    MyComposable()
}
  • ์„ค๋ช…
    • Text ์ปดํฌ์ €๋ธ”์€ ์ƒํƒœ(text)์— ๋”ฐ๋ผ ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œ
    • ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด, ์ƒํƒœ์— ๋”ฐ๋ผ UI๊ฐ€ ์ž๋™์œผ๋กœ ์žฌ๊ตฌ์„ฑ
    • "์–ด๋–ป๊ฒŒ"๊ฐ€ ์•„๋‹ˆ๋ผ "๋ฌด์—‡์ด" ๋ณด์—ฌ์ ธ์•ผ ํ•˜๋Š”์ง€๋ฅผ ์„ ์–ธ

์„ ์–ธํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฌด์—‡์„ ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ƒํƒœ๊ฐ€ ๋ณ€ํ•˜๋ฉด UI๊ฐ€ ์ž๋™์œผ๋กœ ์žฌ๊ตฌ์„ฑ๋˜๋ฉฐ, ๊ฐœ๋ฐœ์ž๋Š” "๋ฌด์—‡์„ ๋ณด์—ฌ์ค˜์•ผ ํ•˜๋Š”์ง€"๋งŒ ์‹ ๊ฒฝ ์”๋‹ˆ๋‹ค.

๐Ÿ“Œ ๋ช…๋ นํ˜• UI๋Š” ๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ์ง์ ‘ ์ œ์–ดํ•ด์•ผ ํ•˜์ง€๋งŒ ์„ ์–ธํ˜• UI์—์„œ๋Š” ์ƒํƒœ๋งŒ ๊ด€๋ฆฌํ•˜๋ฉด ๊ทธ ์ƒํƒœ์— ๋งž๊ฒŒ UI๊ฐ€ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๋œ๋‹ค.

1.4 Compose

Jetpack์— ์ถ”๊ฐ€๋œ ์ƒˆ๋กœ์šด ๋„๊ตฌ ํ‚คํŠธ

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€