Tag: 匕首

Dagger2不能在andorid中工作

我有使用Dagger2的问题 我创建组件,模块,提供 class testModule { @Provides @Singleton fun provideTestServer(): TestService { } } 我在MainActivity中调用了onCreate() DaggerImageComponent.builder().build().inject(this) 这里是我的问题DI在MainActivity中正常工作 class MainActivity: AppCompatActivity { @Inject lateinit var testService: TestService } 但其他文件不工作。 object TestObject { @Inject @JvmSynthetic // error: static field cannot inject lateinit var testService: TestService fun test() = testService.testfun() } 要么 @Singleton class TestClass { @Inject lateinit var […]

IncompleteAnnotationException:dagger.提供缺少的元素类型

当我添加下面的代码时出现错误: @Singleton @Provides fun provideGson() : Gson { return Gson() } 我真的不知道为什么。

如何在测试时注入不同的客户端进行改造?

有什么方法可以改变用Dagger将具有不同客户端的Retrofit模块注入到仪器测试的RestAdapter上的方法吗? @Provides @Singleton public APIService getAPIService() { RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(BuildConfig.HOST) .build(); return restAdapter.create(APIService.class); } 但是,我想在执行仪器测试时设置一个新的客户端。 @Provides @Singleton public APIService getAPIService() { RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(BuildConfig.HOST) .setClient(new MockClient()) .build(); return restAdapter.create(APIService.class); } 有没有办法做到这一点? 谢谢

如何在AndroidStudio中使用Dagger2和Kotlin在其他项目模块中实现接口库

我想在AndroidStudio中可以有2个不同的被动接口实现在其他工程模块中获取用户位置。 所以要具体它可以通过使用GMS,或只是本地Android的LocationManager。 这是我的存储库接口: interface RxLocationRepository { @SuppressLint("MissingPermission") fun onLocationUpdate(): Observable<Location> fun stopLocationUpdates() } 所以现在,我在这里实现这个接口,在AndroidStudio中的同一个项目模块中的类: class RxLocationRepositoryImpl(val reactiveLocationProvider: ReactiveLocationProvider, val reactiveLocationRequest: LocationRequest, val isUsingLocationNativeApi: Boolean, val locationManager: LocationManager, val geoEventsDistanceMeters: Int, val geoEventsIntervalSeconds: Int ) : RxLocationRepository { var locationToPopulate: Location = Location(LocationManager.GPS_PROVIDER) lateinit var mLocationCallbackNativeApi: LocationListener private val subject: BehaviorSubject<Location> = BehaviorSubject.createDefault(locationToPopulate) var locationEmitter: Observable<Location> […]

我怎样才能注入对象到android kotlin MVP mosby应用程序与匕首的主持人

我试图让我的应用程序中的匕首工作。 创建模块组件和MyApp后,我可以使用匕首注入数据库服务,但我有麻烦与演示者做同样的事情。 码: class MyApp : Application() { var daoComponent: DaoComponent? = null private set override fun onCreate() { super.onCreate() daoComponent = DaggerDaoComponent.builder() .appModule(AppModule(this)) // This also corresponds to the name of your module: %component_name%Module .daoModule(DaoModule()) .build() } } 模 @Module class DaoModule { @Provides fun providesEstateService(): EstateService = EstateServiceImpl() } 零件 @Singleton @Component(modules = […]

匕首在基类中注入接口

我想在抽象类构造函数中注入接口实现,并在子类中使用它。 我有编译时间错误: Error:Gradle: Dagger does not support injection into private fields Error:Gradle: Example.A cannot be provided without an @Provides-annotated method. Error:Gradle: Example.B cannot be provided without an @Inject constructor or from an @Provides-annotated method. Error:Gradle: Execution failed for task ':app:compileDemoDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. 在kotlin中的例子。 object Example { interface […]

Haskell中的HasSupportFragmentInjector问题 – DispatchingAndroidInjector为null

我试图在我的应用程序中使用匕首2支持来实现mvp模式 以下是对象: class BaseApplication : Application(), HasActivityInjector { override fun onCreate() { super.onCreate() initDi() } private fun initDi(){ DaggerAppComponent.builder().application(this).build().inject(this) } @Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity> override fun activityInjector(): AndroidInjector<Activity> { return activityInjector } } @Singleton @Component(modules = arrayOf(AndroidInjectionModule::class, AppModule::class, ActivityBuilder::class)) interface AppComponent { @Component.Builder interface Builder { @BindsInstance fun application(application: Application): Builder fun build(): AppComponent […]

匕首2 – 多模块应用程序错误

我在我的android kotlin应用程序中使用匕首2。 一切工作正常,直到我添加其他模块项目。 然后我看到这个错误: e: java.lang.IllegalStateException: failed to analyze: java.lang.IllegalArgumentException: expected one element but was: <android.support.v4.app.FragmentActivity, android.support.v4.app.TaskStackBuilder.SupportParentable> at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51) at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92) at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:386) at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96) at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:889) at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:96) at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137) at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916) at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888) at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385) at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at […]

用匕首使用Kotlin

使用Dagger向类中注入依赖关系的最好方法是什么,特别是像Dagger这样的零参数构造函数类,比如Activities? 匕首2可能会改善情况吗? 提前致谢。

Kotlin注解处理会忽略具有相似名称的项目

我最近把我的大部分项目都转换成了kotlin。 现在我遇到了几个似乎与注释库有关的不寻常的错误。 不用说,它并没有在Java中发生。 我将描述这些案例 – 一个在匕首,另一个在Butterknife。 1.在具有相同名称的不同模型中使用2个@Provides方法时。 例如在具有“provideFooOrBar”方法的文件“FooProvider.kt”中 @Module class FooProvider(private val view: FooActivity) { … @Provides @FooScope fun provideView() = view @Provides @FooScope fun provideFooOrBar() = Foo() } 和另一个文件“BarProvider.kt”具有相同的方法名称 @Module class BarProvider(private val view: BarActivity) { … @Provides @BarScope fun provideView() = view @Provides @BarScope fun provideFooOrBar() = Bar() } 在这种情况下,Dagger无法生成一些工厂库,并且出现以下编译错误: Error:(27, 32) error: […]