[Android] Koin ์‚ฌ์šฉํ•˜๊ธฐ

์—…๋ฐ์ดํŠธ:

๐Ÿ‘ฉโ€๐Ÿ’ป Koin ์‚ฌ์šฉํ•˜๊ธฐ ๐Ÿ•

๊ณต๋ถ€ํ•ด์•ผ์ง€ ๋ฐฑ๋งŒ๋ฒˆ์€ ๋งˆ์Œ ๋จน์€ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด์ œ์„œ์•ผ ๋“ค์—ฌ๋‹ค๋ณด๋Š” DI (Dependency Injection)
Koin์€ ์ฝ”ํ‹€๋ฆฐ ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•ด ๊ฐœ๋ฐœ๋œ ์‹ค์šฉ์ ์ด๊ณ  ๊ฐ€๋ฒผ์šด ์˜์กด์„ฑ ์ฃผ์ž… ํ”„๋ ˆ์ž„์›Œํฌ๋‹ค. ์˜์กด์„ฑ ์ฃผ์ž… ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ Dagger, Hilt, Koin ๋“ฑ์ด ์žˆ๋Š”๋ฐ, Koin ์€ ๊ฐ€์žฅ ๋‚ฎ์€ ๋Ÿฌ๋‹ ์ปค๋ธŒ๋ฅผ ์ž๋ž‘ํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž…์— ์ž…๋ฌธํ•˜๊ธฐ ์ข‹์€ ํ”„๋ ˆ์ž„์›Œํฌ!

์˜์กด์„ฑ ์ฃผ์ž…์€ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์„ ๋œปํ•œ๋‹ค ์–ด๋–ค Activity๋‚˜ Fragment์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š”์ง€์— ๋”ฐ๋ผ Context๊ฐ€ ๊ณ„์† ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ํด๋ž˜์Šค ํƒ€์ž… ๊ฐ์ฒด์ž„์—๋„ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ Activity๋‚˜ Fragment์— ์ฃผ์ž… ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋ฉด Context์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ ๋„ ๊ณตํ†ต์œผ๋กœ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋œ๋‹ค.
์„ค๋ช…๋งŒ์œผ๋กœ๋Š” ์–ด๋–ค ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฑด์ง€ ๊ฐ์ด ์ž˜ ์˜ค์ง€ ์•Š์•„์„œ ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ๋“ค์„ ํ†ตํ•ด์„œ Koin์„ ์ ์šฉํ•˜๋Š” ๊ณผ์ •์„ ๋”ฐ๋ผํ•ด๋ดค๋‹ค.
ย 


๐ŸŒŠ DI ์‚ฌ์šฉ์‹œ ์ด์ 

  • ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค.
  • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์šฉ์ด
  • ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด(์ข…์†) ๊ด€๊ณ„๋ฅผ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถฐ ์คŒ
    ย 

๐ŸŒŠ Koin ์‚ฌ์šฉ ์‹œ ์•Œ์•„๋‘ฌ์•ผ ํ•  ํ‚ค์›Œ๋“œ

  • module : ์ฃผ์ž… ๋ฐ›๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค์–ด ์„ ์–ธ
  • single : ์•ฑ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ๊ณ„์† ์œ ์ง€๋˜๋Š” ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
  • factory : ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑ
  • get() : ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์•Œ๋งž์€ ์˜์กด์„ฑ์„ ์ฃผ์ž… ๋ฐ›์Œ
    ย 

๐ŸŒŠ dependency ์ถ”๊ฐ€

dependencies {
    implementation 'org.koin:koin-androidx-viewmodel:2.1.5'
}

ย 

๐ŸŒŠ ๋ชจ๋“ˆ ์„ ์–ธ

1. ์ƒ˜ํ”Œ ํด๋ž˜์Šค ์ƒ์„ฑ
class SampleRepository() {
    val sampleData = "Sample Repository"
}

class SampleController(val repository: SampleRepository) {
    fun printSampleData() {
        Log.d("Print sample data", repository.sampleData)
    }
}

class SampleViewModel : ViewModel() {
    private var _isLoading = MutableLiveData<Boolean>()
    val isLoading: LiveData<Boolean> = _isLoading
}

ย 

2. ๋ชจ๋“ˆ๋กœ ์„ ์–ธํ•˜์—ฌ ๋ณ€์ˆ˜์— ์ €์žฅ
val appModule = module {
    single { SampleRepository() }
    factory { SampleController(get()) }
}

val viewModelModule = module {
    viewModel { SampleViewModel() }
}

์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ Koin์—์„œ ์•Œ์•„๋‘ฌ์•ผ ํ•  ํ‚ค์›Œ๋“œ๋ฅผ ๋Œ€์ž…ํ•ด๋ณด์ž๋ฉด,

  • single ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ๋‹จ ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑ๋˜๊ณ 
  • factory ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ์š”์ฒญ ์‹œ์ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑ
  • get() ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ์•Œ๋งž์€ ์˜์กด์„ฑ์„ ์ฃผ์ž…
  • viewModel ViewModel์˜ ๊ฒฝ์šฐ๋Š” viewModel ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ
    ย 

๐ŸŒŠ ๋ชจ๋“ˆ ๋“ฑ๋ก

class InquiryApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidLogger()
            androidContext(this@InquiryApplication)
            modules(appModule)
            modules(viewModelModule)
        }
    }
}
  • startKoin ์„ ํ˜ธ์ถœํ•ด์„œ ์„ ์–ธํ•œ ๋ชจ๋“ˆ ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.
  • androidLogger() : AndroidLogger๋ฅผ Koin logger๋กœ ์‚ฌ์šฉ
  • androidContext() : ํ•ด๋‹น ์•ˆ๋“œ๋กœ์ด๋“œ context ์‚ฌ์šฉ
  • modules() : ์‚ฌ์šฉํ•  ๋ชจ๋“ˆ ๋“ฑ๋ก
  • Application ํด๋ž˜์Šค ์ƒ์„ฑ ํ›„ ๊ผญ AndroidManifest.xml์— ๋“ฑ๋กํ•ด์•ผ ํ•จ!
<application
    android:name=".InquiryApplication"
    ...
}

ย 

๐ŸŒŠ ์˜์กด์„ฑ ์ฃผ์ž…

  • inject() ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ์„ ์ฃผ์ž…
  • ViewModel์˜ ๊ฒฝ์šฐ viewModel() ์‚ฌ์šฉ
  • ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” Activity ํด๋ž˜์Šค์—์„œ ์ฃผ์ž…ํ•˜๋ฉด ๋œ๋‹ค.
class SampleActivity : AppCompatActivity() {

    val controller: SampleController by inject()
    val viewModel: SampleViewModel by viewModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate()
    }
}

ย 
ย 
[์ฐธ๊ณ ]
https://salix97.tistory.com/265
https://velog.io/@hoyaho/Koin
https://spoqa.github.io/2020/11/02/android-dependency-injection-with-koin.html

ํƒœ๊ทธ: , ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: