头图

Hilt is an Android application recommended by Jetpack Dependency Injection (DI) solution, now stable . This means that Hilt can already be used in the production environment . Hilt is more convenient than Dagger, and it can also help you reduce template code. It is designed for Android and integrates multiple Jetpack dependent libraries. Many companies have already used Hilt in their applications and benefited from it.

In June 2020, Hilt preview version for the first time. It is responsible for defining the Android dependency injection standard program mission. Since then, we have received massive feedback from developers. These feedbacks not only improved Hilt, but also made it clear that we are on the right path.

Hilt does not need to manually create a dependency graph, nor does it need to manually inject and pass types. Instead, it automatically generates the required code based on the annotations during compile time. Hilt realizes the complex part of the work and generate all template codes instead of manual writing, helping you to get the most benefit from DI's best practices . In addition, Hilt is fully integrated with Android, which can help you automatically manage the life cycle of the dependency graph of Android Framework classes.

Let's observe Hilt's behavior through a simple example! configuring Hilt , injecting ViewModel into Activity from scratch in the project is as easy as adding annotations to the code, as shown below:

@HiltAndroidApp // 在应用中配置 Hilt
class MyApplication : Application() { ... }

// 使 Hilt 识别该 ViewModel
@HiltViewModel
class LoginViewModel @Inject constructor(
    private val savedStateHandle: SavedStateHandle,
    /*…Hilt 关注的其他依赖项... */
) : ViewModel() { ... }


// 使该 Activity 使用正确的 ViewModel 工厂,并注入其他依赖项
@AndroidEntryPoint 
class LoginActivity : AppCompatActivity() {

    private val loginViewModel: LoginViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // loginViewModel 已经可以使用
    }
}

In addition to the above, what other reasons do you choose to use Hilt in your application?

is more convenient than Dagger

Hilt is built on the popular DI library Dagger , so it can benefit from the compile-time verification provided by Dagger, good runtime performance, scalability, and Android Studio's support for . Some Dagger annotations are also commonly used in Hilt, such as @Inject (tell Dagger/Hilt how to provide an instance of a type). But Hilt is more convenient than Dagger.

I strongly recommend using Dagger for dependency injection in Android applications. However, simply using Dagger may cause excessive memory usage during creation. When this is used in conjunction with various complex lifecycle-aware components in Android development, many pitfalls may occur, such as memory leaks: A dependency whose scope is Activity is accidentally passed to the ViewModel. Hilt tailored specifically for Android can help you avoid some of the pitfalls of Dagger's basic use.

——Marcelo Hernandez, Senior Software Engineer, Tinder

If you already use Dagger in your application and want to migrate to Hilt, don’t worry! Dagger and the Hilt can co-exist, the application can be based on the need for migration .

Less template code

Hilt is customized-which means that in order to save you from writing code, it makes some decisions for you. Hilt defines standard components and dependency graphs, and fully integrates classes in the Android Framework, such as: Application, Activity, Fragment, View, and also defines scope annotations that restrict the scope of type instances to these components.

Pass @HiltAndroidTest annotation, Hilt can automatically generate test applications and test components. After migrating to Hilt, we can delete 20%-40% of the test-related template code.

——Jusun Lee, Software Engineer, YouTube

We only did shallow work on Hilt migration. However, we saw a change in the number of lines of code +72/-182 in one of the modules that were migrated to Hilt.

——Marcelo Hernandez, Senior Software Engineer, Tinder

customized for Android

Unlike Dagger, the dependency injection solution for Java programming language applications, Hilt only supports Android applications. Some annotations are exclusively for Hilt and define a proprietary Android DI method. These annotations include: @HiltAndroidApp , @AndroidEntryPoint , @HiltViewModel .

Finally, Hilt provides a built-in Dagger component that recognizes the Android life cycle. With Hilt, we can only focus on Dagger @Modules without worrying about components, sub-components, and component provider modes.

—— Marcelo Hernandez, Senior Software Engineer, Tinder

components and binding

Different from the understanding of Dagger, Hilt uses single-component system to simplify the dependency graph and generate less code during compilation.

through Hilt's single-component system, provides binding definitions only once, and can be shared among all classes that use the component. This is a big improvement over the previous. YouTube used a multi-component system. Modules need to be manually connected to custom components, and there are many duplicate binding definitions.

——Jusun Lee, Software Engineer, YouTube

Since our Gradle module separation allows us to isolate development functions, this makes it easy for us to be too flexible when using Dagger. We found that migrating these modules to Hilt revealed that we inadvertently violated the separation of concerns.

——Marcelo Hernandez, Senior Software Development Engineer, Tinder

integrate other Jetpack libraries

You can use your favorite Jetpack library out of the box. So far, we provide direct injection support ViewModel , WorkManager , Navigation and Compose

Refer to document to learn more about Jetpack support.

I am very grateful for the way Hilt is used out of the box with ViewModel, and it eliminates the ViewModel.Factory template code that must be set when using Dagger alone.

——Marcelo Hernandez, Senior Software Development Engineer, Tinder

Hilt Learning Resources

Hilt is a DI solution for Android applications recommended by Jetpack. To learn more and start using it in your application, please refer to the following resources:


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。