The sample cover the following topics:
- Tips and Tricks using dependency injection in Android projects with Dagger & Hilt.
- UI Testing with Hilt Testing
- Screenshot Testing with Hilt Testing
- Flow & StateFlow
I collected tips and tricks that will help you get approaches used to apply DI in android apps.
Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically.
You can see my Hilt implementation on this example:
- Hilt is stable! Easier dependency injection on Android
- hilt-android - doc
- dependency injection on android with hilt - blog
- hilt - Android Dependency Injection - talk
- migrating to Hilt - doc
- android Hilt - codelab
- migrating your Dagger app to Hilt - codelab
- an opinionated guide to Dependency Injection on Android (ADS '19) - talk
- hilt — Adding components to the hierarchy - blog
- hilt-dagger.dev - doc
- android-hilt - example
- android-dagger-hilt - example
There are a few ways you can create a ViewModel instance. Since it’s a lifecycle-aware component, it needs a reference of the lifecycle owner such as an activity or fragment, so the samples contain some practices commonly used on the Android community.
Goal | Example |
---|---|
Basic implementation using ViewModelProvider.Factory to instantiate ViewModels. | dagger-viewmodel-factory |
Classic implementation to provide dependencies using constructor injection and modules | dagger-viewmodel-module |
Avoid arguments in Dagger - Modules using BindsInstance | dagger-viewmodel-binds-instance |
Provides a ViewModel using Multibindings | dagger-viewmodel-multibindings |
(At this time probably you're an expert using dagger but if not these are great resources to deep in.)
- dependency Injection the masterpiece on Android - slides
- dagger - doc
- dagger google - talk
- dagger basics - android-doc
- dagger-android - example
- dagger-mvp - example
- dependency Injection - blog-spanish
- flexible dependency - slides
./gradlew executeScreenshotTests
./gradlew connectedAndroidTest
./gradlew connectedCheck executeScreenshotTest
Maybe you noticed some test are pretty similar as:
showEmptyViewWhenThereAreNotPlayersScreenShot & showEmptyViewWhenThereAreNotPlayers
Instrumental Test
showEmptyViewWhenThereAreNotPlayers: is ensuring the view is shown
Screenshot Test
showEmptyViewWhenThereAreNotPlayersScreenShot: is ensuring the view is shown in the correct place on the screen
Yes, probably you can replace an instrumental test with a screenshot test in some scenarios in another ones you can be benefited from both.
Feel free to report or add any useful feature, I will be glad to improve it with your help.
- Erik Jhordan Rey - erikjhordan.rey@gmail.com
Copyright 2020 Erik Jhordan Rey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.