Why I get Execution failed for task ':app:kaptDebugKotlin' if I use Moshi?

1.3k views Asked by At

Hy

I created an app which uses dagger, room, Moshi, Retrofit etc...

It works correctly until I add this line to my Model class : @JsonClass(generateAdapter = true)

After that I get this error:

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

java.lang.reflect.InvocationTargetException (no error message)

I don't know what is the problem, beacuse my another app is uses Moshi, and it works good.

I added all of the Moshi dependencies, and plugins

My gradle settings:
buildTypes {
        all {
            buildConfigField 'String', 'API_URL', "\"$apiurl\""

            lintOptions {
                abortOnError true
                //ignoreWarnings true
                fatal 'MissingTranslation', 'ExtraTranslation'
            }

        }

        debug {
            //applicationIdSuffix ".debug"
            ext.enableCrashlytics = false
            ext.alwaysUpdateBuildId = false
            multiDexEnabled true
        }

        staged {
            signingConfig signingConfigs.debug
            //applicationIdSuffix ".staged"
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', getDefaultProguardFile('proguard-android-optimize.txt')

            matchingFallbacks = ['debug']
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }

ApiModule:

  @Provides
    @Singleton
    fun provideMoshi(): Moshi {
        return Moshi.Builder()
            .add(DateConverter())
            .add(BigDecimalConverer())
            .add(KotlinJsonAdapterFactory())
            .build()
    }

@Provides
@Singleton
fun provideAuthApi(@Named("base_url") url: String, moshi: Moshi): AuthApi {

    var builder = OkHttpClient.Builder()
    builder = BuildTypeInitializations.setupInterceptor(builder)

    return Retrofit.Builder()
        .baseUrl(url)
        .addCallAdapterFactory(CoroutineCallAdapterFactory())
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .client(builder.build())
        .build()
        .create(AuthApi::class.java)
}

4

There are 4 answers

0
Coldzz On

For me only migrating from kapt to ksp helped

Here how to upgrade https://developer.android.com/build/migrate-to-ksp#kts

Change version to the last release version of ksp https://github.com/google/ksp/releases

And you may also need to update your kotlin plugin version, here you can find the latest https://kotlinlang.org/docs/releases.html#release-details

2
Andreas1234 On

Finally, I resolved. I used 1.8.x version of moshi, and when I updated to 1.11.0 the erros is gone

0
Miloš Černilovský On

I had a similar problem where InvocationTargetException was being thrown by kapt when processing Room entities, saying something about a nonexisting type. After a long investigation I found out the library which contained the entities also used Moshi and tagged one entity with @Json annotations, but since the app which used this library did not import Moshi kapt couldn't resolve the type which caused the error. The solution was importing Moshi in the lib via "api" instead of "implementation" or importing Moshi in the client app via "implementation".

0
Daniel S. On

I had the same issue as you but, for me, the solution was adding implementation "com.squareup.moshi:moshi-adapters:$1.12.0" to build.gradle file