Hilt a preliminary

Hilt is an Android-based dependency injection framework developed by the Android team in conjunction with the Dagger2 team. The most obvious features of Hilt compared to Dagger2 are: 1. Simplicity. 2. Android specific apis are provided.

The optimizations Hilt does include

  • You don’t have to write a lot of Component code
  • Scope is also automatically bound to Component
  • Predefined bindings, such as Application and Activity
  • Predefined qualifiers, such as @ApplicationContext and @ActivityContext

This section describes Hilt components

Hilt component life cycle

Hilt component scope

Hilt integration

1. Add the following code to build. Gradle in your project structure

      classpath 'com. Google. Dagger hilt - android - gradle - plugin: 2.28 alpha'
Copy the code

2. Add it to build.gradle of App structure

apply plugin: 'dagger.hilt.android.plugin'
Copy the code
  compileOptions {	/ / support java8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
Copy the code
   implementation Com. Google. "dagger hilt - android: 2.28 alpha." "
   annotationProcessor Com. Google. "dagger hilt - android - the compiler: 2.28 alpha." "
Copy the code

3. App complete Gradle code

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 - the compiler: 2.28 alpha." "
}
Copy the code

Use of Hilt objects

1. Provide an object HttpObject

public class HttpObject {}Copy the code

2. Write corresponding Module

// See the corresponding hilT component above
@InstallIn(ApplicationComponent.class)
@Module
public class HttpModule {

    @Singleton
    @Provides 
    public HttpObject getHttpObject(a){
        return newHttpObject(); }}Copy the code

3. Inject into the Activity

@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()+""); }}Copy the code

4. Register with Application

@HiltAndroidApp
public class MyApplication extends Application {}Copy the code

5. Project operation and printing

The 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: 59372848Copy the code

Use of the Hilt interface

1. Provide an interface to implement

public interface TestInterface {

    void method(a);
}
Copy the code

2. Provide an object that implements the interface

import android.util.Log;

import javax.inject.Inject;

public class TestClass implements TestInterface {


    @Inject
    TestClass() {
    }

    @Override
    public void method(a) {
        Log.i("TestClass".TestClass injection successful!); }}Copy the code

3. Write corresponding Module

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);
}
Copy the code

4. Final use

@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(); }}Copy the code

The Demo address:Let me download