Tag: 匕首 2

匕首2构造函数注入与命名参数在kotlin

我有这个依赖: @Singleton class SpiceMix @Inject constructor(@field:[Named(“oregano”)] private val oregano: Spice, @field:[Named(“sage”)] private val sage: Spice, @field:[Named(“rosemary”)] private val rosemary: Spice) 还有一个模块来完成它的依赖: @Module class SpiceModule { @Provides @Named(“oregano”) @Singleton fun provideOregano(): Spice = Oregano() @Provides @Named(“sage”) @Singleton fun provideSage(): Spice = Sage() @Provides @Named(“rosemary”) @Singleton fun provideRosemary(): Spice = Rosemary() SpiceMix然后被注入我的应用程序的各个位置。 但是,这不会编译,我得到一个错误: Spice cannot be provided […]

匕首类显示为红色,但仍然建立

我是Dagger2的新手,我正在尝试在我的Kotlin项目(1.1.51)中使用它。 我跟着几个教程,一切都很好。 我有这些在我的gradle文件,我使用的是Android Studio 3.01,我必须使用gradle-3.3 apply plugin: ‘kotlin’ apply plugin: ‘kotlin-allopen’ apply plugin: ‘kotlin-kapt’ compile ‘com.google.dagger:dagger:2.11’ kapt ‘com.google.dagger:dagger-compiler:2.11’ kapt { generateStubs = true } 我可以成功地运行我的项目和Dagger2似乎工作。 唯一令人讨厌的问题是,Dagger创建的类在导入列表中总是显示为红色。 例如DaggerMainComponent。 import com.burfdevelopment.hack24.Dagger.MainComponent import com.burfdevelopment.hack24.Dagger.MainModule import com.burfdevelopment.hack24.Dagger。 DaggerMainComponent 所以,如果做一个代码整洁,删除未使用的导入它总是删除它,即使它被使用。 如果我点击DaggerMainComponent,它会转到生成的类。

Dagger2限定符不能与Kotlin一起使用?

我有一个简单的类如下 class MainString(val msg: String) 我想注入不同的参数,所以我使用@Named限定符根据https://google.github.io/dagger/users-guide 与我的AppModule有 @Provides @Named(“Two”) fun provideTwoMainString(): MainString { return MainString(“Two”) } @Provides @Named(“One”) fun provideOneMainString(): MainString { return MainString(“One”) } 在我的主动活动中,我只是打电话 @Inject @Named(“One”) lateinit var stringOne: MainString @Inject @Named(“Two”) lateinit var stringTwo: MainString 然而,当我编译时,它抱怨 Error:(11, 1) error: com.elyeproj.demo_dagger_scope.MainString cannot be provided without an @Inject constructor or from an @Provides- or […]

kotlin限定符注释被忽略

下面的代码编译第一​​次,但在第二次构建给出: 错误:SomeObject绑定多次: @Provides @NotNull SomeObject SomeModule.provideSomeObject() @Provides @NotNull SomeObject SomeModule.provideSomeScopedObject() 看起来,编译器忽略了连续构建中的Qualifier注释。 当组件和模块在java中被编写时没有帮助,因为在Main类中注释被忽略。 @dagger.Component(modules = arrayOf(SomeModule::class)) interface Component { fun inject(main: Main) } class Main { @field:[javax.inject.Inject SomeScope] lateinit var obj: SomeObject } @dagger.Module class SomeModule { @dagger.Provides fun provideSomeObject(): SomeObject { return SomeObject(“noScope”) } @SomeScope @dagger.Provides fun provideSomeScopedObject(): SomeObject { return SomeObject(“someScope”) } } data […]

Android Kotlin Dagger2提供了gson:指定为非null的参数为null

我想提供一个单一的gson实例,所以我提供它在我的NetworkModule,当我创建改造api,gson是非null,但是当我在改造类中使用gson实例,它是空的.. @Module class NetworkModule { @Provides @Singleton fun provideGson(): Gson { return Gson() } @Provides @Singleton fun provideMyEnterpriseApi(gson: Gson): MyEnterpriseApi { //HERE GSON IS NOT NULL return RetrofitMyEnterpriseApi(BuildConfig.API_MYENTERPRISE_URL, BuildConfig.API_MYENTERPRISE_CONNECTION_TIMEOUT, BuildConfig.API_MYENTERPRISE_READ_TIMEOUT, gson) } } 我的翻新API类: class RetrofitMyEnterpriseApi(baseUrl: String, connectTimeout: Long, readTimeout: Long, private val gson: Gson) : RetrofitApi(baseUrl, connectTimeout, readTimeout, RetrofitMyEnterpriseCalls::class.java) { override fun buildRetrofit(baseUrl: String, […]

匕首+ Kotlin不注射

我学习DI的Dagger 2,我只是做了这个代码注入改造: NetModule.kt @Module class AppModule(val mApplication: Application) { @Provides @Singleton fun provideApplication() : Application{ return mApplication } } AppModule.kt @Module class AppModule(val mApplication: Application) { @Provides @Singleton fun provideApplication() : Application{ return mApplication } } NetComponent.kt: @Singleton @Component(modules = arrayOf(AppModule::class, NetModule::class)) interface NetComponent { fun inject(activity: Activity) } CustomApplication.kt class CustomApplication : Application() { […]

kotlin可见性修饰符不匹配

这是官方文件说的。 但是当我做这样的事情: var human : Human? = null 并反编译成java代码,我发现它实际上是 @Nullable private Human human; 写这个没用 public var human : Human? = null 但是当我写这个: lateinit var human : Human 它这次成为公众 @NotNull public Human human; 当我使用Dagger2时,这是个大问题 在Dagger2中,注入的属性必须公开,但是如果我写var human : Human? = nullvariablesvar human : Human? = null var human : Human? = null它将无法建立。 所以,这是一个kotlin的错误? 或官方文件的错误?

Dagger 2 + Kotlin无法将Presenter插入到View中

我正在尝试使用Dagger 2创建简单的MVP构建应用程序。我正在试图获得与本教程中相同的结果,但使用Kotlin。 这是我的代码到目前为止。 主持人: class MainPresenter @Inject constructor(var view: IMainView): IMainPresenter{ override fun beginMessuring() { view.toastMessage(“Measuring started”) } override fun stopMessuring() { view.toastMessage(“Measuring stopped”) } } 视图: class MainActivity : AppCompatActivity(), IMainView { @Inject lateinit var presenter : MainPresenter val component: IMainComponent by lazy { DaggerIMainComponent .builder() .mainPresenterModule(MainPresenterModule(this)) .build() } override fun onCreate(savedInstanceState: Bundle?) { […]

在混合Java / Kotlin项目中使用Dagger 2的Maven配置

什么是在混合Java / Kotlin项目中使用Dagger 2的建议Maven设置? 我find了一个使用Gradle的示例项目: https : //github.com/damianpetla/kotlin-dagger-example与Maven类似的东西会非常有帮助。 更新:我试过了什么? 我使用了kotlinlang.org/docs/reference/using-maven.html中的Kotlin配置和google.github.io/dagger中的Dagger配置。 我还使用了build-helper-maven-plugin插件来将注释处理集成到IDEA中。 我的主要问题是我遇到了编译周期。 我的配置混合了Kotlin的编译和调用生成Dagger2类的注释处理器。 我无条件地尝试分离两个阶段,但缺乏更深入的Maven了解它的工作。

如何注入与Dagger 2科特林授权财产?

我需要( @Named )注入dagger2到一个kotlin委托的属性。 //works great! @set:Inject var cat: Cat by Ref(ref) //fails @set:[Inject Named(“dog”)] var dog : Animal by Ref(ref) 所以我试了 //fails, can’t use `@field` with a delegated property @field:[Inject Named(“dog”)] var dog : Animal by Ref(ref) //fails, can’t use `lateinit` with a delegated property @field:[Inject Named(“dog”)] lateinit var dog : Animal by Ref(ref)