我正在使用Dagger 2并使其运行但我现在需要访问Android应用程序上下文。
我不清楚如何注入和访问上下文。我试着这样做:
@Module
public class MainActivityModule {
private final Context context;
MainActivityModule(Context context) {
this.context = context;
}
@Provides @Singleton
Context provideContext() {
return context;
}
然而,这会导致以下异常:
java.lang.RuntimeException:无法创建应用程序:java.lang.IllegalStateException:必须设置mainActivityModule
如果我检查Dagger生成的代码,则会在此处引发此异常:
public Graph build() {
if (mainActivityModule == null) {
throw new IllegalStateException("mainActivityModule must be set");
}
return new DaggerGraph(this);
}
我不确定这是否是注入Context的正确方法 - 将非常感谢任何帮助。
答案 0 :(得分:24)
@Module
public class MainActivityModule {
private final Context context;
public MainActivityModule (Context context) {
this.context = context;
}
@Provides //scope is not necessary for parameters stored within the module
public Context context() {
return context;
}
}
@Component(modules={MainActivityModule.class})
@Singleton
public interface MainActivityComponent {
Context context();
void inject(MainActivity mainActivity);
}
然后
MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder()
.mainActivityModule(new MainActivityModule(MainActivity.this))
.build();
答案 1 :(得分:1)
未正确构建应用程序组件,需要传入应用程序。这个Dagger 2示例完美地展示了如何执行此操作:https://github.com/google/dagger/tree/master/examples/android-simple/src/main/java/com/example/dagger/simple
答案 2 :(得分:1)
可能我们可以注入上下文,如下所示:
应用程序组件
@Component(
modules = [
(ApplicationModule::class),
(AndroidSupportInjectionModule::class),
(UiBindingModule::class)
]
)
interface ApplicationComponent : AndroidInjector<AndroidApplication> {
override fun inject(application: AndroidApplication)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: AndroidApplication): Builder
@BindsInstance
fun context(context: Context): Builder
fun build(): ApplicationComponent
}
}
自定义应用程序扩展匕首应用程序
class AndroidApplication : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerApplicationComponent.builder().application(this).context(this).build()
}
}
示例ApplicationModule
@Module
abstract class ApplicationModule {
/**
* Binds a background thread executor, which executes threads from a thread pool
* @param jobExecutor
* @return
*/
@Binds
internal abstract fun provideThreadExecutor(jobExecutor: JobExecutor): ThreadExecutor
/**
* Binds main ui looper thread
* @param uiThread
* @return
*/
@Binds
internal abstract fun providePostExecutionThread(uiThread: UIThread): PostExecutionThread
}
UI绑定模块示例
@Module
abstract class UiBindingModule {
@ContributesAndroidInjector(modules = [(MainActivityModule::class)])
internal abstract fun mainActivity(): MainActivity
@ContributesAndroidInjector(modules = [(MapFragmentModule::class)])
internal abstract fun mapFragment(): MapFragment
}
答案 3 :(得分:0)
我花了一段时间才找到合适的解决方案,因此认为这可能会为其他人节省一些时间,据我所知,这是当前Dagger版本(2.22.1)的首选解决方案。
在下面的示例中,我需要Application
的{{1}}来创建Context
(在RoomDatabase
中发生)。
如果您发现任何错误或错误,请告诉我,这样我也将学习:)
组件:
StoreModule
AppModule:
// We only need to scope with @Singleton because in StoreModule we use @Singleton
// you should use the scope you actually need
// read more here https://google.github.io/dagger/api/latest/dagger/Component.html
@Singleton
@Component(modules = { AndroidInjectionModule.class, AppModule.class, StoreModule.class })
public interface AwareAppComponent extends AndroidInjector<App> {
// This tells Dagger to create a factory which allows passing
// in the App (see usage in App implementation below)
@Component.Factory
interface Factory extends AndroidInjector.Factory<App> {
}
}
StoreModule:
@Module
public abstract class AppModule {
// This tell Dagger to use App instance when required to inject Application
// see more details here: https://google.github.io/dagger/api/2.22.1/dagger/Binds.html
@Binds
abstract Application application(App app);
}
应用程序:
@Module
public class StoreModule {
private static final String DB_NAME = "aware_db";
// App will be injected here by Dagger
// Dagger knows that App instance will fit here based on the @Binds in the AppModule
@Singleton
@Provides
public AppDatabase provideAppDatabase(Application awareApp) {
return Room
.databaseBuilder(awareApp.getApplicationContext(), AppDatabase.class, DB_NAME)
.build();
}
}
答案 4 :(得分:0)
我已经阅读了这篇文章,它非常有帮助。
https://medium.com/tompee/android-dependency-injection-using-dagger-2-530aa21961b4
示例代码。
更新:由于不需要这些行,我从AppComponent.kt中删除了这些行
USA
SF
NY
India
Agra
Mumbai
AppComponent.kt
fun context(): Context
fun applicationContext(): Application
AppModule.kt
@Singleton
@Component(
modules = [
NetworkModule::class,
AppModule::class
]
)
interface AppComponent {
fun inject(viewModel: LoginUserViewModel)
}
NetworkConnectivityHelper.kt
,仅添加了@Inject构造函数以传递上下文
@Module
class AppModule(private val application: Application) {
@Provides
@Singleton
fun providesApplication(): Application = application
@Provides
@Singleton
fun providesApplicationContext(): Context = application
@Singleton
@Provides
fun providesNetworkConnectivityHelper(): NetworkConnectivityHelper{
return NetworkConnectivityHelper(application.applicationContext)
}
}
App class.kt
class NetworkConnectivityHelper @Inject constructor(context: Context) {
private val connectivityManager =
context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
@Suppress("DEPRECATION")
fun isNetworkAvailable(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nc = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
nc != null
&& nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
val networkInfo = connectivityManager.activeNetworkInfo
return networkInfo != null && networkInfo.isConnected
}
}
最后,在我的活动中,我注入了助手
class App : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
this.appComponent = this.initDagger()
}
private fun initDagger() = DaggerAppComponent.builder()
.appModule(AppModule(this))
.build()
}
还有艾!它对我有用。
答案 5 :(得分:-1)
在本教程中介绍了在String的帮助下实现dagger2的最简单方法,这是您可以找到的最简单的方法
component = DaggerMyComponent.builder()
.appModule(new AppModule(this))
.dataModule(new DataModule())
.build();
@Provides
@Singleton
String getString(){
return "Androidcoding.in";
}
@Provides
@Singleton
int getInteger(){
return 123;
}
((MyApplication) getApplication()).getComponent().inject(this);
有关匕首2 http://www.androidcoding.in/2020/05/10/dagger-2-android/
的更多信息