Hilt就是Android团队联系了Dagger2团队,一起开发出来的一个专门面向Android的依赖注入框架。
相比于Dagger2,Hilt最明显的特征就是:1. 简单。2. 提供了Android专属的API。
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
apply plugin: 'dagger.hilt.android.plugin'
compileOptions { //支持 java8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
implementation "com.google.dagger:hilt-android:2.28-alpha"
annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"
apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.hqk.hiltdemo"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "com.google.dagger:hilt-android:2.28-alpha"
annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
public class HttpObject {
}
//详见上面的 hilt 对应组件
@InstallIn(ApplicationComponent.class)
@Module
public class HttpModule {
@Singleton
@Provides
public HttpObject getHttpObject(){
return new HttpObject();
}
}
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
HttpObject httpObject;
@Inject
HttpObject httpObject2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("hqk_MainActivity",httpObject.hashCode()+"");
Log.i("hqk_MainActivity",httpObject2.hashCode()+"");
}
}
@HiltAndroidApp
public class MyApplication extends Application {
}
2021-07-12 16:03:31.319 3509-3509/com.hqk.hiltdemo I/hqk_MainActivity: 59372848
2021-07-12 16:03:31.319 3509-3509/com.hqk.hiltdemo I/hqk_MainActivity: 59372848
public interface TestInterface {
void method();
}
import android.util.Log;
import javax.inject.Inject;
public class TestClass implements TestInterface {
@Inject
TestClass() {
}
@Override
public void method() {
Log.i("TestClass", "TestClass注入成功!");
}
}
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
@Module
@InstallIn(ActivityComponent.class)
public abstract class TestInterfaceModule {
// @Binds
// public abstract TestInterface bindTestClass(TestClassTwo testClass //实现不同的 功能);
@Binds
public abstract TestInterface bindTestClass(TestClass testClass);
}
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
HttpObject httpObject;
@Inject
HttpObject httpObject2;
@Inject
public TestInterface testInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("hqk_MainActivity",httpObject.hashCode()+"");
Log.i("hqk_MainActivity",httpObject2.hashCode()+"");
testInterface.method();
}
}