๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Android & Kotlin

Lesson 1: Build your First App

Udacity์—์„œ Developing Android Apps with Kotlin ์ด๋ผ๋Š” ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  lesson ๋ณ„๋กœ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์„ ํ•„๊ธฐํ•œ ๋‚ด์šฉ์ด๋‹ค. 
์˜์–ด ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ํ•„๊ธฐํ•œ ๋‚ด์šฉ์ด๋‹ค๋ณด๋‹ˆ ๋ฒˆ์—ญ์ด๋‚˜ ํ•ด์„์ด ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋˜ ์ผ๋ถ€๋Š” ์˜์–ด ๋‹จ์–ด๋‚˜ ๋ฌธ์žฅ์„ ๊ทธ๋Œ€๋กœ ์ ์–ด๋†“์•˜๋‹ค. 
์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ฐ•์˜๋ฅผ ์ง์ ‘ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•œ๋‹ค.

 

Developing Android Apps with Kotlin | Udacity Free Courses

Take Udacity's free online course and learn Android development with Kotlin. Master the fundamentals of the Kotlin programming language taught by Kotlin experts at Google.

www.udacity.com


9. Main App Anatomy

  • Kotlin files: ์•ฑ์˜ ์ฝ”์–ด ๋กœ์ง
  • res folder: ์ด๋ฏธ์ง€, ์ŠคํŠธ๋ง ๊ฐ™์€ ์ •์  ์ปจํ…์ธ 
  • AndroidManifest.xml: OS์—์„œ ์•ฑ์„ ๊ตฌ๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก app์˜ ํ•„์ˆ˜์š”์†Œ๋“ค์— ๋Œ€ํ•œ ์ •์˜
  • Gradle script: ์•ฑ ๋นŒ๋“œ ๋ฐ ์‹คํ–‰์„ ์œ„ํ•œ ๊ฒƒ

11. Activity and Layout

Activity๊ฐ€ ์‹œ์ž‘๋˜๋ฉด layout inflation ๊ณผ์ •์ด ์‹œ์ž‘๋˜๋Š”๋ฐ layout inflation ๋™์•ˆ์— XML Layout ํŒŒ์ผ์— ์ •์˜๋œ view ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— Kotlin view ๊ฐ์ฒด๋กœ ๋ฉ”๋ชจ๋ฆฌ์— *inflate ๋œ๋‹ค. 
์ด ๊ณผ์ •์ด ์ง„ํ–‰๋˜๋ฉด activity๋Š” ์ด ๊ฐ์ฒด๋“ค์„ ์Šคํฌ๋ฆฐ์— ๊ทธ๋ฆฌ๊ณ  ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๊ฒŒ ๋œ๋‹ค. 
*inflate: (๊ณต๊ธฐ๋‚˜ ๊ฐ€์Šค๋กœ) ๋ถ€ํ’€๋ฆฌ๋‹ค[๋ถ€ํ’€๋‹ค]

15. Connecting the Button

๋งค๋ฒˆ XML ํŒŒ์ผ์— view๋ฅผ ์œ„ํ•œ ID๋ฅผ ์ •์˜ํ•˜๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋Š” ์ž๋™์œผ๋กœ ID์™€ ํ•จ๊ป˜ R(Resource) ํด๋ž˜์Šค์— ์ •์ˆ˜ํ˜• ์ƒ์ˆ˜(Integer constant)๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. R ํด๋ž˜์Šค๋Š” ํ”„๋กœ์ ํŠธ ๋‚ด์˜ ๋ชจ๋“  resources ๋งคํ•‘ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด๋‹น id์˜ view๋ฅผ ์ฐพ์œผ๋ฉด Kotlin view object์˜ reference๋ฅผ return ํ•ด์„œ activity ๋‚ด์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ๋‹ค. 

โญ๏ธ View binding: ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค 3.6+ ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ findViewById๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค. View binding์€ ๊ฐ XML Layout ๋งˆ๋‹ค binding ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

// Creating a binding object for the main_activity.xml layout
binding = ActivityMainBinding.inflate(layoutInflater) 

// Referencing a view with the ID roll_button
binding.rollButton

View binding์€ 1) Type safety 2) Null safety๋ผ๋Š” ์ด์ ์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. 
1) Type safety: findViewById๋Š” ๋ฐ˜ํ™˜๋ฐ›๊ธฐ ์›ํ•˜๋Š” ํƒ€์ž…์„ ๋ช…์‹œํ•˜๊ธฐ๋ฅผ ์š”๊ตฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์‹ค์ œ ํƒ€์ž…์€ Button์ธ๋ฐ ์‹ค์ˆ˜๋กœ ImageButton์„ ๋ฐ˜ํ™˜๋ฐ›๋Š” ๊ฒƒ์œผ๋กœ ๋ช…์‹œํ•˜์˜€๋‹ค๋ฉด ClassCastException์ด ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. View binding์€ View๊ฐ€ binding์˜ ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹ ์†์„ฑ์œผ๋กœ ์ด๋Ÿฌํ•œ ํƒ€์ž…์˜ ์—๋Ÿฌ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•ด์ค€๋‹ค. 
2) Null safety: findViewById๋Š” view์˜ resource id๋ฅผ ๋œปํ•˜๋Š” ์ •์ˆ˜ํ˜• ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์˜ˆ์ƒํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ด€๋ จ ์—†๋Š” ์ •์ˆ˜์™€ ์œ ํšจํ•˜์ง€ ์•Š์€ view id์™€ ๊ฐ™์€ ์–ด๋– ํ•œ ์ •์ˆ˜๋“ ์ง€ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ ๋ ˆ์ด์•„์›ƒ ๋‚ด์˜ view resource id์™€ ๋งค์น˜๋˜์ง€ ์•Š๋Š” ์ •์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒฝ์šฐ findViewById๋Š” null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  NullPointerException์„ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. View binding์€ view ๊ฐ์ฒด๋ฅผ ๋ฐ”๋กœ ์ฐธ์กฐํ•˜๊ณ  ์ •์ˆ˜ํ˜• id๋กœ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— null์—์„œ ์•ˆ์ „ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 

17. Exercise: OnClickListener

Context ๊ฐ์ฒด: ์•ˆ๋“œ๋กœ์ด๋“œ OS์˜ ํ˜„์žฌ ์ƒํƒœ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป๊ณ  ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค. ๋˜ํ•œ MainActivity๋Š” context์˜ subclass๋กœ MainActivity ๋‚ด๋ถ€์—์„œ this ํ‚ค์›Œ๋“œ๋Š” context ๊ฐ์ฒด์ด๋‹ค. 

23. Exercise: Finding Views Efficiently

findViewById()๋กœ view hierarchy๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์€ expensive operation์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ•„๋“œ๋กœ ์ •์˜ํ•ด์ฃผ์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 
๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ํ•„๋“œ๋ฅผ ์–ด๋””์„œ ์ดˆ๊ธฐํ™”ํ•  ๊ฒƒ์ด๋ƒ๋Š” ๊ฒƒ์ด๋‹ค. 
์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ๋Š” activity constructor์— ์ฝ”๋“œ๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๋˜ layout์˜ view๋Š” setCotentView()๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ๋•Œ๊นŒ์ง€ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๋ง์€ setContentView()๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ๋‚˜์„œ์•ผ view ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

ํ•ด๊ฒฐ์ฑ…1๏ธโƒฃ: view ํ•„๋“œ๋ฅผ nullable ํ•„๋“œ๋กœ ์ง€์ •ํ•˜๊ณ  ๋จผ์ € null๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. setContentView() ๋‹ค์Œ์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค. 
โžก๏ธ ์ด ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๋ฅผ ๋ณต์žกํ•˜๊ฒŒ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋งค๋ฒˆ view ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ null ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค. 

ํ•ด๊ฒฐ์ฑ…2๏ธโƒฃ: ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ non-null ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฒ•. lateinit ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ. 
ํ•„๋“œ ๊ฐ€์žฅ ์•ž์— lateinit์„ ์จ์ฃผ๋ฉด Kotlin ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์–ด๋–ค ๋‹ค๋ฅธ operation ์ „์— ์ดˆ๊ธฐํ™”ํ•  ๊ฒƒ์„ ์•ฝ์†ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐœ๋ฐœ์ž ์—ญ์‹œ null๋กœ ๋†”๋‘์ง€ ์•Š์„ ๊ฒƒ์ž„์„ ์•ฝ์†ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— nullable ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•ด์ฃผ๊ฑฐ๋‚˜ null๋กœ ์ดˆ๊ธฐํ™”ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. setContentView() ๋‹ค์Œ์—์„œ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

24. Namespaces

dummy data๋ฅผ preview pane์—์„œ preview ํ•˜๋ ค๋ฉด tools:src="@drawable/<src-name>"์„ ํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ android:src="@drawable/<src-name>" ๊ณผ ๋น„๊ตํ•ด์„œ ๋‘˜ ๋‹ค src๋ผ๋Š” ๊ฐ™์€ attribute๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ฝœ๋ก  ์•ž์— namespace๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์—ญํ• ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. dummy data๋Š” ์ปดํŒŒ์ผํ•  ๋•Œ๋Š” ์ œ๊ฑฐ๋œ๋‹ค. 

25. Introduction to Gradle

Gradle: Android Bulid tool

  • Describing what devices the app is actually build to run on
  • Compiling code and resources into executable code
  • Declaring and managing dependent code and libraries(dependencies)
  • App signing for Google play
  • Running automated tests

Gradle ์‹คํ–‰ ๊ณผ์ •

Run ๋ฒ„ํŠผ ํด๋ฆญ โžก๏ธ Gradle ๋ช…๋ น์–ด๋“ค์ด ์†Œ์Šค์ฝ”๋“œ, dependencies, Resources, Others๋ฅผ ์ปดํŒŒ์ผ โžก๏ธ Android Application Package(APK: disributing ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์˜ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํฌ๋งท)๋กœ ๋งŒ๋“ฆ โžก๏ธ ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋Š” APK๋ฅผ ์‹ค์ œ ๊ธฐ๊ธฐ๋‚˜ ์• ๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ ์ „์†กํ•˜์—ฌ ์„ค์น˜ ํ›„ ์‹คํ–‰

Gradle ํŒŒ์ผ์˜ ์ข…๋ฅ˜

1๏ธโƒฃ bulid.gradle(Project): Project wide build settings.
2๏ธโƒฃ build.gradle(Module:app): For each *module of project. 
*module: Collections of source file and build settings for a discrete unit of functionality in app. ๋ณดํ†ต default๋กœ ํ•œ ๊ฐœ์˜ ๋ชจ๋“ˆ App์ด ์žˆ๋Š”๋ฐ ๋” ํฐ ์•ฑ์—์„œ ๊ธฐ๋Šฅ์„ ๋‚˜๋ˆ„๋‹ค ๋ณด๋ฉด ํ˜น์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ํ•˜๋‹ค ๋ณด๋ฉด ๋ชจ๋“ˆ์„ ๋” ๋งŒ๋“ค๊ฒŒ ๋˜๊ณ  ๊ฐ ๋ชจ๋“ˆ์€ build.gradle ํŒŒ์ผ์ด ์ƒ๊ธด๋‹ค. 

27. Android Compatibility

androidx๋Š” Android Jetpack namespace.
AppCompatActivity๋Š” compatibility class, activity๊ฐ€ menu bar(action bar)๋ฅผ ํฌํ•จํ•˜๋„๋ก ํ•ด์คŒ. 

28. Exercise: Vector Drawables

vector drawables๋Š” API 21 ์ด์ƒ์—์„œ ์ง€์›ํ•œ๋‹ค. ๋งŒ์•ฝ ์•ฑ ์ƒ์„ฑ ์‹œ minSdkVersion์ด 19๋ผ๋ฉด API 19, 20์—์„œ๋Š” vector drawables๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ฃผ๋Š”๊ฐ€? 
gradle์ด png ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ API 21๋ณด๋‹ค ๋‚ฎ์€ sdk์—์„œ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์˜ ๋ฌธ์ œ๋Š” png๋“ค์ด ์•ฑ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฐ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•ฑ ํฌ๊ธฐ๊ฐ€ ํฌ๋ฉด ๋‹ค์šด๋กœ๋“œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ๋””๋ฐ”์ด์Šค ์šฉ๋Ÿ‰๋„ ๋งŽ์ด ์ฐจ์ง€ํ•˜๊ณ  ์‚ฌ์šฉ์ž ๋ถˆ๋งŒ์ด ๋Š˜๊ธฐ ์‰ฝ๋‹ค. 

๐Ÿ’กํ•ด๊ฒฐ๋ฐฉ์•ˆ: androidx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ vector drawables๋ฅผ API 7๋ถ€ํ„ฐ ์ง€์›๋˜๊ฒŒ ๋„์™€์ค€๋‹ค. 
1๏ธโƒฃ build.gradle(app)์— androidx vector support ๊ฐ€๋Šฅํ•˜๋„๋ก ์ถ”๊ฐ€

vectorDrawables.useSupportLibrary = true

2๏ธโƒฃ layout์˜ root์— ์ƒˆ๋กœ์šด namespace ์ถ”๊ฐ€

xmlns:app="http://schemas.android.com/apk/res-auto"

3๏ธโƒฃ ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ์˜ ์ด๋ฏธ์ง€ ํƒœ๊ทธ์— app:srcCompat ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€

app:srcCompat="@drawable/<src-name>"

'Android & Kotlin' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Lesson 5: App Architecture(UI Layer)  (0) 2022.02.22
๊ณต๋ถ€ ์ฃผ์ œ  (0) 2022.02.19
Lesson 4: Activity & Fragment Lifecycle  (0) 2022.02.11
Lesson 3: App Navigation  (0) 2022.02.08
Lesson 2: Layouts  (0) 2022.02.06