安卓||在 Hilt 不支持的类中注入依赖项,例如 Helper 类
Posted
技术标签:
【中文标题】安卓||在 Hilt 不支持的类中注入依赖项,例如 Helper 类【英文标题】:Android || Inject dependencies in classes not supported by Hilt like Helper class 【发布时间】:2021-08-19 21:29:00 【问题描述】:我在我的应用程序中使用了 Hilt,我成功地在 Activity 类中注入了依赖项并获得了所需的结果,但无法在其他类中注入依赖项,例如 helper 类。
请检查我在 MainActivity 类下面尝试过的以下代码
package com.inn.hiltdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.inn.hiltdemo.hilt.MyFirstHelperClass;
import javax.inject.Inject;
import javax.inject.Named;
import dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity
@Inject
@Named("MyHelperClass")
MyFirstHelperClass myFirstHelperClass;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////IT IS RUNNING SUCCESSFULLY
System.out.println("HERE IS THE HELPER CLASS DATA==>>>> "+ myFirstHelperClass.getStringData());
MySecondHelperClass mySecondHelperClass = new MySecondHelperClass();
mySecondHelperClass.getSecondaryDataFromHelper(this);
我的MyModule类
package com.inn.hiltdemo.hilt
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Named
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
class MyModule
@Singleton
@Provides
@Named("MyHelperClass")
fun provideMyHelperClass(): MyFirstHelperClass
return MyFirstHelperClass()
请检查 MyFirstHelperClass 类
package com.inn.hiltdemo.hilt;
public class MyFirstHelperClass
public String getStringData()
return "My Data";
这是我的 MySecondHelperClass.java,我试图在其中获取 Hilt 依赖注入,请检查以下代码。
package com.inn.hiltdemo;
import android.content.Context;
import com.inn.hiltdemo.hilt.MyFirstHelperClass;
import javax.inject.Singleton;
import dagger.hilt.EntryPoint;
import dagger.hilt.InstallIn;
import dagger.hilt.android.EntryPointAccessors;
import dagger.hilt.components.SingletonComponent;
@Singleton
public class MySecondHelperClass
@EntryPoint
@InstallIn(SingletonComponent.class)
interface DeviceDiagnosticHelperEntryPoint
public MyFirstHelperClass provideMyFirstHelperClass();
public void getSecondaryDataFromHelper(Context mContext)
Context appContext = mContext.getApplicationContext();
DeviceDiagnosticHelperEntryPoint hiltEntryPoint =
EntryPointAccessors.fromApplication(appContext,
DeviceDiagnosticHelperEntryPoint.class);
MyFirstHelperClass myFirstHelperClass =
hiltEntryPoint.provideMyFirstHelperClass();
System.out.println("HERE WE CAN USE==>>>> " + myFirstHelperClass.getStringData());
从 MySecondHelperClass.java ,我想在我的上面的类中进行依赖注入但是得到如下异常
[Dagger/MissingBinding] com.inn.hiltdemo.hilt.MyFirstHelperClass 不能在没有@Inject 构造函数或@Provides 注释的方法的情况下提供。 com.inn.hiltdemo.hilt.MyFirstHelperClass 请求在 com.inn.hiltdemo.MySecondHelperClass.DeviceDiagnosticHelperEntryPoint.provideMyFirstHelperClass()
【问题讨论】:
你返回的是MyFirstHelperClass
而不是MySecondHelperClass
其实从我的 SecondClassHelper 我想要我的 FirtClassHelper 的依赖注入并且想要访问它的 dtaa
MyFirstHelperClass
不归你所有,所以你真的需要那个 Module-class 吗?
【参考方案1】:
您在第二个 Helper 中错过了 @Named 注释:
public class MySecondHelperClass
@EntryPoint
@InstallIn(SingletonComponent.class)
interface DeviceDiagnosticHelperEntryPoint
@Named("MyHelperClass")
MyFirstHelperClass provideMyFirstHelperClass();
public void getSecondaryDataFromHelper(Context mContext)
Context appContext = mContext.getApplicationContext();
DeviceDiagnosticHelperEntryPoint hiltEntryPoint =
EntryPointAccessors.fromApplication(appContext,
DeviceDiagnosticHelperEntryPoint.class);
MyFirstHelperClass myFirstHelperClass =
hiltEntryPoint.provideMyFirstHelperClass();
System.out.println("HERE WE CAN USE==>>>> " +
myFirstHelperClass.getStringData());
第二个选项是用注入注解MyFirstHelperClass
的构造函数并完全移除Module类(如果MyFirstHelperClass
是你的)
【讨论】:
以上是关于安卓||在 Hilt 不支持的类中注入依赖项,例如 Helper 类的主要内容,如果未能解决你的问题,请参考以下文章