Assembling your Jetpack – Android Conference Talk

hi I'm Tim song a developer programs engineer on the Android devrel team in this video we'll explore all about jetpack a suite of tools libraries and guidance to make high quality Android development easier well talk about Android X the packaging name for check back and then take a tour of sunflower a gardening app illustrating Android development practice practices with jetpack what is jetpack jetpack helps you accelerate development the components are individually adoptable but built to work together while taking advantage of Kotlin language features that make you more productive it helps you eliminate boilerplate code Android jetpack manages tedious activities like background tasks navigation and lifecycle management so we can focus on what makes your app great finally it helps you build high quality robust apps built around modern design practices Android jetpack components enable fewer crashes and less memory leaked with backwards compatibility baked in it is the recommended way of creating Android apps the jetpack components are divided into four groups foundation architecture behavior and UI the foundation components provide cross-cutting functionality like backwards compatibility testing and Kotlin language support architecture components help you design robust testable apps components include room which helps you manage your database work manager for scheduling background work and navigation which helps you navigate between pages in your app behavior components help your app integrate with standard Android services like notifications permissions and sharing finally the UI components provide widgets and helpers to make your app not only easy but delightful to use they also help you develop for other form factors such as Auto TV and wearables so that's a brief overview of jetpack now let's about how to use that in your app for that let's talk about Android X Android X is the package name and name space for jetpack libraries if fully replaces the support library and just like the support library Android action ships separately from the Android operating system and provides backward compatibility across Android releases unlike the Android operating system which is updated on a yearly basis jetpack libraries are updated on a roughly two-week cadence now why should you use Android X new feature development only happens on Android X as the old support library is deprecated if your app is still using the old support library you'll need to migrate to Android X to use the jetpack libraries there are a few ways to migrate in Android studio there's a migrate to Android X tool to update the old support libraries to their new Android X versions this tool works across your entire project so you don't need to run it per module we recommend using this method to migrate to Android X if you're not using Android studio or just prefer to script your migration there are CSV files on our developer website that map's each support library to its new Android X equal n't library here are some tips for a smoother migration first make sure the dependencies you're using are compatible with Android X those libraries that don't yet support Android X will prevent your app from migrating to Android X the best way to determine of a dependencies compatible is to migrate your project and check if there are any build errors additionally if the dependency is open-source visit the project's page to check on its Android X compatibility status next make sure your project is using support library 28 before starting the migration if you're upgrading from support library 26 27 or an older version the migration is much less likely to succeed so upgrade and steps first from the older version of the support library to version 28 and then from version 28 to Android X support library version 28 is the last version of the support library and is a binary equivalent to Android X 1.

0 thus minimizing the number of conflicts that can occur during migration finally pause or at least slow down development during the migration I know this is a rather big request especially for larger development teams the reason behind this is that the migration will change the majority of your files and your project which can result in a lot of code conflicts with feature branches thus if we're able to slow down development or work on a separate branch the number of merge conflicts that the team will need to resolve will be greatly reduced so now that we've explored what jet pack is let's see how it can be used in an app for that we'll talk about sunflower and the inspiration behind the app gardening gardening is a hobby of mine it's a very relaxing to spend time outdoors enjoying the sights and smells of the flowers and here the busy bees and with some patience I can literally enjoy the fruits of my labor but sometimes there are just too many plants for me to keep track of and sometimes a pot of flowers that gets crawled away is easily forgotten then later I'd rediscover it and it hasn't been watered for weeks and because of that I chose to make sunflower a gardening app as a sample for this talk here are some goals that sunflower aims to achieve the app has a catalog of plants that the user can indicate is in their garden the user can choose from those and add plants to the garden to keep track of and the app can show when it was added finally the app can keep track of watering schedules letting the user know when each plant was last watered and when it needs watering next based on each plant's unique watering needs an established food tree may only need water once a month but a tomato plant in the middle of summer might need a daily dose of water so let me introduce Android sunflower an open source gardening sample app illustrating Android development best practices with jetpack let's take a quick tour of the app when the user first launches the app they're taken to the my garden page which is currently empty there's a call to action for the user to tap on the add plant button tapping on that will open the plant list these are a list of plants that are in the apps catalog and that the user can add to their garden let's tap on the plant to see more details here in the plant detail page we can see the plants name description watering needs and a header image of the plant there's also a share button and a floating action button that when tapped will add the plant to your garden a snack bar will briefly appear confirming the addition after adding a few more plants that my garden page will look something like this each plant card shows when it was added to your garden along with when it was last watered it also gives a reminder for each plant on when it next needs to be watered the app has both a light and a dark theme recent versions of the Android operating system has a system-wide toggle between light and dark themes allowing apps to switch themes based on this setting here's a quick overview of sunflowers design sunflower is written entirely in Colin and takes advantage of Kotlin language features such as co-routines and Android KTX Colin extensions for jetpack libraries it is written with the model view viewmodel architecture or and the VM for short sunflower follows the principles of navigation where many fragments are attached to a single activity and finally it is an open-source project sunflower is one of androids flagship open-source samples and is hosted on github and developed in the open sunflower uses several jetpack components but will focus on just a handful for the rest of this presentation room live data view model data binding and navigation now let's take a look at some code first we'll need a way to represent our plant data so let's create a Kotlin data class to capture this the class provides a unique ID of the plant its name and description a watering interval for how often each plant needs to be watered with the default of seven days and a nice URL for a picture of the plant to persist this data we have a few options Android provides a variety of storage types in memory shared preferences on the filesystem and in the database for our needs we want to use a database due to the need for structure around the data for that we'll use the room jetpack component an abstraction layer over sequel Lite for more robust database access we can add a few annotations so that the class can be used by a room adding the entity annotation tells room that this is a table with the name plants we also indicate which column contains the primary key and the name of the associated column if no column name is specified room will use the variable name as the column name to access and store data in the database we'll use a dowel a data access object here and the plant dowel we have two methods that return plant data from the database and an insert method that stores data notice that each of the two get methods have an annotation that contains the sequel query now the israel method doesn't have a query as the insert annotation will generate the appropriate query depending on if the data is being inserted is a list of plants or a single plant we do state that if there's a conflict on data insertion the new data will replace the old data the get methods also return live data objects which we will cover later in this talk next we'll extend the room database class to customize our database note the list of entities at the top which contains the plant class from earlier also in this file are a list of dows including our plant Dow this is the main entry point for the app to interact with the database now let's move up to the repository the repository is a gateway brief in the data layer and the view model and sunflower the repository uses a database but this can be extended to use other sources such as data from the network or the filesystem a single repository can be used by multiple view models for example we have different view models for the plant list page and the plant details page which would use the get plants and get plant methods respectively the view model is another jetpack component providing the bridge between business logic and the UI is to sign the store and manage UI related data that is lifecycle aware the view model class allows data to survive configuration changes such as screen rotations why data is also a jetpack component and is an observable data holder class that is lifecycle aware it respects the life cycle of other app components such as activities fragments and services and only updates those components that are in an active life cycle here in our plant detail view model we have a live data bouillon called is planted that represents if a plant is added to our garden we also have a live data that wraps a plant object to store the result from querying the database for a specific plant can tuning up the stack we now examine the plant detail fragment here we get the plant detail view model and then use data binding to surface it to the UI data binding another jetpack component lets you bind the layout UI to data sources using a declarative format rather than programmatically let's examine this further here's a screenshot of the plant details page let's focus on the plants name and the floating action button here's a layout for the plant details page and here we define a data binding variable that is used in the rest of the layout let's return to the plant detail fragment where we fetched the plant detail view model we'll use the data binding util class to inflate our layout and assign it to a variable since we want to use flight data with data binding we'll need to set a lifecycle of the binding in this case is the fragment itself finally we set the bindings view model variable to the plant detail view model we fetched earlier let's go back to the plant detail layout file and examine the text view here we're using the view model data binding variable grabbing the plant from the view model and then the plants name to use as text attribute thus we're able to populate the text view without having to use fine view by ID and then set text now we have a slightly more complex example for the floating action button we're also using the view model data binding variable as before but this time we're using it with the binding adapter passing in the Miss planted variable which is a lie data wrapped boolean here's the binding adapter note the annotation that declares the binding adapters name is used by the layout file inside this binding adapter we define the logic for programmatically showing or hiding the floating action button thus when the is planted variable changes after the plant is added to our garden the floating action button is automatically hidden using this logic this isn't just about moving code a binding adapter can be used for other views helping simplify your code base from duplicate code we've now explored how data of surface from the database all the way to the UI next let's explore how we navigate through our app let's recap on the pages of the sunflower app the main page uses a view pager with the garden and plant lists and then there's a separate plant details page sunflower uses a view pager too and navigation jetpack components view pager too provides the view paging needs and has advantages over the old view pager one library such as built-in right-to-left support and vertical orientation support the navigation component helps provide a consistent API when navigating between destinations whether they are fragments activities or other components now let's examine the navigation component a bit more here is the layout file for the garden activity the single activity inner app in here will declare a fragment and the navigation graph associated with it note that if there are multiple activities in an app then each activity that's used with navigation will need its own navigation graph here is the navigation graph filed with two fragments let's analyze the file piece-by-piece first we had the viewpager fragment that houses the my garden and plant list pages it has an ID a viewpager fragment which is also used as the start destination of the graph the start destination indicates which fragment appears when the app is launched the viewpager fragment also has an action that defines a navigation direction in this case we can navigate from the viewpager fragment to the plant detail fragment using a defined ID action view pager to plant detail finally the entry for the plant detail fragment includes an argument section which includes the plant ID as a stream here's the adapter that's associated with a list of plants we can use the navigation Direction defined in a navigation graph and pass it the ID of the plant or navigating to and then assign all of this to a direction variable then we find the navigation controller and navigate to set direction since we defined the plant ID as an argument in your navigation graph the build will throw an error if it's not included in the navigation direction this provides better error checking that catches errors with navigation at compile time rather than at runtime when not using the jetpack navigation finally and the plant detail fragment were able to grab the plant ID from the list of arguments which can then be passed down to the v-model down to the repository and then down to the dowel which will then return a live data a plant for use in the UI that's a quick overview of sunflower and how jetpack components work together in an android app now this just scratches the surface of many of these components so to learn more and dig deeper into specific jetpack components here are some online resources to help you out our developer site DDOT android.

com slash jet pack contains guides release notes and API reference stocks for our jetpack components all of the code used in his presentation is available in the sunflower repository located at github comm / Android / sunflower thank you for watching and best of luck adding jet pack to your Android app did you enjoy this video please click like subscribe to the Android developers youtube channel to learn more about Android development you.

Source: https://zika-news.com

See More Android: https://zika-news.com/category/android/

Hãy bình luận đầu tiên

Để lại một phản hồi

Thư điện tử của bạn sẽ không được hiện thị công khai.


*