1.使用Hilt实现快速依赖注入
1.1 导入依赖
//hilt依赖
//Hilt
implementation("com.google.dagger:hilt-android:2.44")
annotationProcessor("com.google.dagger:hilt-android-compiler:2.44")
1.2 在build.gradle(app)中加入插件
plugins {
id("com.android.application")
id("dagger.hilt.android.plugin")//hilt插件
}
1.3 在build.gradle(project)中加入classpath
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.1.4" apply false
}
buildscript{
dependencies{
classpath("com.google.dagger:hilt-android-gradle-plugin:2.44")
}
}
1.4 创建自定义Applciation
package com.tiger.hilt;
import android.app.Application;
import dagger.hilt.android.HiltAndroidApp;
@HiltAndroidApp //生成相应的组件
public class MyApplication extends Application {
}
1.5 将自定义Application添加到 清单文件
1.6 将要注入的类
package com.tiger.hilt;
import javax.inject.Inject;
public class User {
//把对象实例的获取称之为绑定
@Inject
public User(){
}
}
1.7 activity
package com.tiger.hilt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import dagger.hilt.android.HiltAndroidApp;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("ning",user.toString());
}
}
注入成功
2.Hilt 的优势
3.在Hilt 中使用Module
package com.tiger.hilt;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityRetainedComponent;
import dagger.hilt.android.internal.managers.ApplicationComponentManager;
import dagger.hilt.components.SingletonComponent;
@InstallIn(SingletonComponent.class)//Application作用域
@Module
public class AppModule {
@Singleton
@Provides
User providerUser(){
return new User();
}
}
package com.tiger.hilt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import dagger.hilt.android.HiltAndroidApp;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
User user;
@Inject
User user2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("ning",user.toString());
Log.d("ning",""+(user==user2));
}
}
4.默认标准组件
ApplicationComponent新版本变成
SingletonComponent
5.限定符注入Context
@Inject//默认是ApplicationComponent
public ViewModel(User user, Application application,Activity activity,@ApplicationContext Context context) {
this.user = user;
this.application = application;
this.activity = activity;
this.context = context;
}
@Inject//默认是ApplicationComponent
public ViewModel(User user, Application application,Activity activity,@ActivityContext Context context) {
this.user = user;
this.application = application;
this.activity = activity;
this.context = context;
}
6.Hilt 对 ViewModel的支持
6.1导入依赖
//hilt依赖
//Hilt
implementation("com.google.dagger:hilt-android:2.44")
annotationProcessor("com.google.dagger:hilt-android-compiler:2.44")
//JetPack hilt 支持
implementation("androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03")
annotationProcessor("androidx.hilt:hilt-comiler:1.0.0-alpha03")
plugins {
id("com.android.application")
id("dagger.hilt.android.plugin")//hilt插件
}
buildscript{
dependencies{
classpath("com.google.dagger:hilt-android-gradle-plugin:2.44")
}
}
这里有点问题,以后在用Kotlin是另一套,这里主要了解使用流程,和核心思想!