3

Dagger之十四、Hilt 对 Jetpack 库的支持

 2 years ago
source link: http://blog.chengyunfeng.com/?p=1126
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Dagger之十四、Hilt 对 Jetpack 库的支持

作者: rain 分类: Android Training 发布时间: 2021-10-30 13:20 6 0条评论

由于目前 Hilt 还处于 alpha 版本,当前只对 Jetpack 库中的 ViewModelWorkManager 组件给予支持,后续估计其他组件也会支持。

使用 Hilt 来注入 ViewModel 对象

需要在 app/build.gradle 中添加所需要的库以及注解处理器(annotation processor):

dependencies {
  implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
  // 当使用 Kotlin 时
  kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
  // 当使用 Java 时
  annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

然后就可以在 ViewModel 的构造函数上使用 @ViewModelInject 了,这样当其他地方需要这个 ViewModel 对象的时候, Dagger 就可以通过该构造函数来创建对象了,注意,对于 ViewModel 构造函数中的 SavedStateHandle 参数,需要使用 @Assisted 注解:

public class ExampleViewModel extends ViewModel {
  private final ExampleRepository repository;
  private final SavedStateHandle savedStateHandle;
  @ViewModelInject // 在构造函数上使用 ViewModelInject
  ExampleViewModel(
      ExampleRepository repository,
      @Assisted SavedStateHandle savedStateHandle)
    this.repository = repository;
    this.savedStateHandle = savedStateHandle;

然后使用了 @AndroidEntryPoint 的 Activity 或者 Fragment 中就可以通过 ViewModelProvider 来获取到这个 ViewModel 对象了,

@AndroidEntryPoint
public class ExampleActivity extends AppCompatActivity {
  private ExampleViewModel exampleViewModel;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class);

如果你使用了 Kotlin 则可以使用 viewModels() 扩展:

@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {
  private val exampleViewModel: ExampleViewModel by viewModels()

和 Jetpack 导航库一起使用

如果您的 ViewModel 被应用在 navigation graph中,可以使用 @AndroidEntryPoint 的 Activity 或者 Fragment 里面的 defaultViewModelProviderFactory 对象来构造 ViewModelProvider,然后通过 ViewModelProvider 来获取 ViewModel:

NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);
ViewModelProvider viewModelProvider = new ViewModelProvider(
  backStackEntry,
  getDefaultViewModelProviderFactory()
ExampleViewModel exampleViewModel = provider.get(exampleViewModel.getClass());

使用 Hilt 来注入 WorkManager 对象

同样需要先添加 hilt 对 WorkManager 的扩展库到您的项目 app/build.gradle 中:

dependencies {
  implementation 'androidx.hilt:hilt-work:1.0.0-alpha01'
  // 当使用 Kotlin 时
  kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
  // 当使用 Java 时
  annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

Worker 对象的构造函数上使用 @WorkerInject 注解来注入该对象。 由于 WorkManager 是在 Application 中构造的,所以在 Worker 对象中只能使用 @Singleton 范围或者没有范围(unscoped)的对象,对于构造函数上的 ContextWorkerParameters 参数也需要使用 @Assisted 注解:

public class ExampleWorker extends Worker {
  private final WorkerDependency workerDependency;
  @WorkerInject
  ExampleWorker(
    @Assisted @NonNull Context context,
    @Assisted @NonNull WorkerParameters params,
    WorkerDependency workerDependency
    super(context, params);
    this.workerDependency = workerDependency;

然后让 Application 类来实现 Configuration.Provider 接口,在 Application 中注入 HiltWorkFactory 对象,并使用 HiltWorkFactory 对象来构造 WorkManagerConfiguration 配置对象:

@HiltAndroidApp
public class ExampleApplication extends Application implements Configuration.Provider {
  @Inject HiltWorkerFactory workerFactory;
  @Override
  public Configuration getWorkManagerConfiguration() {
    return Configuration.Builder()
             .setWorkerFactory(workerFactory)
             .build();

注意,上面已经通过代码定制了 WorkManager 的配置信息,所以需要从 AndroidManifest.xml 文件中移除默认的配置文件


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK