#AskAndroid: A Summary of the questions asked and answered at Android Dev Summit 2021
The amazing list of product offerings is not the only thing Android developers look forward to after Google I/O as there is also the live Q&A session known as #AskAndroid. The aim of this session is to afford developers around the world the opportunity to ask Modern Android Development (MAD for short) questions through the hashtag #AskAndroid on Twitter and in the session live chat which will be answered by the team from Google. This article aims to curate those questions asked and what the team at Google had to say.
This year’s #AskAndroid was hosted and moderated by Google Developer Expert and GoDaddy Principal Android Engineer, Rebecca Franks. She was joined by the team from Google consisting of Yigit Boyar (Yigit Boyar) from the Android Jetpack Team, Manuel Vivo (Manuel Vivo) from the Android Developer Relations Team, Amanda Alexander a Product Manager on Android, and Wojtek Kaliciński (@wkalic) from the Developer Relations Team.
Daya: “If someone is very interested in Android development and has no idea about Jetpack, what is your advice for them?”
Amanda took the question and shared that the purpose of Jetpack is to help developers build Android apps with modern design practices, accelerate development, reduce boilerplate, and code that is consistent across a wide range of devices and Android versions. In addition to this, resources, code-labs, and online training material are readily available on the website https://developer.android.com/jetpack for either adopting Jetpack into an existing or brand new app. Wojtek also shared that for beginners there are also training courses available to get started with programming such as the Android Basics in Kotlin course.
John: “What is the best way to collect flows from the Android UI?”
Manuel answered with “the best and safe way” would be to use the repeatOnLifecycle suspend function or alternatively the Flow.flowWithLifecycle flow operator. He added that the good thing about these APIs is that they are lifecycle-aware and will only collect from the flow whenever the User Interface (UI) is visible on the screen. He also wrote a detailed article about it here.
Max: “I have some performance problems in my app. Where do I start with debugging? What tools should I use?”
Amanda answered this question that there are two libraries available, Macrobenchmark to help measure startup, scrolling, or jank frame performance in your app, and Benchmark library is designed to measure CPU cost of specific functions. She added that these libraries can be used remotely to track metrics in continuation integration or locally where the profile results can be seen in Android Studio. Manuel also pointed out that there was a Performance Mad Skills series on the Android Developers YouTube channel which is a recommended watch.
Craig Petty: “When is the next Android Studio coming out?”
Wojtek started his answer by saying specific dates are normally not provided as releases can slip and thus the desire is to not give developers an expectation of a date that might change. However, he added that monitoring the Canary, Beta, and Release Candidate channels will provide more of a rough idea for when the next stable release might be. The stability of the release candidates where major bugs are found and fixed is the confidence they need in releasing to production. Furthermore, developers are encouraged to test the beta and pre-release versions and let the team know about what they find to not be working.
Ruif3r: “For new apps/features, do I learn Motion Layout over Compose?”
Yigit answered by saying Motion Layout and Jetpack Compose are different view systems, however, the recommendation is to choose Jetpack Compose especially for new apps as there is not much reason to choose the old view system unless the codebase is historical or legacy. He added that Jetpack Compose animations stand out and are a lot easier to implement than with the old view system. If there is a great need to use Motion Layout, it is possible to embed regular views inside Jetpack Compose applications due to it being fully interoperable.
Cypher: “What is preferred? MVVM or MVI?”
Manuel stated that there isn’t much difference between the two approaches — MVVM (Model-View-ViewModel) and MVI (Model-View-Intent) and both are a way to do the preferred unidirectional flow. Which one to choose is purely based on the needs of the application. He believes that either approach will set you up for success as each uses the aforementioned unidirectional flow.
Manju Reddy: “Best ways to communicate from update of data from repo to viewModel?”
Manuel answered this one as well with a succinct “Flows or suspend functions”. He added if it’s a one-shot call then suspend functions and if it's a stream of data then use Kotlin Flows. Yigit also shared that thinking of problems from the perspective of whatever has a smaller lifecycle that observes the one with a larger lifecycle will greatly help with dealing with cases such as the repository and the ViewModel and the views. Furthermore, he advised no longer using LiveData in favor of Flows due to LiveData’s inability to scale properly where Flows would be ideal.
Rahul Ray: “How to start multimodule apps using Hilt?”
Manuel advised that the best place to start would be the documentation and with regards to multi-modules it works as though you had everything in the app module because different modules could expose bindings to different components in the same way. He added that if the question was referring specifically to Dynamic Feature Modules, this is also covered in the documentation.
Besong-Anong Ernest: “Does background manager replace background service?”
Yigit started by saying that there are two important points for the way Work Manager was designed: for important tasks that should run as well as deferrable tasks. He then recommended using the regular executors if there is no concern for losing the work or if it's important to use Work Manager. Yigit also recommended taking a look at the Expedited Job API introduced to Work Manager in version 2.7 which is also detailed here. It also includes information about what is needed to adapt the application to support Android 12.
Stavros Georgiou: “Are there scenarios that need to be handled with LiveData than in Flow?”
Yigit answered that the biggest difference between LiveData and Flows is that LiveData has pre-imposed limitations “that make this API simple”. He then went on to share Jose Alcérreca’s article on ‘Migrating from LiveData to Flow’ which should help highlight which is ideal to use. Wojtek added that when LiveData was introduced it “was a simple choice”, however, more advanced technologies such as Kotlin Flows were introduced. Wojtek also recommended watching Manuel’s talk ‘Kotlin Flows in practice’ which will help with understanding Flows in Android better.
Siddharth Gupta: “How can we reduce build times in Android Studio?”
Wojtek acknowledged that this is a big topic like this is on the developers’ and Google’s minds which they are actively working on. He added that the build process is not just one thing but a combination of Gradle, its plugins, the compilers, other components that build and package resources, and that they are tackling the problem “one by one”. He further recommended staying on the latest versions of Android Studio as well as the Android Gradle Plugin (AGP) and all other plugins. The fixes or changes in these new releases may include bug fixes that addressed irregular cache invalidation, making the build more incremental as well as the speed itself. Wojtek also mentioned Kotlin Symbol Processing will also make the builds faster in certain scenarios. Wojtek went further to advise against “trying to do custom stuff in your build” if you don't really have to and to stick to the declarative Domain Server Language (DSL) style configuration.
Carlos A. PT: “Is there a way to transition from legacy pre-Jetpack apps into modern awesome Jetpack?”
Manuel advised choosing a really good architecture for the app and making the dependencies that the app depends on replaceable, for example, easily switching from SharedPreferences to Shared DataStore. Yigit also advised not to reinvent the wheel when creating a component while there is an accepted open-source or Jetpack library. He added that though a developer would feel comfortable with their own implementation or creation, a lot of work has gone into the different Jetpack libraries that enable it to work across older Android versions, handle interesting cases. He provided an example of foreground changes introduced in the Android updates where using WorkManager would involve making minor adjustments in order to adapt. Manuel then added that testing your application is very important in order to ensure that nothing is broken with changes introduced to the code. Yigit then supported this by saying that in addition to Android being open source, it also has a downstream branch and the tests that are run will help them detect if something is going to break in the next version of Android months ahead of release. Thus, their detection allows them to fix the issue well in advance.
Stefan Wärting: “Does ViewModels and Hilt/Dagger have a place in the Compose world?”
Manuel said they are actually recommending this and went on to highlight a talk ‘A Compose state of mind: Using Jetpack Compose’s automatic state observation’ which talks about different ways to handle state complexity in Compose. He added that the ViewModel is supposed to be the state holder that is in charge of providing the business logic of the app and providing the UI state for a particular screen. He also shared how ViewModels are a part of Compose Navigation.
Stefan Wärting: “Is there any bet on building multiplatform apps (iOS)” and “Can we use Room, Hilt, etc in KMM [Kotlin Multiplatform Mobile]?”
Yigit started by saying “not yet”. He added that they understand that developers are interested in the KMM technology and that they also use Jetpack, however, they do not want Jetpack to be a blocker for moving to KMM. He further stated that it is not straightforward for them to move a library to KMM, however, they are looking at the technology, using smaller Jetpack libraries to see how they work with KMM.
Bavnik Desai: “What is the best practice to handle network exception or other exception using Coroutine?”
Manuel answered by saying that the best way is to use or rely on the mechanism that coroutines provide to handle exceptions. He added that there are also articles that they have written around error handling as well as talks done in the past, however, reliance on the mechanism is what is recommended. Furthermore, he advised that knowledge on Supervisor Jobs or Supervisor Scopes versus Coroutines Scopes works would help make sense of it all.
Sajal Jain: “How to automate the process of publishing to the Play Store?”
Wojtek answered by saying there isn’t anything ready-made, for example, an Android Studio plugin, and acknowledged how this is something the developer community might need. However, he also mentioned that there is a third-party plugin combined with Android Gradle Plugin that can be used to automate this. He added they might add a plugin in the future but for now the aforementioned third-party plugin should suffice.
Nicolás David Muñoz Cuervo: “Is there a way to unit test the Pager class using a RemoteMediator? If so, where could I find examples?”
Yigit mentioned that they have plans to create a test output for Paging to address these cases because it relies on some internal APIs that they do not want to expose yet. He added that the ideal thing would be to unit test the RemoteMediator or to run integration tests on the actual Paging. Paging is very UI-dependent where it tries to optimize what is being shown in the UI and without having that part of the UI it is difficult to control. He further acknowledged that a testing library should be added and they are going to work on adding this.
Mihai: “What does Macrobenchmarking library do?”
Amanda answered this by saying that Macrobenchmarking allows the developer to set tests for both startup times for the app (if the desire is to test different stages of startup) such as cold start or warm start and it also helps test jank or the different frame performances of the app. For instance, if the app has a lot of scrolling then the intent should be to ensure that there are no dropped frames. The library also helps with tests for specific scenarios that are more in line with how users interact with the app. She added that they are also looking at other key performance metrics that could also be tracked and thus expand the library.
Mahesh Chunkhade: “Does DataStore support encryption?”
Yigit answered that DataStore does not currently have encryption out the box and they are thinking of creating that integration. However, he added that it is possible to provide the method of serialization of how it will be written to the disk allowing the developer to have their own encryption behind DataStore.
