无法提供,除非有一个带有@Provides注解的方法(在Android Kotlin中使用Dagger 2)

huangapple go评论71阅读模式
英文:

Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin)

问题

在许多网站和各种文章中搜索过,但没有效果,因此我请求您帮助解决Dagger 2(DI)的问题。

我无法理解问题出在哪里。

我对Dagger 2(Di)还很新,正在学习中,请帮助我理解。

我的代码:

MainActivity.kt

class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var networkService: NetworkService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        App().gerAppComponents().inject(mainActivity = this)

        setContentView(R.layout.activity_main)

        networkService.buildService(service = Movies::class.java).getPopularMovie(apiKey = "test", language = "en-US", page = 1)

    }

}

App.kt

class App : Application() {

    fun gerAppComponents(): AppComponent {
        return DaggerAppComponent.builder().appNetworkModule(AppNetworkModule())
            .build()
    }
}

ServiceBuild.kt

object ServiceBuild : NetworkService {
    private val client = OkHttpClient.Builder().build()

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl("https://api.themoviedb.org/3")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build()

    override fun <T> buildService(service: Class<T>): T = retrofit.create(service)
}

NetworkService.kt

interface NetworkService {
    fun <T> buildService(service: Class<T>): T
}

AppNetworkModule.kt

@Module
class AppNetworkModule {

    @Provides
    @Singleton
    fun getRetrofitService(): ServiceBuild {
        return ServiceBuild
    }
}

AppComponent.kt

@Singleton
@Component(modules = arrayOf(AppNetworkModule::class))
interface AppComponent {
    fun inject(mainActivity: MainActivity)
}

构建错误:

app/build/tmp/kapt3/stubs/debug/com/rebus/client/di/components/AppComponent.java:10: error: [Dagger/MissingBinding] com.rebus.client.services.impl.NetworkService cannot be provided without an @Provides-annotated method.
public abstract void inject(@org.jetbrains.annotations.NotNull()
                     ^
com.rebus.client.services.impl.NetworkService is injected at
      com.rebus.client.MainActivity.networkService
com.rebus.client.MainActivity is injected at
      com.rebus.client.di.components.AppComponent.inject(com.rebus.client.MainActivity)
英文:

searched on many sites and in various articles, but without effect, so I ask for your help with Dagger 2 (DI)

I can not understand what the problem is

I'm new to Dagger 2 (Di) and I'm just studying it, please help me understand

My code:

MainActivity.kt

class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var networkService: NetworkService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        App().gerAppComponents().inject(mainActivity = this)

        setContentView(R.layout.activity_main)

        networkService.buildService(service = Movies::class.java).getPopularMovie(apiKey = &quot;test&quot;, language = &quot;en-US&quot;, page = 1)

    }

}

App.kt

class App : Application() {

    fun gerAppComponents(): AppComponent {
        return DaggerAppComponent.builder().appNetworkModule(AppNetworkModule())
            .build()
    }
}

ServiceBuild.kt

object ServiceBuild : NetworkService {
    private val client = OkHttpClient.Builder().build()

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl(&quot;https://api.themoviedb.org/3&quot;)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build()

    override fun &lt;T&gt; buildService(service: Class&lt;T&gt;): T = retrofit.create(service)
}

NetworkService.kt

interface NetworkService {
    fun &lt;T&gt; buildService(service: Class&lt;T&gt;): T
}

AppNetworkModule.kt

@Module
class AppNetworkModule {

    @Provides
    @Singleton
    fun getRetrofitService(): ServiceBuild {
        return ServiceBuild
    }
}

AppComponent.kt

@Singleton
@Component(modules = arrayOf(AppNetworkModule::class))
interface AppComponent {
    fun inject(mainActivity: MainActivity)
}

Build error

app/build/tmp/kapt3/stubs/debug/com/rebus/client/di/components/AppComponent.java:10: error: [Dagger/MissingBinding] com.rebus.client.services.impl.NetworkService cannot be provided without an @Provides-annotated method.
    public abstract void inject(@org.jetbrains.annotations.NotNull()
                         ^
      com.rebus.client.services.impl.NetworkService is injected at
          com.rebus.client.MainActivity.networkService
      com.rebus.client.MainActivity is injected at
          com.rebus.client.di.components.AppComponent.inject(com.rebus.client.MainActivity)

答案1

得分: 0

将你的代码中的 ServiceBuild 改成 NetworkService,就像这样:

@Module
class AppNetworkModule {

    @Provides
    @Singleton
    fun getRetrofitService(): NetworkService {
        return NetworkService
    }
}
英文:

Change your code ServiceBuild to NetworkService like this:

@Module
class AppNetworkModule {

    @Provides
    @Singleton
    fun getRetrofitService(): NetworkService {
        return ServiceBuild
    }
}

huangapple
  • 本文由 发表于 2020年9月27日 08:17:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定