๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[์•ˆ๋“œ๋กœ์ด๋“œ] Kotlin-Android-Extensions(KTX) deprecated๋œ ์ด์œ 

by Jay Din 2023. 8. 13.
728x90
๋ฐ˜์‘ํ˜•

๊ธฐ์กด์—๋Š” XMLํŒŒ์ผ์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ findViewById๋กœ ์ฐพ์•„์™”์Šต๋‹ˆ๋‹ค.

 

findViewById ์˜ˆ์ œ

๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ์— text๋ฅผ ๋„ฃ๋Š”๋‹ค๊ณ  ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ์•ผํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ๋ฒ„ํŠผ์ด 100๊ฐœ๋ผ๋ฉด findViewById๋ฅผ ํ†ตํ•ด 100๊ฐœ์˜ ๋ฒ„ํŠผ์„ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด

  • ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
  • ์ผ์ผ์ด ์ง์ ‘ ์ž…๋ ฅํ•ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ ค ์ƒ์‚ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // ๋งŒ์•ฝ ๋ฒ„ํŠผ์ด 100๊ฐœ๋ผ๋ฉด? findViewById -> 100๊ฐœ
        val button1 : Button = findViewById(R.id.button1)
        val button2 : Button = findViewById(R.id.button2)
        val button3 : Button = findViewById(R.id.button3)
        
        button1.text = "abcd"
        button2.text = "abcd"
        button3.text = "abcd"
    }
}

์ผ์ผ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ๊ณผ ์ƒ์‚ฐ์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.

์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด Kotlin-Android-Extension์ด ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 Kotlin-Android-Extension ๋“ฑ์žฅ

Kotlin-Android-Extension์„ KTX๋ผ๊ณ ๋„ํ•ฉ๋‹ˆ๋‹ค.
์‚ฌ์‹ค โ€˜kotlin-android-extensionsโ€™์€ ์ข‹์€ ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ์•„๋‹™๋‹ˆ๋‹ค.

findViewById์˜ ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ๋งŒ๋“ค์–ด์กŒ๊ณ , ๋‚ด๋ถ€์ ์ธ ์บ์‹œ๋ฅผ ํ†ตํ•ด ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์˜€์„ ๋ฟ์ž…๋‹ˆ๋‹ค.

KTX ์„ธํŒ… ๋ฐฉ๋ฒ•

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    // KTX๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์„ ์–ธ
    id 'kotlin-android-extensions'
}

MainActivity.kt

KTX๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด
activity_main.xml์—์„œ Button ์ปดํฌ๋„ŒํŠธ์— ๋ถ€์—ฌํ•ด์ค€ Id๋ฅผ ๋ฐ”๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

        // KTX
        button1.text = "abcd"
        button2.text = "abcd"
        button3.text = "abcd"
        
    }
}

 

deprecated๋œ ์ด์œ 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ช‡ ๊ฐ€์ง€ ์„ฑ๋Šฅ์ ์ธ ์ด์Šˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์—๋Ÿฌ ๋ฐœ์ƒ ์œ ๋ฐœ, ๋Œ€ํ‘œ ์˜ˆ์ œ

SecondActivity.kt์™€ activity_second.xml Second ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 

activity_second.xml ์— ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/secondButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

๋‚ด๊ฐ€ ์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€ ์‹ค์ˆ˜๋กœ MainActivity.kt์—์„œ secondButton์„ ์ž‘์„ฑํ•ด๋„ ์ฝ”๋“œ์ƒ์—๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ์ปดํŒŒ์ผ ํ–ˆ์œผ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ ์•ฑ์ด ์ฃฝ์Šต๋‹ˆ๋‹ค.
ํ˜ผ์ž ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ์‹ค์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ๊ธด ํ•˜์ง€๋งŒ ํ˜‘์—…์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ๋ ˆ์ด์•„์›ƒ ์ปดํฌ๋„ŒํŠธ Id๊ฐ€ ๊ฒน์ณค์„ ๊ฒฝ์šฐ์—๋„ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์‹œ๊ฐ„ ๋‚ญ๋น„๋ฅผ ํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด๋Ÿฐ์‹์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๊ธ€์—์„œ ์“ฐ์ง€ ๋ง๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

MainActivity.kt

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

        // KTX
        button1.text = "abcd"
        button2.text = "abcd"
        button3.text = "abcd"
        
        // SecondActivity์— ์žˆ๋Š” ๋ฒ„ํŠผ ํ˜ธ์ถœ
        secondButton.text="abcd"
        
    }
}

์—๋Ÿฌ ํ™”๋ฉด

 ViewHolder ์—์„œ๋Š” ์บ์‹ฑ์„ ํ•ด์ฃผ์ง€ ์•Š์Œ

ViewHolder๋Š” ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๋ ค๊ณ  ๋งŒ๋“ค์–ด์กŒ๋Š”๋ฐ, ์ •์ž‘ ViewHolder์—์„œ Koltin-android-extensions์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์บ์‹ฑ์„ ํ•ด์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋””์ปดํŒŒ์ผ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜๋ฉด _findCachedViewById๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฒฐ๊ตญ onBind๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค findViewById๋ฅผ ๋งค๋ฒˆ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ์•ผ ํ•˜๋Š”RecyclerView.ViewHolder์—์„œ ์ด๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ??

๊ตฌ๊ธ€์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ViewBinding๊ณผ DataBinding์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

ViewBinding ์‚ฌ์šฉ๋ฒ•

https://jay-din.tistory.com/61

 

[์•ˆ๋“œ๋กœ์ด๋“œ] View Binding(๋ทฐ๋ฐ”์ธ๋”ฉ) ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

์ด ํฌ์ŠคํŒ…์—์„œ๋Š” View Binding์˜ Activity์™€ Fragment์˜ ์˜ˆ์ œ๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. // ์ด๋ก ์ด ํ•„์š”ํ•œ ์‚ฌ๋žŒ์€ ViewBinding vs DataBinding ์ฐธ๊ณ  ์„ธํŒ… build.gradle android { . .. // ํ•ด๋‹น ์ฝ”๋“œ ์ถ”๊ฐ€ buildFeatures{ viewBinding = true } } A

jay-din.tistory.com

DataBinding ์‚ฌ์šฉ๋ฒ•

https://jay-din.tistory.com/60

 

[์•ˆ๋“œ๋กœ์ด๋“œ Android] Data Binding ์‚ฌ์šฉ๋ฒ•

DataBinding๊ณผ ViewBinding์ด ๋ญ๊ฐ€ ๋‹ค๋ฅธ๊ฐ€? ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด, DataBinding์€ ์ด๋ฆ„์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฐ์ดํ„ฐ์™€ ๊ฐ™์ด ๊ฒฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ) // ์ž์„ธํ•œ ์ด๋ก ์€ ViewBinding vs Data

jay-din.tistory.com


์ฐธ๊ณ 

https://images.velog.io/images/deepblue/post/c779b210-567e-4984-839b-5e62eb0e9dad/1_rLvIP4jfh3D-Sw9xm7T0IQ.png

https://thdev.tech/android/2020/10/07/Remove-kotlinx-synthetic/

728x90
๋ฐ˜์‘ํ˜•