Reputation: 1261
I'm having a heck of a time adding Kotlin serialization to a new Android project. I'm using Android Studio Chipmunk | 2021.2.1 Patch 2
.
When I add the plugin to the project-level build.gradle
(see below), I get this sync error:
Build file '/Users/sean/Desktop/code/GradleTest/app/build.gradle' line: 3
An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.android']
> Failed to apply plugin 'org.jetbrains.kotlin.android'.
> Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension.
> Companion
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.android']
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin 'org.jetbrains.kotlin.android'
Caused by: org.gradle.api.reflect.ObjectInstantiationException: Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
Caused by: java.lang.NoSuchFieldError: Companion
Here's the project-level build.gradle
where I've attempted to add the plugin:
buildscript {
ext {
compose_version = '1.1.0-beta01'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here's the app module build.gradle
which is just the default one that's generated when you create a new project:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.sthomas.gradletest"
minSdk 25
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}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha01'
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"
// implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'
}
Here's settings.gradle
which is also default:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "GradleTest"
include ':app'
Upvotes: 15
Views: 20413
Reputation: 820
If you are using Kotlin DSL then you can follow the below steps add the below code in the level gradle file
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10" apply false
Go to app level gradle file add the below code in the plugin
plugins {
id("com.android.application")
..
..
..
id("org.jetbrains.kotlin.plugin.serialization")
}
add the below dependency in the dependency section
// KotlinX Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
Upvotes: 4
Reputation: 3169
First step is on build.gradle
from project level: You forgot the apply false
in front of the version in the plugin line.
plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10' apply false
}
Second step is on build.gradle
from app module: Apply the plugin
and add the dependency
.
plugins {
id 'org.jetbrains.kotlin.plugin.serialization'
}
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
}
With that your project should work, but I would like to draw attention to a possible incompatibility issue if you use different versions of Koltin
and Kotlin Plugins
. And since you are using Compose
, it is necessary to pay attention that the Compiler
version of Compose
needs to be compatible with the Kotlin
version as well and that currently the Compiler
version of Compose
is no longer linked to versions of other Compose
libraries. To know more you can take a look at this link.
So, to improve version compatibility in your project you can do this:
buildscript {
ext {
kotlin_ver = '1.7.10'
compose_compiler_ver = '1.3.0'
compose_ver = '1.2.1'
}
}
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version "$kotlin_ver" apply false
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_ver" apply false
}
// ...
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.serialization'
}
android {
// ...
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_ver
}
// ...
}
dependencies {
// ...
implementation "androidx.compose.ui:ui:$compose_ver"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ver"
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
//...
}
Upvotes: 41