monjur35 / Ktor-Client

Implementation of Ktor Client side implementation in android application with kotlin,Dependency Injection ( Hilt )
0 stars 0 forks source link

"No virtual method getModule()" Error for dependency injection! #1

Open FookThisOffical opened 1 year ago

FookThisOffical commented 1 year ago

Hey Does that kind of providing dependency work for you? have you tried to run it on emulator? cause i'm actually getting that error i've wrote in title. It's NoSuchMethodError exception as you can see and i don't know how to fix it really, though i've done a lot. I had the problem with koin, too and did bunch of invalidating Android Studio cache and all which of course it didn't do it either so i decided trying it with Hilt and it throws that same exception when i run the app.

Full Logcat

FATAL EXCEPTION: main Process: com.client.client, PID: 14525 java.lang.NoSuchMethodError: No virtual method getModule()Ljava/lang/Module; in class Ljava/lang/Class; or its super classes (declaration of 'java.lang.Class' appears in /apex/com.android.art/javalib/core-oj.jar) at ch.qos.logback.core.util.EnvUtil.logbackVersionByModule(EnvUtil.java:56) at ch.qos.logback.core.util.EnvUtil.logbackVersion(EnvUtil.java:36) at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:81) at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:77) at ch.qos.logback.classic.spi.LogbackServiceProvider.initializeLoggerContext(LogbackServiceProvider.java:52) at ch.qos.logback.classic.spi.LogbackServiceProvider.initialize(LogbackServiceProvider.java:41) at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183) at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170) at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455) at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390) at io.ktor.util.logging.KtorSimpleLoggerJvmKt.KtorSimpleLogger(KtorSimpleLoggerJvm.kt:10) at io.ktor.client.plugins.DefaultResponseValidationKt.(DefaultResponseValidation.kt:18) at io.ktor.client.plugins.DefaultResponseValidationKt.addDefaultResponseValidation(Unknown Source:0) at io.ktor.client.HttpClient.(HttpClient.kt:170) at io.ktor.client.HttpClient.(HttpClient.kt:84) at io.ktor.client.HttpClientKt.HttpClient(HttpClient.kt:43) at com.client.client.di.Client.provideClient(UserModule.kt:133) at com.client.client.di.Client_ProvideClientFactory.provideClient(Client_ProvideClientFactory.java:29) at com.client.client.DaggerMainApplication_HiltComponents_SingletonC$SingletonCImpl.userApi(DaggerMainApplication_HiltComponents_SingletonC.java:557) at com.client.client.DaggerMainApplication_HiltComponents_SingletonC$SingletonCImpl.access$1800(DaggerMainApplication_HiltComponents_SingletonC.java:537) at com.client.client.DaggerMainApplication_HiltComponents_SingletonC$SingletonCImpl$SwitchingProvider.get(DaggerMainApplication_HiltComponents_SingletonC.java:602) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at com.client.client.DaggerMainApplication_HiltComponents_SingletonC$ViewModelCImpl$SwitchingProvider.get(DaggerMainApplication_HiltComponents_SingletonC.java:461) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory$1.create(HiltViewModelFactory.java:104) at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.kt:90) at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:116) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:215) at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:156) at com.client.client.feature_signup.presentation.signup.SignupKt.TextFields(Signup.kt:649) at com.client.client.feature_signup.presentation.signup.SignupKt.Signup(Signup.kt:113) at com.client.client.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:29) at com.client.client.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:28) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:132) at androidx.compose.material3.SurfaceKt$Surface$1.invoke(Surface.kt:114) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) 2023-07-24 19:48:41.432 14525-14525 AndroidRuntime com.client.client E at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.material3.SurfaceKt.Surface-T9BRK9s(Surface.kt:111) at com.client.client.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:25) at com.client.client.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:24) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.material3.TextKt.ProvideTextStyle(Text.kt:360) at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81) at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.material3.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:73) at com.client.client.ui.theme.ThemeKt.ClientTheme(Theme.kt:112) at com.client.client.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:24) at com.client.client.ComposableSingletons$MainActivityKt$lambda-3$1.invoke(MainActivity.kt:23) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:428) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:194) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:123) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:122) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:114) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:156) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:155) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:155) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:140) 2023-07-24 19:48:41.437 14525-14525 AndroidRuntime com.client.client E at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:78) at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3373) at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3363) at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341) at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1) at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3363) at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3298) at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:587) at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:966) at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:519) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131) at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1099) at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:131) at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:181) at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.kt:314) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.kt:192) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:138) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131) at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1174) at android.view.View.dispatchAttachedToWindow(View.java:21290) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3491) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3498) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3498) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3498) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3498) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2820) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2328) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9087) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239) at android.view.Choreographer.doCallbacks(Choreographer.java:899) at android.view.Choreographer.doFrame(Choreographer.java:832) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

HttpClient Module

```kotlin @Module @InstallIn(SingletonComponent::class) object Client { @Provides fun provideClient(): HttpClient { return HttpClient(CIO) { install(ContentNegotiation) { json() } install(Logging) { logger = Logger.DEFAULT level = LogLevel.HEADERS sanitizeHeader { header -> header == HttpHeaders.Authorization } } } } @Provides fun provideUserApi(http: HttpClient): UserApi { return UserApiImp(http) } } ```

Repository Module

```kotlin @Module @InstallIn(SingletonComponent::class) interface UserRepo { @Singleton @Binds fun bindUserRepo(userRepo: UserRepositoryImp): UserRepository } ```

UserApi Module

```kotlin @Module @InstallIn(SingletonComponent::class) interface UserApi { @Singleton @Binds fun bindUserApi(userApi: UserApiImp): UserApi } ```

UserApi Implementation class

```kotlin class UserApiImp @Inject constructor(private val client: HttpClient): UserApi { override suspend fun postUser() { TODO("Not yet implemented") } override suspend fun getUser(): List { TODO("Not yet implemented") } override suspend fun getUserById(): List { TODO("Not yet implemented") } override suspend fun deleteUserById(): List { TODO("Not yet implemented") } } ```

UserRepository Implementation class

```kotlin class UserRepositoryImp @Inject constructor(private val dao: UserDao, private val api: UserApi) : UserRepository { override fun getAllItemsStream(): Flow> = dao.observeAll() override suspend fun insertItem(localUser: LocalUser) = dao.upsert(localUser) override suspend fun deleteItem(localUser: LocalUser) = dao.upsert(localUser) override suspend fun updateItem(localUser: LocalUser) = dao.upsert(localUser) } ```

Version of library and things i'm using

``` kotlin_version = '1.9.0' ktor_version = '2.3.2' hilt_version = '2.46.1' ``` ``` Gradle Plugin Version = 7.4.2 Gradle Version = 7.6 ``` And this is koin which got the same error with: ``` koin_version = '3.4.2' koin_compose = '3.4.5' koin_ktor = '3.4.1' koin_android = '3.4.2' ``` `

If i delete private val api: UserApi from UserRepository Implementation constructor the error is gone. Error's also gone when i delete HttpClient from UserApi Implementation. I can see that problem's somewhere in between but i don't know where exactly. I thought maybe it's related to providing HttpClient since logcat says about getModule(). Not sure if this way of providing HttpClient only doesn't work for me. i appreciate if you'd give me a clue.

FookThisOffical commented 1 year ago

@mitchtabian I have seen your work about dota2 here: https://github.com/mitchtabian/Dota-Info but i don't understand how you do it with HttpClient. I got through the project and couldn't find out how you injected dependencies for it.

@Stexxe