1
头图

DI (Dependency Injection) is a technology that is widely used in programming and is very suitable for Android development. This technology can provide dependencies to classes so that they do not have to create these dependencies themselves. By following the DI principles, you will lay the foundation for a good application architecture, higher code reusability and convenient testing. Have you tried manual dependency injection in your application? Even if you use many of the existing dependency injection libraries today, as your project grows larger, these libraries still require a lot of template code, because you must manually construct each class and its dependencies, and create a container for replication Use and manage dependencies.

By following the DI principle, you will lay the foundation for a good application architecture, higher code reusability and convenient testing.

By providing a container for each Android class in the project and automatically managing its life cycle, the new Hilt library defines a standard way of DI in applications. Hilt is currently in the alpha stage, please try it in your app and provide .

Hilt is built on the basis of the popular DI library Dagger , so it can benefit from Dagger's compile-time correctness, runtime performance, scalability and Android Studio support . For details, see " Dagger navigation has never been easier | Android Studio 4.1 ". Because of this, 74% of the top 10k apps in the Google Play store use Dagger extensively. However, due to the generation of code during compilation, the build time will increase.

Since many classes in the Android Framework are instantiated by the operating system itself, when using Dagger in an Android application, there will be template codes related to this. Different from Dagger, Hilt integrated Jetpack libraries and Android Framework in class, and removes most of the template code, so you can focus on important definitions and injected into the binding link , without having to worry about managing the configuration and Dagger Associated. Hilt can automatically generate and provide the following content:

  • used to integrate Android Framework classes and Dagger components , avoiding manual creation
  • Hilt automatically generates scope annotations for
  • predefined bindings and qualifiers

Most importantly, because Dagger and Hilt can coexist, you can migrate applications as needed.

Hilt actual combat

In order to show you Hilt's ease of use, we will demonstrate some fast DI through a typical Android application. Let's use Hilt to inject AnalyticsAdapter into MainActivity.

First, add the @HiltAndroidApp annotation to your Application class to enable Hilt in your application and trigger the code generation of Hilt:

@HiltAndroidApp
class MyApplication : Application() { ... }

Secondly, by using @Inject annotation to modify the constructor of AnalyticsAdapter, indicate how Hilt provides its instance:

class AnalyticsAdapter @Inject constructor() { ... }

Third, in order to inject the AnalyticsAdapter instance into MainActivity, you need to add @AndroidEntryPoint annotation to the Activity to enable Hilt, and perform injection through the @Inject annotation modification field:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
  @Inject lateinit var analytics: AnalyticsAdapter
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // analytics 实例已经通过 Hilt 赋值,并且可以使用
  }
}

For more information, please check the new annotation function easily in the cheat sheet at the end of the article.

support for Jetpack

You can easily use your favorite Jetpack library through Hilt. In this version, we support ViewModel and WorkManager direct injection.

For example, inject a component architecture ViewMode into LoginActivity - LoginViewModelm: Add @ViewModelInject annotation to LoginViewModel, and then you can use it in Activity or Fragment as you want.

class LoginViewModel @ViewModelInject constructor(
  private val analyticsAdapter: AnalyticsAdapter
): ViewModel { ... }
@AndroidEntryPoint
class LoginActivity : AppCompatActivity() {
  private val loginViewModel: LoginViewModel by viewModels()
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // loginViewModel 已经可以被使用
  }
}

For more information about Jetpack support, please refer to: Android official documentation|Hilt and Jetpack integration

Start using Hilt

If you have already Hilt interested and would like more information, we've put together the following guide to help you understand how add Hilt to your Android application

document

If you are new to DI or Dagger, please check the guide above to add Hilt to Android apps. If you already know Dagger, please check the documentation provided in dagger.dev/hilt. If you just want to know about the new notes and what you can do with Hilt, please check and bookmark the cheat sheet at the end of the article.

for Dagger users

If you already use Dagger or dagger.android in your application, check the migration guide or the Codelab mentioned below to help you switch to Hilt. Since Dagger and Hilt can coexist, you can migrate your application gradually.

Codelab

We have released the following two Codelabs to teach you how to use Hilt:

sample code

Do you want to see how to use Hilt in an existing application? Check out the following resources:

Feedback

Hilt is currently in version 1.0.0-beta01, if you have any problems in use, please report to us .

cheat sheet

This cheat sheet allows you to quickly view the functional differences between Hilt and Dagger annotations and how to use them.


Android开发者
404 声望2k 粉丝

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