我一直在尝试设置依赖注入,但有一些我无法处理的东西。我几乎理解Dagger期待像这样的一些奇怪的问题。
以下是日志错误:
e: /Users/costular/AndroidProjects/TransmissionRemote/app/build/tmp/kapt3/stubs/debug/com/costular/transmissionremote/di/components/ServerComponent.java:8: error: com.costular.transmissionremote.torrents.TorrentRepository cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
e:
e: public abstract void inject(@org.jetbrains.annotations.NotNull()
e: ^
e: com.costular.transmissionremote.torrents.TorrentRepository is injected at
e: com.costular.transmissionremote.torrents.torrent_list.TorrentsActivity.torrentRepository
e: com.costular.transmissionremote.torrents.torrent_list.TorrentsActivity is injected at
e: com.costular.transmissionremote.di.components.ServerComponent.inject(activity)
e: /Users/costular/AndroidProjects/TransmissionRemote/app/build/tmp/kapt3/stubs/debug/com/costular/transmissionremote/di/components/TorrentComponent.java:4: error: This @Singleton component cannot depend on scoped components:
e:
e: @dagger.Component(modules = {com.costular.transmissionremote.di.modules.NetworkModule.class, com.costular.transmissionremote.di.modules.StorageModule.class, com.costular.transmissionremote.di.modules.TorrentModule.class}, dependencies = {com.costular.transmissionremote.di.components.ApplicationComponent.class})
e: ^
e: @Singleton com.costular.transmissionremote.di.components.ApplicationComponent
这是代码: 的 ApplicationComponent.kt
@Singleton
@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {
fun inject(myApp: MyApp)
fun context(): Context
fun app(): MyApp
}
ApplicationModule.kt
@Module
class ApplicationModule(private val myApp: MyApp) {
@Provides
@Singleton
fun provideApplication(): MyApp = myApp
@Provides
@Singleton
fun providesContext(): Context = myApp.applicationContext
}
TorrentComponent.kt
@Singleton
@Component(
modules = arrayOf(NetworkModule::class, StorageModule::class, TorrentModule::class),
dependencies = arrayOf(ApplicationComponent::class)
)
interface TorrentComponent {
fun inject(torrentsActivity: TorrentsActivity)
}
TorrentModule.kt
@Module
class TorrentModule {
@Singleton
@Provides
fun provideTorrentRepository(client: TransmissionClient): TorrentRepository = TorrentRepository(client)
}
NetworkModuke.kt
@Module
class NetworkModule(val host: String){
@Provides
@Singleton
fun providesRPCClient(): TransmissionClient = TransmissionClient.getInstance(host)
}
TorrentsActivity.kt
class TorrentsActivity : AppCompatActivity() {
@Inject
lateinit var torrentRepository: TorrentRepository
lateinit var presenter: TorrentsPresenter
private val fab by lazy { findViewById(R.id.fab) as FloatingActionButton }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
// Fab listener
fab.setOnClickListener { presenter.addTorrent() }
// Fragment initialization
// Injection
DaggerTorrentComponent.builder()
.applicationComponent((application as MyApp).applicationComponent)
.build()
}
非常感谢你!