【问题标题】:Dagger 2 using classes generated by another libraryDagger 2 使用另一个库生成的类
【发布时间】:2018-10-19 08:31:34
【问题描述】:

我有一个生成 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
}

内容数据存储库

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

MyGeneratedDataMapper

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

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

如果我做一个干净的 + 构建,它不起作用,同样的错误。 我想让它连续工作。

【问题讨论】:

  • 我猜你可能有时间问题我认为出于某种原因匕首在你的课之前执行
  • 就是这样,如果你仔细查看 gradle 任务顺序,kaptGenerateStubsDebugKotlin 是 dagger 想要访问我生成的类的地方,而我的类是 kaptDebugKotlin 我猜的任务
  • 抱歉回复延迟。你是如何将 dagger 导入你的处理器的,如果你自己不导入,那么你指望用户来实现它吗?
  • 我没有在我的库中导入处理器,所以是的,我让用户(我)在最终项目中实现它。

标签: android kotlin dagger-2 kapt


【解决方案1】:

我不知道您是否使用带有 androidX 工件的 AS3.2 或 AS3.3,但也许您也是这种情况。

所以当我在 AS3.2 中迁移到 androidX artifacts 时,我遇到了一堆 NonExistentClass 错误,以结束构建

kaptGenerateStubsDebugKotlin 
:data:kaptDebugKotlin
:data:kaptDebugKotlin 

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

更新:

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

所以这个错误不在 Dagger 中,但它在 Jetifier 中,我完全忽略了它在迁移期间设置为启用的事实

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

抱歉,jetifier beta01 与 alpha10 二进制不兼容。

我们已经发布了 beta02,应该可以解决这个问题。

请尝试:

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

【讨论】:

    【解决方案2】:

    您可能不会喜欢我的回答,但顺序有点随机。 查看thread 以获得更多解释,也许还有更多指导,但是,如果您想验证您是否正在运行,请先查看 Gradle 插件以及如何使用它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多