0

I am trying to add json serialization dependencies in my kotlin project but it doesnt work and lots of error appear when I sync project. I dont know why. I did as in the serialization dependency addition section on the kotlin web site, but it didn't work. how do I add it to my codes ?.

Build.Gragle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        compose_version = '1.0.1'
    }
    repositories {

        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Build.Gradle(module)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id  'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'org.jetbrains.kotlin.android.extensions'


}


android {
    compileSdk 32

    defaultConfig {
        applicationId "com.enestigli.dictionaryapp"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }


    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    androidExtensions {
        experimental = true
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"



    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "com.squareup.okhttp3:okhttp:4.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-android-compiler:2.38.1"
    //implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'

    // Coroutine Lifecycle Scopes
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"

    // Room
    implementation "androidx.room:room-runtime:2.3.0"
    kapt "androidx.room:room-compiler:2.3.0"


    // To use Kotlin annotation processing tool (kapt)


    // Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:2.3.0"


    //Material theme3
    implementation "androidx.compose.material3:material3:1.0.0-alpha12"
    implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12"

    //skrape{it}
    //implementation 'org.jsoup:jsoup:1.12.1'

    //compose
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

}

EDIT

I found the solution

In this case, you need to install whatever Kotlin version is. In my case like this

my kotlin version is : 1.5.21

and I added this dependency in gradle.plugin(module)

plugins {
    
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.21'

}

and I added this

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1'
}

and that's it, now you can use serialization in your data class like this.

for example

import kotlinx.serialization.Serializable

    @Serializable
    data class example(
        val example: String,
       
    )
NewPartizal
  • 604
  • 4
  • 18

0 Answers0