Related Articles:
Elegant Kotlin Development for Android Applications with Demo
A Comprehensive Guide to Kotlin with Excellent Open Source Kotlin Android Projects and Videos
Kotlin Learning Resources with Exclusive Learning Videos
The Google IO conference is ongoing, and the biggest news for Android is the recognition of Android O and Kotlin by the official team. I discovered that there is also an official architecture library release and a recommended usage guide, which I want to share with everyone.
Architecture Principles
-
Separation of Concerns
-
Model-driven UI, prioritizing persistent models
New Architecture
As shown in the figure above, the new architecture pattern consists of: Activity/Fragment UI layer, typically Activity/Fragment, listens to ViewModel, refreshes UI when ViewModel data updates, and listens to user events to provide feedback to ViewModel.
ViewModel Holds data or retrieves data needed by the UI layer from the Repository, responds to UI layer events, executes corresponding operations, responds to changes, and notifies the UI layer.
Repository The complete data model of the app, the object interacted with by ViewModel, provides a simple interface for data modification and retrieval, and synchronizes network data updates with local persistent data updates.
Data Source Includes local databases, network APIs, etc. This is quite similar to existing MVVM and Clean architecture combinations, but Google has provided some new libraries to help us implement this architecture.
Google’s New Toys During the local IO conference, Google provided new libraries to implement this functionality. I refer to them as new toys because this library is currently in alpha version 1, and the official recommendation is to use it only in personal small projects.
This library includes the following: Lifecycle Android lifecycle callbacks that help us separate code originally needed in onStart() and other lifecycle callbacks from Activity or Fragment.
LiveData A data holder class that holds data and can be observed. Unlike other Observers, it is bound to the Lifecycle.
ViewModel
Room A Sqlite ORM library launched by Google, which simplifies database operations significantly using annotations.
Framework Supplement
Testing Reasonable layering between each layer provides great convenience for testing.
UI Layer Testing Using Android Instrumentation Test, aided by the Espresso library, with a Mock ViewModel to focus on UI testing.
ViewModel Testing Using a Mock Repository to provide data, using JUnit tests, because it does not involve the UI, the running speed will be much faster.
Repository Testing Mock some data to return to the Repository for testing with JUnit.
Data Layer Testing Using JUnit tests. If using Room, the official provides testing support, creating an in-memory database during testing.
For network requests, use MockWebServer to provide a fake server.
Additionally, here’s a project structure diagram from the conference, taking a user information page as an example. Final Thoughts
This article will not include related code; please refer to the official documentation for the usage of various libraries: https://developer.android.com/topic/libraries/architecture/guide.html Here’s the official DEMO project: https://github.com/googlesamples/android-architecture-components
For more learning materials, click on the “Read Original” below.