Hilt基础使用
添加依赖
custom.gradle
ext {
dependencies = [
"hilt-android" : "com.google.dagger:hilt-android:2.28-alpha",
"hilt-android-compiler" : "com.google.dagger:hilt-android-compiler:2.28-alpha"
]
}
build.gradle
apply from : "custom.gradle"
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.28-alpha"
}
}
mytest1\build.gradle
plugins {
id 'com.android.application'
}
apply plugin: "dagger.hilt.android.plugin"
dependencies {
......
implementation rootProject.ext.dependencies["hilt-android"]
annotationProcessor rootProject.ext.dependencies["hilt-android-compiler"]
}
HttpObject.java
mytest1\src\main\java\com\example\mytest1\obj\HttpObject.java
public class HttpObject {
}
HttpModule.java
mytest1\src\main\java\com\example\mytest1\module\HttpModule.java
import com.example.mytest1.obj.HttpObject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
import dagger.hilt.android.components.ApplicationComponent;
import dagger.hilt.android.scopes.ActivityScoped;
@InstallIn(ApplicationComponent.class)
@Module
public class HttpModule {
@Provides
@Singleton
public HttpObject getHttpObject() {
return new HttpObject();
}
}
MyApplication
mytest1\src\main\java\com\example\mytest1\MyApplication.java
import android.app.Application;
import dagger.hilt.android.HiltAndroidApp;
@HiltAndroidApp
public class MyApplication extends Application {
}
MainActivity
mytest1\src\main\java\com\example\mytest1\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.mytest1.obj.HttpObject;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
private static final String TAG = "AAAAAAAAAAA";
@Inject
HttpObject httpObject;
@Inject
HttpObject httpObject2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: " + httpObject.hashCode());
Log.d(TAG, "onCreate: " + httpObject2.hashCode());
findViewById(R.id.test_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}
MainActivity2
mytest1\src\main\java\com\example\mytest1\MainActivity2.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.mytest1.obj.HttpObject;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint
public class MainActivity2 extends AppCompatActivity {
private static final String TAG = "AAAAAAAAAAA";
@Inject
HttpObject httpObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.d(TAG, "onCreate: " + httpObject.hashCode());
}
}
接口依赖注入
MyApplication
mytest2\src\main\java\com\example\mytest2\MyApplication.java
import android.app.Application;
import dagger.hilt.android.HiltAndroidApp;
@HiltAndroidApp
public class MyApplication extends Application {
}
IHttpRequest接口
mytest2\src\main\java\com\example\mytest2\http\IHttpRequest.java
public interface IHttpRequest {
void get();
}
HttpRequestImpl
mytest2\src\main\java\com\example\mytest2\http\HttpRequestImpl.java
import android.util.Log;
import javax.inject.Inject;
public class HttpRequestImpl implements IHttpRequest{
private static final String TAG = "AAAAAAAAAAAAAAAAAA";
@Inject
HttpRequestImpl() {}
@Override
public void get() {
Log.d(TAG, "测试接口注入!");
}
}
HttpRequestModule
mytest2\src\main\java\com\example\mytest2\http\HttpRequestModule.java
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
@Module
@InstallIn(ActivityComponent.class)
public abstract class HttpRequestModule {
@Binds
public abstract IHttpRequest bindTest(HttpRequestImpl httpRequest);
}
MainActivity
mytest2\src\main\java\com\example\mytest2\MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.mytest2.http.IHttpRequest;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Inject
IHttpRequest httpRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpRequest.get();
}
}
代码示例
地址:HiltDemo
|