안녕하세요 휴몬랩 초보 개발자 호Park 입니다.
이번엔 Kotlin Android 개발에 있어서 매우 유용한 라이브러리에 대해 이야기해 볼까 합니다
라이브러리란?
쉽고 간단하게 얘기하자면 그냥 가져다 쓰는 거라고 볼 수 있습니다.
이러한 복잡하고 사용하기 불편한 기능을 구글이나 다른 사람이 편하게 만들어 놓은 것을 우리가 그냥 가져다가 사용하는 것이라고 볼 수 있는데요 그렇다면 왜 그렇게까지 해야 할까요?
=> 이유는 재사용성으로 인한 코드 단축이 제일 크지 않을까요?!
2줄 짜리 코드를 100개 만들어야한다면 총 200줄이 될텐데,
그것을 한줄로 바꿔서 사용한다면 총 1 x 100 = 100줄이 되면 코드가 절반가량이나 줄어들겠죠?
코드가 단축되는것도 좋지만 무엇보다 같은 코드를 반복해서 사용해야할 경우 귀차니즘이 발동됩니다 ㅠㅠ
그런것을 방지하기 위해 우리는 라이브러리를 사용하게 됩니다.
한번도 안써본 사람은 있지만 한번만 쓴 사람이 없을만큼 편안한!!!
하지만 라이브러리 사용에도 단점이 존재한다 는 점!
IT업계는 항상 강조하지만 지금 이 순간에도 엄청난 발전을 하고 있습니다.
어제까지만해도 있었는데 오늘 갑자기 사라질 수 도 있는 곳이지요.
라이브러리 또한 마찬가지로 어느 날 갑자기 업데이트가 되지 않을 수 있고 서비스 중단이 될 수 도 있습니다.
그렇다면 이 라이브러리를 사용하고 있는 우리 앱들은 어찌될까요?
당장은 아무런 영향이 없지만 유지보수 하거나 업데이트시에 변경사항이 크다면..아무래도 쓰기 찝찝하겠죠?
새집으로 이사가는데 30년된 다 낡은 쇼파를 굳이 가져가야겠다면..ㅎㅎㅎㅎㅎ
그래서 그 예를 한번 살펴 보도록 하겠습니다.
코틀린 언어가 나오고부터 지금 시점까지 가장 유용하고 많이 쓰이던 라이브러리가 Anko 였습니다.
그런 Anko 라이브러리가 2019년 12월부로 deprecated가 되었죠 ㅠㅠ
기존에 있던 Anko는 사용할 수 있지만 지원이 멈추었기에 추후에 있을 유지보수와 업데이트를 생각하면 이미 사라진 라이브러리를 사용한다는건 좋은 선택은 아니라고 봅니다.
Anko 지원을 중단하게 된 이유로는 안드로이드 자체의 최적화가 지속적으로 진행되고 있으며 Kotlin 사용자들이 늘어남에 따라 더 안정되고 좋은 라이브러리들이 많이 나오게 되어 결국은 손을 놓게 되었다고 합니다. 흑흑!!
하지만 친절하게도 Anko 에서는 대안책까지 내놓았는데요.. (역시 내사랑 Anko!!)
정말 친절하게도 상황별로 Anko와 유사하거나 대체할 수 있는 라이브러리들을 나열해 놓았습니다.
그 중에서 이번에 살펴볼 라이브러리들은 ... 유틸리티 전용 라이브러중에 하나인 Splitties에 관하여 짧게 살펴보도록 할까요?!
Splitties
Splitties is a collection of small Kotlin multiplatform libraries (with Android as first target).
Splitties라고 이름을 짓게된 이유는 라이브러리들을 최대한 작게 쪼개어 놓았기 때문입니다.
예를들어서 a기능 b기능 c기능 모두 넣어논 A 라이브러리가 있다면 (그것이 Anko였죠..),
Splitties는 그 A라이브러리를 쪼개서 a라이브러리, b라이브러리, c라이브러리로 만들고 필요한 것만 가져다 쓰게끔 하였기에 그렇게 이름을 지었다고 합니다.
어찌됬든, 그냥 무작정 큰 것을 가져다가 쓰는것보단 필요한것만 가져다가 쓰면서 최대한 앱의 무게를 줄이는것이 목표라는 것이지요.
안드로이드에서 사용할 수 있는 스플릿은..
너무 많지요?ㅠㅠ
초보 안드로이드 개발자가 사용하기에 유용한것 2가지만 살펴보도록 하지요..
- Activities: Start activities with minimal boilerplate.
- Alert Dialog: Create simple alert dialogs with simple code.
- Alert Dialog AppCompat: AppCompat version of Alert Dialog.
- Alert Dialog AppCompat Coroutines: showAndAwait extension functions for AppCompat AlertDialog.
- App Context: Always have your application Context at hand with appCtx.
- Arch Lifecycle: Extensions to get ViewModels, use LiveData and observe Lifecycles.
- Arch Room: Room helpers to instantiate your DB and perform transactions in Kotlin.
- Bundle: BundleSpec to use Bundle with property syntax for Intent extras and more.
- Checked Lazy: mainThreadLazy that checks property access on main thread, and checkedLazy to make your own variant.
- Dimensions: Android dp extensions for View and Context. Particularly handy when using Views DSL.
- Exceptions: unexpectedValue(…), unsupportedAction(…) and similar functions that return Nothing.
- Fragments: Start activities from fragments and do transactions with minimal boilerplate.
- Fragment Args: Fragment arguments without ceremony thanks to delegated properties.
- Init Provider: Base class for ContentProviders used for automatic initialization purposes.
- Intents: Transform companion objects into powerful typesafe intent specs, and create PendingIntents the clean and easy way.
- Lifecycle Coroutines: Coroutines integration with AndroidX Lifecycle.
- Main Handler: Top-level mainHandler property to stop allocating multiple Handlers for main Looper.
- Main Thread: Properties and precondition checkers related to Android main thread.
- Material Colors: 2014 Material Design color palettes as color resources.
- Material Lists: List item Views implementing Material Design guidelines (perfect for usage in a RecyclerView).
- Permissions: Request runtime permissions without polluting your codebase.
- Preferences: Property syntax for Android's SharedPreferences.
- Resources: Extensions to get resources like strings, colors or drawables easily, with support for themed attributes.
- Selectable Views: Selectable Views with foreground property before API 23.
- Selectable Views AppCompat: Selectable Views for AppCompatTextView.
- Selectable Views ConstraintLayout: Selectable Views for ConstraintLayout.
- Snackbar: Grab a snack without ceremony with snack(…) and longSnack(…).
- Stetho init: Have Stetho for your debug builds, without writing any code!
- System Services: No more context.getSystemService(NAME_OF_SERVICE) as NameOfManager.
- Toast: Show a toast by just calling toast(yourText), and dodge API 25 BadTokenException.
- Typesafe RecyclerView: Typesafe ViewHolder and ItemViewHolder for easy basic usage of RecyclerView.
- Views: Extensions function and properties on Views.
- Views AppCompat: AppCompat extension of Views. Includes helpers for ImageView tinting, ActionBar and tooltip.
- Views CardView: CardView extension of Views. Provides a contentPadding property.
- Views Coroutines: Android Views + Kotlin coroutines.
- Views Coroutines Material: Material Components + Kotlin coroutines.
- Views DSL: Create UIs with readable Kotlin code.
- Views DSL AppCompat: AppCompat extension of Views DSL.
- Views DSL ConstraintLayout: ConstraintLayout extension of Views DSL.
- Views DSL CoordinatorLayout: CoordinatorLayout extension of Views DSL.
- Views DSL IDE preview: Preview Views DSL UIs in the IDE.
- Views DSL Material: Material Components extension of Views DSL.
- Views DSL RecyclerView: RecyclerView extension of Views DSL.
- Views Material: Material Components extension of Views.
- Views RecyclerView: RecyclerView extension of Views.
제일먼저 Activity Split 에 관해서 알아봅시다!
안드로이드에서 Activity는 하나의 화면이라고 볼 수 있는데요, A라는 화면에서 B라는 화면으로 넘어가려면,
val intent = Intent(this, bActivity::class.java) // this = 현재 activity
startActivity(intent)
이렇게 intent 객체를 생성해서 넘어가려고 하는 bActivity 클래스를 서술한뒤 startActivity 메소드를 통해 넘어가야합니다.
하지만 Acitivty Split을 사용한다면?
start<bActivity>()
넘어가려고 할 bActivity만 <> 안에 써준다면 끝이죠!
2줄 짜리 코드를 Split 라이브러리를 통하면 이렇게 간단하게 깔끔한 한줄짜리 코드로도 실행 가능한 점!!!
2줄 짜리 코드를 한줄로 하는게 모 별거라고..라고 생각될 수도 있지만 앞으로 이 Acitivity Intent 코드는 수 없이 쓰게 될텐데 저렇게 짧게 코드를 변경하는 것만으로도 행복함을 느낄 수 있습니다 :)
하나 더 Toast Split에 대해서 알아볼까요?
안드로이드 Toast 란?
앱을 사용하다보면 하단에 위의 이미지처럼 팝업 글씨가 보일 때가 있습니다.
이것을 toast 라고 하는데요, 이 toast를 사용하기 위해서는
Toast.makeText(this@MainActivity, "Hi There! This is a Toast.", Toast.LENGTH_SHORT).show()
이렇게 길게 (현재Activity, 토스트에 나올 TEXT값, 보여지는 시간).show() 를 입력해줘야 합니다.
하지만 toast spilt을 사용한다면?!
toast("Hi There! This is a Toast.")
아주 간단하죠?
안드로이드 스튜디오 내에서 사용되는 로그에 익숙해지기전에 안드로이드 초보 개발자들이 많이 쓰는 것이 toast 입니다. 그렇기 때문에 항상 toast를 쓰기위해서 저렇게 긴 줄을 보다는 짧고 간결한 한줄로 끝내는 마법이 필요한 것이죠!!
그렇다면 사용법은?
1. Splitties 깃헙에 들어가 어떤 라이브러리를 쓸 지 선택한다.
2. 선택한 라이브러리 화면에 (Activity Split 예제)
implementation("com.louiscad.splitties:splitties-activities:$splitties_version")
splitties_version은 현재 최신버전을 찾는다.
3. build.gradle 폴더에
사용할 라이브러리를 넣고 우측 상단에 프로젝트 Sync Now 클릭!!
4. 클래스 페이지에서 바로 사용한다.
이 외에도 유용한 라이브러리들이 많으니 하나씩 사용해보고 익숙해지도록 많이 사용해보시길 바랍니다 :)
부디 Splitties는 영원하기를!!!!!!!!!
'Development > Android (Kotlin)' 카테고리의 다른 글
Kotlin 카메라 & 갤러리 사진 처리하기1 (Old Camera1 Ver.) (1) | 2020.06.11 |
---|---|
Kotlin 안드로이드의 꽃, 애니메이션 Lottie 사용법! (1) | 2020.04.21 |
안드로이드 스튜디오 설치하기 (0) | 2020.03.10 |
카카오 로그인 연동하기 with Kotlin (0) | 2020.01.10 |
안드로이드 앱 개발은 Java? Kotlin? (0) | 2020.01.03 |
댓글