Search code examples
androidkotlin

Unable to add Kotlin Serialization to Android Project


I am unable to import Kotlin serialization, I think I have followed the instructions from the Kotlin docs but when I use the @Serializable annotation I get the following warning:

kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.

Here is my project level build.gradle:

buildscript {
    ext {
        compose_version = '1.5.5'
        kotlin_version = '1.9.20'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '8.1.4' apply false
    id 'com.android.library' version '8.1.4' apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
    id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_version" apply false
}

I created a new module cannotimportserialization just for sanity check and here is the module level (cannotimportserialization) build.gradle file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.serialization'
}

android {
    namespace 'com.example.cannotimportserialization'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.cannotimportserialization"
        minSdk 29
        targetSdk 34
        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 '1.4.3'
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.1'
    implementation platform('androidx.compose:compose-bom:2023.03.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2"
}

Here is the project level settings.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "Test Project"
include ':app'
include ':cannotimportserialization'

warning

What do I do here to apply the compiler plugin to the module?


Solution

  • Based on the response from the developers in the GitHub issue, it's a problem with Kotlin plugin for Android Studio. As I understand, this problem will be present if the plugin version looks like this: 231-1.9.10-release-459-AS9392.1.2311.11076708 (1.9.10 is the key part). You can check it in Settings -> Languages & Frameworks -> Kotlin.

    Solutions (from the same response):

    To fix this problem, you need to update the Kotlin IDE plugin. Unfortunately, 1.9.20 IDE plugin version is not available for Hedgehog 2023.1.1 yet. There are two ways to fix it: