Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevFest Songdo 2019 ~ 최신 AndroidX 체크

DevFest Songdo 2019 ~ 최신 AndroidX 체크

DevFest Songdo-Incheon 2019에서 발표한 자료입니다.

pluulove (노현석)

November 30, 2019
Tweet

More Decks by pluulove (노현석)

Other Decks in Programming

Transcript

  1. ୭न AndroidX ୓௼ Google Developer Experts for Android GDG Korea

    Android Organizer Naver Noh HyeonSeok GDG Songdo - Incheon
  2. Index • Introduction AndroidX • Android Components • AndroidX in

    Android Dev Summit • Migration GDG Songdo - Incheon
  3. Question?! 1. AndroidXܳ ٜযࠌ׮? 2. Jetpackਸ ঌҊ੓׮? 3. Support Libraryо

    ӝরդ׮? 4. Android Architecture Components? 5. ੉ٜ੄ ର੉੼ਸ ঌҊ੓׮?
  4. AndroidX Android ౱੉ Jetpack ղীࢲ ۄ੉࠳۞ܻܳ ѐߊ, పझ౟, ಁః૚, ߡ੹

    ҙܻ, ୹दೞחؘ ࢎਊೞח য়೑ࣗझ ೐۽ં౟ੑפ׮. https://developer.android.com/jetpack/androidx
  5. Updated packaging • Android java ࣗझܳ ಁః૑߹۽ ܻಂష݂ ▪ Android.<feature>.ClassName

    • Maven ▪ androidx.<feature>:<feature>-<sub-feature> ▪ ೙ਃೠ ӝמ߹ ߓನ • -v7, -v4 ಁః૑ݺ ઁѢ
  6. Activity & Fragment & Navigation - Activity 1.0.0(Stable) , 1.1.0-rc02(RC)

    - Fragment 1.1.0(Stable) , 1.2.0-rc02(RC) - Navigation 2.1.0(Stable) , 2.2.0-rc02(RC)
  7. class OldFragment : Fragment() { // Crazy fun backStart() {

    requireActivity().onBackPressed() } } җѢ Fragmentীࢲ onBackPressed // Crazy fun backStart() { requireActivity().onBackPressed() } // Safe override fun onAttach(context: Context) { super.onAttach(context) backListener = context as? BackListener backListener?.onBackPressed() }
  8. class MainFragment : Fragment() { private val dispatcher by lazy

    { requireActivity().onBackPressedDispatcher } private val backPressedCallback = object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { // TODO } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) dispatcher.addCallback(this, backPressedCallback) } } Fragmentীࢲ onBackPressed ୊ܻ Back Pressed Flag
  9. // ComponentActivity.java public class ComponentActivity { private final OnBackPressedDispatcher mOnBackPressedDispatcher

    = new OnBackPressedDispatcher(new Runnable() { @Override public void run() { // fallbackOnBackPressed ComponentActivity.super.onBackPressed(); } }); @Override @MainThread public void onBackPressed() { mOnBackPressedDispatcher.onBackPressed(); } } // OnBackPressedDispatcher.java public final class OnBackPressedDispatcher { @MainThread public void onBackPressed() { Iterator<OnBackPressedCallback> iterator = mOnBackPressedCallbacks.descendingIterator(); while (iterator.hasNext()) { OnBackPressedCallback callback = iterator.next(); if (callback.isEnabled()) { callback.handleOnBackPressed(); return; } } if (mFallbackOnBackPressed != null) { mFallbackOnBackPressed.run(); } } } ComponentActivity / BackPressedDispatcher ൒ܴ
  10. // ComponentActivity.java public class ComponentActivity { private final OnBackPressedDispatcher mOnBackPressedDispatcher

    = new OnBackPressedDispatcher(new Runnable() { @Override public void run() { // fallbackOnBackPressed ComponentActivity.super.onBackPressed(); } }); @Override @MainThread public void onBackPressed() { mOnBackPressedDispatcher.onBackPressed(); } } // OnBackPressedDispatcher.java public final class OnBackPressedDispatcher { @MainThread public void onBackPressed() { Iterator<OnBackPressedCallback> iterator = mOnBackPressedCallbacks.descendingIterator(); while (iterator.hasNext()) { OnBackPressedCallback callback = iterator.next(); if (callback.isEnabled()) { callback.handleOnBackPressed(); return; } } if (mFallbackOnBackPressed != null) { mFallbackOnBackPressed.run(); } } } ComponentActivity / BackPressedDispatcher ൒ܴ
  11. // ComponentActivity.java public class ComponentActivity { private final OnBackPressedDispatcher mOnBackPressedDispatcher

    = new OnBackPressedDispatcher(new Runnable() { @Override public void run() { // fallbackOnBackPressed ComponentActivity.super.onBackPressed(); } }); @Override @MainThread public void onBackPressed() { mOnBackPressedDispatcher.onBackPressed(); } } ComponentActivity / BackPressedDispatcher ൒ܴ // OnBackPressedDispatcher.java public final class OnBackPressedDispatcher { @MainThread public void onBackPressed() { Iterator<OnBackPressedCallback> iterator = mOnBackPressedCallbacks.descendingIterator(); while (iterator.hasNext()) { OnBackPressedCallback callback = iterator.next(); if (callback.isEnabled()) { callback.handleOnBackPressed(); return; } } if (mFallbackOnBackPressed != null) { mFallbackOnBackPressed.run(); } } }
  12. // ComponentActivity.java public class ComponentActivity { private final OnBackPressedDispatcher mOnBackPressedDispatcher

    = new OnBackPressedDispatcher(new Runnable() { @Override public void run() { // fallbackOnBackPressed ComponentActivity.super.onBackPressed(); } }); @Override @MainThread public void onBackPressed() { mOnBackPressedDispatcher.onBackPressed(); } } // OnBackPressedDispatcher.java public final class OnBackPressedDispatcher { @MainThread public void onBackPressed() { Iterator<OnBackPressedCallback> iterator = mOnBackPressedCallbacks.descendingIterator(); while (iterator.hasNext()) { OnBackPressedCallback callback = iterator.next(); if (callback.isEnabled()) { callback.handleOnBackPressed(); return; } } if (mFallbackOnBackPressed != null) { mFallbackOnBackPressed.run(); } } } ComponentActivity / BackPressedDispatcher ൒ܴ
  13. // ComponentActivity.java public class ComponentActivity { private final OnBackPressedDispatcher mOnBackPressedDispatcher

    = new OnBackPressedDispatcher(new Runnable() { @Override public void run() { // fallbackOnBackPressed ComponentActivity.super.onBackPressed(); } }); @Override @MainThread public void onBackPressed() { mOnBackPressedDispatcher.onBackPressed(); } } // OnBackPressedDispatcher.java public final class OnBackPressedDispatcher { @MainThread public void onBackPressed() { Iterator<OnBackPressedCallback> iterator = mOnBackPressedCallbacks.descendingIterator(); while (iterator.hasNext()) { OnBackPressedCallback callback = iterator.next(); if (callback.isEnabled()) { callback.handleOnBackPressed(); return; } } if (mFallbackOnBackPressed != null) { mFallbackOnBackPressed.run(); } } } ComponentActivity / BackPressedDispatcher ൒ܴ
  14. ViewModel ୡӝച lifecycle-viewmodel private val viewModel by lazy { ViewModelProviders.of(this).get<MainViewModel>()

    } This constructor was deprecated in API level 1.1.0. This class should not be directly instantiated
  15. ViewModel ୡӝച lifecycle-viewmodel val vm: VM = ViewModelProvider(this, factory).get(VM::class.java) val

    vm: VM = ViewModelProvider(this).get(VM::class.java) public ViewModelProvider(@NonNull ViewModelStoreOwner owner) { this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory ? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory() : NewInstanceFactory.getInstance()); }
  16. Support Coroutines in ViewModel After lifecycle-viewmodel-ktx 2.1.0 val ViewModel.viewModelScope: CoroutineScope

    get() { val scope: CoroutineScope? = this.getTag(JOB_KEY) if (scope != null) { return scope } return setTagIfAbsent(JOB_KEY, CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)) } To be continue…
  17. #AndroidDevSummit @Composable fun FilteredList(contacts: List<Contact>, filterText: String = "") {

    val state = +state { filterText } VerticalScroller { Column { TextField(filterText, onValueChange = { state.value = it }) contacts.filter { it.contains(state.value) }.forEach { Row { Image(it.photo) Text(it.name) } } } } }
  18. #AndroidDevSummit • Simplify • Fix what’s broken • No more

    cheating • Material + Animations • Compatibility Jetpack Compose
  19. #AndroidDevSummit Android Studio Live preview, Apply Changes Compose Compiler Plugin

    Code generation extensions kotlinc Upstream Kotlin compiler (1.4) Compose UI Foundation Standard layouts, interactions Compose UI Core Input, Measure, Layout, Drawing Compose Runtime Tree management, Effects Compose UI Material Surface, Buttons, Tabs, Themes Compose Build time (development host) Runtime (on device)
  20. #AndroidDevSummit Compose UI Foundation Standard layouts, interactions Compose UI Core

    Input, Measure, Layout, Drawing Compose UI Material Surface, Buttons, Tabs, Themes BUTTON Gestures, accessibility, & other internals
  21. Jetpack Compose • I/O 19, Unbundled • Announced Android Studio

    4.0 Canary 1 in Android Dev Summit • Last Release 0.1.0-dev02
  22. #AndroidDevSummit Using FragmentScenario @Test fun testEventFragment() { val scenario =

    launchFragmentInContainer<MyFragment>() onView(withId(R.id.refresh)).perform(click()) scenario.onFrament { fragment -> // Check that the fragment handled the click correctly. } }
  23. #AndroidDevSummit Using FragmentScenario @Test fun testEventMoveToCreatedFragment() { val scenario =

    launchFragmentInContainer<MyFragment>() scenario.moveToState(State.CREATED) } @Test fun testEventFragment() { val scenario = launchFragmentInContainer<MyFragment>() scenario.recreate() }
  24. #AndroidDevSummit Using FragmentFactory private class MyFactory() : FragmentFactory() { override

    fun instantiate( classLoader: ClassLoader, className: String ) = when (className) { MyFragment::class.java.name -> MyFragment(...) else -> super.instantiate(classLoader, className) } } override fun onCreate(savedInstanceState: Bundle?) { supportFragmentManager.fragmentFactory = MyFactory() super.onCreate(savedInstanceState) }
  25. FragmentContainerView extends FrameLayout • Fragmentਊ Container ◦ AddView => IllegalStateException

    • <fragment> / <FrameLayout>ܳ ؀୓ • Z-index ޙઁ ೧Ѿ • 3о૑ ੘সਸ ૓೯ ◦ ࢜۽਍ Fragment ੋझఢझ ࢤࢿ ◦ Fragment.onInflate(Context, AttributeSet, Bundle) ഐ୹ ◦ FragmentTransactionਸ प೯ೞৈ FragmentManagerী ୶о
  26. ViewModel ୡӝച // activity-ktx val activityVM : ActivityViewModel by viewModels()

    // fragment-ktx val fragmentViewModel : MyViewModel by viewModels() val activityViewModel : MyViewModel by activityViewModels() // navigation-ktx val navGraphViewModel: MyViewModel by navGraphViewModels(R.id.main)
  27. ROOM #AndroidDevSummit • RxJava/Coroutines ૑ਗ • Raw ௪ܻ ૑ਗ •

    AndroidX ݃੉Ӓۨ੉࣌ • Full Text Search • Views • SQLiteী ؀ೠ ୶࢚ച ҅க ઁҕ • ஹ౵ੌ ఋ੐ী Query ୓௼ • Migration੉ ए਑ • పझ౟ೞӝ ए਑ • Last 2.2.2(Stable)
  28. #AndroidDevSummit Async queres Query Type Observable Read One-Shot Read /

    Write Lifecycle RxJava Coroutines LiveData Publisher, Observable, Flowable Flowable N/A Single, Maybe, Completale suspend fun New!
  29. Flow 101 - Kotlin coroutine primitive - Asynchronous - Cold

    stream • transform stream (e.g. map, filter) • consume data (e.g. collect, single, toList)
  30. Flow Example fun getDogsToPet(limit: Int = -1): Flow<Dog> = flow

    { var i = 0 while (limit < 0 || i > limit) { emit(getRandomDog()) i++ } }
  31. ୡӝ DB ؘ੉ఠ ࢸ஖ (җѢ) 1. Database ৌӝ 2. Schema

    Ѩૐ 3. Database File ਫ਼Ә 4. Thread زӝച 5. Content ࠂࢎ 6. Database ײӝ #AndroidDevSummit
  32. #AndroidDevSummit • Easy to get off main thread • Minimal

    boilerplate • Structured concurrency = managed jobs Coroutines ੢੼
  33. #AndroidDevSummit class MyActivity : Activity { override fun onCreate(state: Bundle?)

    { super.onCreate(state) lifecycleScope.launch { // Run } lifecycleScope.launchWhenResumed { // Run } } } Activities & Fragments lifecycleScope.launchWhenResumed { // Run }
  34. #AndroidDevSummit ViewModel + LiveData class MainActivityViewModel : ViewModel { private

    val _result = MutableLiveData<String>() val result = LiveData<String> = _result init { viewModelScope.launch { val computationResult = doComputation() _result.value = computationResult } } }
  35. #AndroidDevSummit liveData coroutine builder class MyViewModel { val result =

    liveData { emit(doComputation()) } } After lifecycle 2.2 liveData(Dispatchers.IO) { emit(LOADING.STRING) emitSource(dataSource.fetchWeather()) } 1 2 LiveData
  36. emit N Values val currentWeather: LiveData<String> = dataSource.fetchWeather() LiveData →

    LiveData Flow → LiveData val currentWeatherFlow: LiveData<String> = liveData { dataSource.fetchWeatherFlow().collect { emit(it) } }
  37. emit N Values LiveData → LiveData Flow → LiveData val

    currentWeather: LiveData<String> = dataSource.fetchWeather() val currentWeatherFlow: LiveData<String> = dataSource.fetchWeatherFlow().asLiveData()
  38. emit 1 + emit N Values val currentWeather: LiveData<String> =

    liveData { emit(LOADING_STRING) emitSource(dataSource.fetchWeather()) } LiveData → LiveData Flow → LiveData val currentWeatherFlow: LiveData<String> = liveData { emit(LOADING_STRING) emitSource( dataSource.fetchWeatherFlow().asLiveData() ) }
  39. emit 1 + emit N Values val currentWeather: LiveData<String> =

    liveData { emit(LOADING_STRING) emitSource(dataSource.fetchWeather()) } LiveData → LiveData Flow → LiveData val currentWeatherFlow: LiveData<String> = dataSource.fetchWeatherFlow() .onStart { emit(LOADING_STRING) } .asLiveData()
  40. Suspend transformation val currentWeather: LiveData<String> = dataSource.fetchWeather().switchMap { liveData {

    emit(heavyTransformation(it)) } } LiveData → LiveData val currentWeatherFlow: LiveData<String> = dataSource.fetchWeatherFlow() .map { heavyTransformation(it) } .asLiveData() } Flow → LiveData
  41. FIRST backup backup backup backup backup backup backup backup backup

    backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup backup
  42. Migration • Menu Bar Refactor > Migrate to AndroidX ࢶఖ

    • External Librariesী AndroidX ޷֢୹द Opt 1. Gradle Sync Opt 2. Invalidate Caches / Restart • 100% ߸ജ੉ উؼ ࣻ ੓ਵ޲۽ ѐੋ੸ਵ۽ ࣻز੉ উ੿੸
  43. Migration to AndroidX • Support Libraryח Deprecate • ঱ઃо ೧ঠೡ

    ੘স਷ ؊ ੉࢚ ইפ׮ • Release Android 10 (2019.09.03) • ࢲ࠺झ੄ ઱ਃ ױ݈੄ Android 10 সؘ੉౟ दӝ ୓௼
  44. Stable Activity AppCompat Lifecycle Navigation Room Work Autofill Benchmark Biometric

    Coordinator Layout Fragment Lifecycle ViewModel-SavedState Navigation RecyclerView ViewPager2 ConstraintLayout RC Beta Alpha Ads Browser Camera Car Compose Security