Hilt就是Android團隊聯繫了Dagger2團隊,一塊兒開發出來的一個專門面向Android的依賴注入框架。 相比於Dagger2,Hilt最明顯的特徵就是:1. 簡單。2. 提供了Android專屬的API。java
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();
}
}
複製代碼