Dagger 2使用另一个库生成的类

时间:2018-10-19 08:31:35

标签: android kotlin dagger-2 kapt

我有一个自制的库,可以生成DataMapper类。

它们是用@Singleton@Inject批注生成的,以便能够将它们注入我需要的位置。

但是,当Dagger尝试创建依赖关系树时,此错误不起作用:

:data:kaptGenerateStubsDebugKotlin
e: /Users/me/myproject/data/build/tmp/kapt3/stubs/debug/com/myproject/data/di/DataComponent.java:11: error: [Dagger/MissingBinding] error.NonExistentClass cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract com.myproject.domain.repository.ContentRepository contentRepository();
                                                                      ^
  error.NonExistentClass is injected at
      com.myproject.data.repository.ContentDataRepository.<init>(…, myGeneratedDataMapper, …)
  com.myproject.data.repository.ContentDataRepository is injected at
      com.myproject.data.di.module.DataModule.contentRepository(contentDataRepository)
  com.myproject.domain.repository.ContentRepository is provided at
      com.myproject.data.di.DataComponent.contentRepository()
:data:kaptDebugKotlin
:data:kaptDebugKotlin FAILED

涉及的类是:

DataModule(匕首的模块)

@Module
class DataModule {
    @Provides
    @Singleton
    fun contentRepository(contentDataRepository: ContentDataRepository): ContentRepository = contentDataRepository
}

DataComponent(匕首的组件):

@Singleton
@Component(modules = [DataModule::class])
interface DataComponent {
    fun contentRepository(): ContentRepository
}

ContentDataRepository

@Singleton
class ContentDataRepository @Inject constructor(
        private val myGeneratedDataMapper: MyGeneratedDataMapper
) : ContentRepository {
    ...
}

MyGeneratedDataMapper

@Singleton
class MyGeneratedDataMapper @Inject constructor() {
   ...
}

问题是,如果我在gradle.build中禁用了匕首依赖的kapt,则先构建,然后启用它,再构建,它就可以工作。

如果我执行清理+构建,则无法正常工作,出现同样的错误。 我想使其连续工作。

2 个答案:

答案 0 :(得分:0)

您可能不会喜欢我的回答,但是顺序有点随机。 请查看此thread,以获得更多的解释和更多的指导,但是,如果您想验证自己是否正在运行,请首先查看Gradle插件及其使用方法

答案 1 :(得分:0)

我不知道您是否将AS3.2或AS3.3与androidX工件一起使用,但也许您也是这种情况。

因此,当我在AS3.2中迁移到androidX artifacts时,我遇到了一堆NonExistentClass错误,以

结尾
kaptGenerateStubsDebugKotlin 
:data:kaptDebugKotlin
:data:kaptDebugKotlin 

我终于发现它与Dagger本身有关,并将Dagger2的版本从2.17降级到2.16,现在Dagger2的最新版本是2.18,我无法使用由于此错误/功能[他们忘了]。

更新

我找到了解决方案,它已经在今天发布,所以这里是问题跟踪链接: https://issuetracker.google.com/issues/115738511

所以该错误不在Dagger中,而是在Jetifier中,我完全忽略了在迁移期间将其设置为启用的事实

这是我从链接中复制的解决方案:

  

抱歉,喷射器beta01与alpha10不二进制兼容。

     

我们已经发布了beta02,它可以解决此问题。

     

请尝试:

buildscript {    dependencies {
       classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02'    } }
相关问题