Living on a green rocket field - DevFest Istanbul 2018

2e100380ab0a1ba03a340932fc6e77ef?s=47 Hasan Hosgel
November 04, 2018

Living on a green rocket field - DevFest Istanbul 2018

At Google IO 2018 the Android team announced Jetpack for Android, which is an opinionated set of libraries and architecture, which is a recommendation for Android developers. We wanted to share our experience with Jetpack and also give some recommendations for all the library & SDK developers.

2e100380ab0a1ba03a340932fc6e77ef?s=128

Hasan Hosgel

November 04, 2018
Tweet

Transcript

  1. @alosdev DevFest Istanbul 2018 Living on a green rocket field

    Hasan Hosgel
  2. @alosdev DevFest Istanbul 2018 History

  3. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  4. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  5. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat 2013/07 - AppCompat, Media Router Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  6. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat 2013/07 - AppCompat, Media Router 2014/10 - RecyclerView, CardView Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  7. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat 2013/07 - AppCompat, Media Router 2014/10 - RecyclerView, CardView 2015/08 - bunch of new libraries Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  8. @alosdev DevFest Istanbul 2018 2011/03 - 1st version Support Library

    with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat 2013/07 - AppCompat, Media Router 2014/10 - RecyclerView, CardView 2015/08 - bunch of new libraries 2016/08 - split support libs Timetable source: https://developer.android.com/topic/libraries/support-library/revisions
  9. @alosdev DevFest Istanbul 2018 Timetable 2011/03 - 1st version Support

    Library with Fragments - Parallel ActionBarSherlock 2012/03 - Notification Compat 2013/07 - AppCompat, Media Router 2014/10 - RecyclerView, CardView 2015/08 - bunch of new libraries 2016/08 - split support libs 2017/06 - multidex … source: https://developer.android.com/topic/libraries/support-library/revisions
  10. @alosdev DevFest Istanbul 2018 What now?

  11. @alosdev DevFest Istanbul 2018 The King is dead - long

    live the king
  12. @alosdev DevFest Istanbul 2018 Welcome Jetpack

  13. @alosdev DevFest Istanbul 2018 android.support.v4.*** -> androidx.*** Except com.google.android.material.***

  14. @alosdev DevFest Istanbul 2018 gradle.properties android.useAndroidX = true android.enableJetifier =

    true
  15. @alosdev DevFest Istanbul 2018 Jetpack components • Foundation • Architecture

    • Behaviour • UI
  16. @alosdev DevFest Istanbul 2018 Foundation libraries • AppCompat • Android

    KTX • MultiDex • Test NEW
  17. @alosdev DevFest Istanbul 2018 Android KTX Kotlin Extension for a

    bunch of jetpack libraries: • Core-ktx • Fragment-ktx • Lifecycle-ktx • Navigation-ktx • …
  18. @alosdev DevFest Istanbul 2018 Architecture libraries • DataBinding • Lifecycles

    • LiveData • Navigation • Paging • Room • ViewModel • WorkManager NEW NEW NEW
  19. @alosdev DevFest Istanbul 2018 Navigation • Navigation Controller • new

    resource type navigation • “easily” define the navigation in a wysiwyg editor ◦ also via xml
  20. @alosdev DevFest Istanbul 2018 Navigation graph <navigation android:id="@+id/main_navigation_graph" app:startDestination="@id/listFragment"> <fragment

    android:id="@+id/listFragment" android:name="ist.devfest.navigation.ListFragment" android:label="fragment_list" tools:layout="@layout/fragment_list" /> <fragment android:id="@+id/detailFragment" android:name="ist.devfest.navigation.DetailFragment" android:label="fragment_detail" tools:layout="@layout/fragment_detail" /> </navigation>
  21. @alosdev DevFest Istanbul 2018 Layout file <android.support.constraint.ConstraintLayout …> <fragment android:id="@+id/main_nav_host_fragment"

    android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/main_navigation_graph" /> </android.support.constraint.ConstraintLayout>
  22. @alosdev DevFest Istanbul 2018 Navigation Editor list detail

  23. @alosdev DevFest Istanbul 2018 Navigation graph <navigation android:id="@+id/main_navigation_graph" app:startDestination="@id/listFragment"> <fragment

    android:id="@+id/listFragment" … > <action android:id="@+id/action_to_detailFragment" app:destination="@id/detailFragment" /> </fragment> <fragment android:id="@+id/detailFragment" … /> </navigation>
  24. @alosdev DevFest Istanbul 2018 Navigation Editor list detail

  25. @alosdev DevFest Istanbul 2018 Using NavigationController view.findNavController() .navigate(R.id.action_to_detailFragment) button.setOnClickListener( Navigation.createNavigateOnClickListener(

    R.id.action_to_detailFragment, null ) ) toolbar.setupWithNavController(navController) collapsingToolbarLayout.setupWithNavController(navController) navigationView.setupWithNavController(navController) bottomNavigationView.setupWithNavController(navController)
  26. @alosdev DevFest Istanbul 2018 Parameterized navigation <fragment android:id="@+id/detailFragment" android:name="ist.devfest.navigation.DetailFragment" android:label="fragment_detail"

    tools:layout="@layout/fragment_detail"> <argument android:name="sessionId" app:argType="string" android:defaultValue="rocket_field" /> </fragment> var bundle = bundleOf("sessionId" to "green_field") view.findNavController().navigate(R.id.action_to_detailFragment, bundle)
  27. @alosdev DevFest Istanbul 2018 Parameterized navigation with TypeSafety Build.gradle: …

    apply plugin: 'androidx.navigation.safeargs' … Caller side: val action = actionToDetailFragment().setSessionId("green_field") findNavController().navigate(action) Consumer side: DetailFragmentArgs.fromBundle(bundle).sessionId
  28. @alosdev DevFest Istanbul 2018 deeplinking nav_graph: <fragment android:id="@+id/detailFragment" … >

    <deepLink android:autoVerify="true" app:uri="www.devfest.istanbul/session/{sessionId}" /> </fragment> </navigation> AndroidManifest: <activity android:name=".MainActivity"> <nav-graph android:value="@navigation/main_navigation_graph"/> </activity> …
  29. @alosdev DevFest Istanbul 2018 deeplinking val deepLink = findNavController().createDeepLink() .setDestination(R.id.detailFragment)

    .setArguments(args) .createPendingIntent() val builder = NotificationCompat.Builder( context, channelId) .setContentTitle("Navigation") .setContentText("Deep link to Android") .setSmallIcon(R.drawable.ic_clock) .setContentIntent(deepLink) .setAutoCancel(true)
  30. @alosdev DevFest Istanbul 2018 Navigation in Multi Modules/ - Features

    base A B C app
  31. @alosdev DevFest Istanbul 2018 Navigation in Multi Modules/ - Features

    base A B C app feature_a_nav feature_b_nav feature_c_nav main_nav main_nav base_nav
  32. @alosdev DevFest Istanbul 2018 base/main_nav <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_nav"> <include

    app:graph="@navigation/base_nav" /> <activity android:id="@+id/feature_a" app:data="https://navigation.devfest.istanbul/a" app:action="android.intent.action.VIEW"/> … </navigation>
  33. @alosdev DevFest Istanbul 2018 base/base_nav <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/base_nav"> <action

    android:id="@+id/action_global_feature_a" app:destination="@+id/feature_a"/> … </navigation>
  34. @alosdev DevFest Istanbul 2018 a/feature_a_nav <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/feature_a"> <fragment

    android:id="@+id/action_global_feature_a" … > <deepLink app:uri="navigation.devfest.istanbul/a"/> </fragment> </navigation>
  35. @alosdev DevFest Istanbul 2018 app/main_nav <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:startDestination="@+id/base_nav" android:id="@+id/main_nav">

    <include app:graph="@navigation/base_nav" /> <include app:graph="@navigation/feature_a_nav" /> <include app:graph="@navigation/feature_b_nav" /> <include app:graph="@navigation/feature_c_nav" /> </navigation>
  36. @alosdev DevFest Istanbul 2018 Paging • PagedList as data object

    • DataSource to serve PageList • PagedListAdapter for RecyclerView
  37. @alosdev DevFest Istanbul 2018 Paging • PagedList as data object

    • DataSource to serve PageList • PagedListAdapter for RecyclerView Attention: if you want to manipulate the List itself that is not the way to go
  38. @alosdev DevFest Istanbul 2018 WorkManager • Successor of JobScheduler, JobDispatcher

    & AlarmManager • Not depending on Google play services (GMS) ◦ Usage outside of Google ecosystem • Defining constraints, when to run them ( charging, network, etc.)
  39. @alosdev DevFest Istanbul 2018 Worker class CompressWorker(context : Context, params

    : WorkerParameters) : Worker(context, params) { override fun doWork(): Result { // Do the work here myCompress() // Indicate success or failure with your return value: return Result.SUCCESS // (Returning RETRY tells WorkManager to try this task again // later; FAILURE says not to try again.) } }
  40. @alosdev DevFest Istanbul 2018 Constraint // Create a Constraints object

    that defines when the task should run val myConstraints = Constraints.Builder() .setRequiresDeviceIdle(true) .setRequiresCharging(true) // Many other constraints are available, see the // Constraints.Builder reference .build()
  41. @alosdev DevFest Istanbul 2018 Running them // ...then create a

    OneTimeWorkRequest that uses those constraints val compressionWork = OneTimeWorkRequestBuilder<CompressWorker>() .setConstraints(myConstraints) .build() OR val compressionWork = PeriodicWorkRequestBuilder<CompressWorker>(12, TimeUnit.HOURS) .setConstraints(myConstraints) .build() // Then enqueue the recurring task: WorkManager.getInstance().enqueue(compressionWork)
  42. @alosdev DevFest Istanbul 2018 Worker chains val chain1 = WorkManager.getInstance()

    .beginWith(workA) .then(workB) val chain2 = WorkManager.getInstance() .beginWith(workC) .then(workD) val chain3 = WorkContinuation .combine(chain1, chain2) .then(workE) chain3.enqueue()
  43. @alosdev DevFest Istanbul 2018 Behaviour libraries • Download Manager •

    Media & Playback • Notifications • Permission • Sharing • Slices NEW
  44. @alosdev DevFest Istanbul 2018 Slices • Currently not visible for

    users, but you can be first movers • Like widgets for the home screen, just in other apps • Supported API level 19 (KitKat) onwards • Can be used with Actions
  45. @alosdev DevFest Istanbul 2018 UI libraries • Animation & transitions

    • Auto • Emoji • Fragment • Layout • Palette • TV • Wear OS by Google
  46. @alosdev DevFest Istanbul 2018 Jetpack - sample app https://github.com/googlesamples/android-sunflower

  47. @alosdev DevFest Istanbul 2018 Wishlist for libraries/ SDKs • Don’t

    force your user to a specific marshaller (GSON, Jackson, Moshi) ◦ Offer the functionality like Retrofit and the ConverterFactory • Don’t force your user to use Rx- there is a cool guy around => Coroutine ◦ Droidcon Panel discussion “Rx must die!?” • Don’t depend on bigger libraries, especially if just use a small part of it • Add proguard config in your Jar/ Aar and don’t widen it to much
  48. @alosdev DevFest Istanbul 2018 Any Questions

  49. @alosdev DevFest Istanbul 2018 slides https://bit.ly/rocket-field-ist