๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค2024 ์•ˆ๋“œ๋กœ์ด๋“œ/๐Ÿฟ ์˜ํ™” ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ ์ผ์ง€

Android : ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ๋‹ด๋Š” "App StartUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ" ์•Œ์•„๋ณด๊ธฐ

by hyeonha 2024. 3. 26.

๋ชฉ์ฐจ

    ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—

    ์ด๋ฒˆ์— ์†Œ์…œ ๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฐ ๋กœ๊ทธ์ธ ๋ณ„๋กœ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค˜์•ผํ–ˆ๋‹ค.

    ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด์„œ ๋‚ด ๊ณ ๋ฏผ์€ 

    - onCreate์™€ init ์ค‘ ์–ด๋Š ๋ฉ”์„œ๋“œ์— ํฌํ•จํ•ด์•ผํ• ์ง€

    - ํ˜„์žฌ์˜ ์•กํ‹ฐ๋น„ํ‹ฐ ํด๋ž˜์Šค์™€  Application ํด๋ž˜์Šค ์ค‘ ์–ด๋””์— ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผํ•˜๋Š”์ง€๊ธฐ์ค€์— ๋Œ€ํ•œ ๊ฒƒ์ด์—ˆ๋‹ค. 

     

    ๋‘˜๋‹ค ์‹œ๋„๋ฅผ ํ•ด๋ณด์•˜์ง€๋งŒ ๋ช…ํ™•ํ•œ ์ฐจ์ด๋ฅผ ์•Œ๊ธฐ ์–ด๋ ค์›Œ  ๋ฉ˜ํ†  ๊ฐœ๋ฐœ์ž๋ถ„๊ป˜ ์งˆ๋ฌธ์„ ๋“œ๋ ธ๋‹ค!

    ๊ฐœ๋ฐœ์ž๋ถ„๊ป˜์„œ๋Š” ๋ณ„๋„์˜ ์ดˆ๊ธฐํ™” ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด์„œ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ง์”€ ์ฃผ์…จ๊ณ  ๊ทธ ๋ฐฉ์‹์ด ๋ฐ”๋กœ App StartUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค!!

    ๊ทธ๋ž˜์„œ ๋กœ๊ทธ์ธ ์ฝ”๋“œ์™€ ๊ด€๋ จํ•˜์—ฌ ๋‚ด ์ฝ”๋“œ๋ฅผ App StartUp ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „ํ›„  ์ฝ”๋“œ๋ฅผ ๋น„๊ตํ•˜๊ณ  ๋‚ด์šฉ์„ ์ดํ•ดํ•ด๋ณด๋ คํ•œ๋‹ค! 

     

    ์ด์ „๊นŒ์ง€๋Š”...

    ์ด์ „์—๋Š” ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ์— ๊ด€ํ•ด ๋ณ„๋„์˜ content provider๋ฅผ ์ •์˜ํ•ด์ฃผ์–ด์•ผํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜  content provider๋Š” ์ดˆ๊ธฐํ™” ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•ด์ฃผ์ง€ ์•Š๊ณ  ๋น„์šฉ์ด ๋งŽ์ด ๋“ ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๋‹ค.  ํ•˜์ง€๋งŒ ์ด์ œ App startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‹จ์ ์„ ๊ทน๋ณต ํ•  ์ˆ˜ ์žˆ๋‹ค.

    App startUp

    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์•ฑ ์‹œ์ž‘ ์‹œ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ข…์†ํ•ญ๋ชฉ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜ํ•˜๋Š”๋ฐ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค! ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ์‹œ์ž‘ ์‹œํ€€์Šค๋ฅผ ๊ฐ„์†Œํ™”ํ•˜๊ณ  ์ดˆ๊ธฐํ™” ์ˆœ์„œ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์•ฑ ์‹œ์ž‘ ์‹œ๊ฐ„๋„ ๊ฐœ์„ ๋œ๋‹ค๊ณ  ํ•œ๋‹ค!!

     

    ๊ตฌ์„ฑ ์š”์†Œ ์ด๋‹ˆ์…œ๋ผ์ด์ € ๊ตฌํ˜„

    ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘ ์‹œ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ž๋™์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด ์•ฑ์—์„œ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•˜๋Š” ๊ฐ ๊ตฌ์„ฑ์š”์†Œ์˜ ๊ตฌ์„ฑ ์š”์†Œ ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ์ •์˜ํ•ด์•ผํ•œ๋‹ค. 

     

    Initializer<T> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ ๊ตฌ์„ฑ์š”์†Œ ์ด๋‹ˆ์…œ ๋ผ์ด์ €๋ฅผ ์ •์˜ํ•ด์ค€๋‹ค. 

    ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” 2๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.

    1๏ธโƒฃ  create ๋ฉ”์„œ๋“œ  : ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ์ž‘์—…์„ ํฌํ•จํ•˜๊ณ  T ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    2๏ธโƒฃ dependencies ๋ฉ”์„œ๋“œ : ์ด๋‹ˆ์…œ๋ผ์ด์ €์— ์ข…์†๋œ ๋‹ค๋ฅธ Initializer<T> ๊ฐ์ฒด ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•ฑ์ด ์‹œ์ž‘๋  ๋•Œ ์ด๋‹ˆ์…œ ๋ผ์ด์ €๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์šฐ์„  startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋Š” ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์•˜๋‹ค.

     

    ์นด์นด์˜ค ์ดˆ๊ธฐํ™”

    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ์šฉ ์ „  : Application ํŒŒ์ผ์—์„œ  ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ๋‹ด๊ณ  ์žˆ๋‹ค. 

    @HiltAndroidApp
    class FilmoApp : Application() {
        override fun onCreate() {
            super.onCreate()
            KakaoSdk.init(this, getString(R.string.kakao_app_key))

     

     

    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ์šฉ ํ›„
    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผํ•˜๋Š” create ์ธํ„ฐํŽ˜์ด์Šค์™€ dependencies ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด Initializer<T>๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค.

    class KakaoInitializer : Initializer<Unit> {
        override fun create(context: Context) {
            KakaoSdk.init(
                context = context,
                appKey = context.getString(R.string.kakao_app_key),
            )
        }
    
        override fun dependencies(): List<Class<out Initializer<*>>> {
            return listOf()
        }
    }

     

    ๋„ค์ด๋ฒ„ ์ดˆ๊ธฐํ™”์˜ ๊ฒฝ์šฐ

    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ์šฉ ์ „

    class NaverInitializer : Initializer<Unit> {
        override fun create(context: Context) {
            NaverIdLoginSDK.initialize(
                context = context,
                clientId = context.getString(R.string.naver_client_id),
                clientSecret = context.getString(R.string.naver_client_secret),
                clientName = context.getString(R.string.naver_client_name),
            )
        }
    
        override fun dependencies(): List<Class<out Initializer<*>>> {
            return listOf()
        }
    }

     

    startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ์šฉ ํ›„

    class NaverInitializer : Initializer<Unit> {
        override fun create(context: Context) {
            NaverIdLoginSDK.initialize(
                context = context,
                clientId = context.getString(R.string.naver_client_id),
                clientSecret = context.getString(R.string.naver_client_secret),
                clientName = context.getString(R.string.naver_client_name),
            )
        }
    
        override fun dependencies(): List<Class<out Initializer<*>>> {
            return listOf()
        }
    }

     

    ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์ดˆ๊ธฐํ™”๋Š”?

    ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์ดˆ๊ธฐํ™”๋Š” ์™œ initilizer๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๊นŒ? 

    ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์€ ๋ณ„๋„์˜ ์ดˆ๊ธฐํ™”์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ   GetGoogleIdOption  ๊ฐ์ฒด ๊ตฌํ˜„ ํ›„ GetCredentialRequest ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ   ์ด๋ฅผ ํ†ตํ•ด credentialManager์„ ์ด์šฉํ•˜์—ฌ  ๋กœ๊ทธ์ธ์ด ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค

     initializer ์ดˆ๊ธฐํ™” ์ˆœ์„œ ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

    dependencies ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์•„๋ž˜์˜ค ใ…๊ฐ™์ด dependencies๋ฅผ ์ •์˜ํ•ด์ค€ ๊ฒฝ์šฐ WorkMangerInitializer๋ฅผ ์ดˆํ•จํ•˜๋ฏ€๋กœ startUp์€ ExampleLogger ์ „์— WorkManager๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค!

    class ExampleLoggerInitializer : Initializer<ExampleLogger> {
        override fun create(context: Context): ExampleLogger {
            // WorkManager.getInstance() is non-null only after
            // WorkManager is initialized.
            return ExampleLogger(WorkManager.getInstance(context))
        }
    
        override fun dependencies(): List<Class<out Initializer<*>>> {
            // Defines a dependency on WorkManagerInitializer so it can be
            // initialized after WorkManager is initialized.
            return listOf(WorkManagerInitializer::class.java)
        }
    }

     

    ๋‚ด๋ถ€ ๋™์ž‘ ๋œฏ์–ด๋ณด๊ธฐ

     

    ๋ฐฐ์šด ์ 

    ์ด์ œ ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด์„œ ์ข‹๋‹ค. ๋˜ํ•œ ๋ณ„๋„์˜ ๋ชจ๋“ˆ๋กœ ์ถ”์ถœํ•˜๋Š” ์ ์ด ์ฝ”๋“œ๊ตฌ์กฐ์—๋„ ํšจ์œจ์ ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

    ์•„์ง ๋” ์•Œ๊ณ  ์‹ถ์€ ์ ์€ ์ด๋ ‡๊ฒŒ startUp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‹œ์ž‘๋˜์—ˆ์„ ๋•Œ์™€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ฐœ์„ ์— ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์˜ํ–ฅ์ด ์žˆ๋Š”์ง€ ์ง์ ‘ ์ธก์ •ํ•ด์„œ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค

    728x90