Skip to content

Hilt is stable! Tips and Tricks Dagger & Hilt, UI Testing, Screenshot Testing, Flow and StateFlow.

Notifications You must be signed in to change notification settings

erikjhordan-rey/ATP-Rankings-Hilt-Dagger

Repository files navigation

Rankings ATP Tour Tennis - Hilt

Android-Health Actions Status

The sample cover the following topics:

Dependency Injection

I collected tips and tricks that will help you get approaches used to apply DI in android apps.

Hilt is stable!

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:

dagger-hilt-example

Learn Dependency Injection with Hilt

Dagger

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

Learn Dependency Injection with Dagger

(At this time probably you're an expert using dagger but if not these are great resources to deep in.)

Testing

Screenshot Testing

./gradlew executeScreenshotTests

Instrumental Testing with Espresso

./gradlew connectedAndroidTest

Screenshot Testing + Instrumental Testing

./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.

Demo

Do you want to contribute?

Feel free to report or add any useful feature, I will be glad to improve it with your help.

Developed By

License

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.