How to use Kotlinx serialization with jvm plugin - kotlin

I can't use Kotlinx serialization with the Kotlin JVM plugin
In the instructions for Groovy DSL:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.5.0'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.0'
}
Because I already use the org.jetbrains.kotlin.jvm, I want to use it with the JVM plugin but the instructions do not explicitly show how.
In build.gradle, I tried using:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.5.0'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.0'
...
}
In code:
import kotlinx.serialization.*
And, I get a compilation error (Unresolved reference: serialization). How can I use kotlinx serialization with the JVM plugin? Also, I need it in the Groovy DSL syntax. The instructions already show it for the Kotlin DSL but I don't use it. Can anyone help?

The plugin is not sufficient to use Kotlinx Serialization, you also need the runtime library to use classes from the kotlinx.serialization.* packages.
This is covered in the docs:
https://github.com/Kotlin/kotlinx.serialization#dependency-on-the-json-library
In Gradle, this means you need to add Kotlinx serialization as a dependency in the dependencies block:
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1"
}
If you need other formats than JSON, you'll need to add the corresponding artifact instead.

Related

Gradle generated buildSrc + Kotlin = how do I upgrade to Java 17?

I'm starting a new Kotlin project, and I used Gradle 7.2 to generate the project structure and the buildSrc scripts. I'm not including them here because I have not changed them - I'm just using whatever Gradle generated.
I'm getting the following message as part of the build:
'compileJava' task (current target is 17) and 'compileKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
I can't find where in the buildSrc and the generated Gradle files the 1.8 target is set. How can I tell the Kotlin compiler to use the Java 17 target?
The recommended way to address that from Kotlin 1.5.30 is to use the Java Toolchain feature:
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

Why does Gradle force a Kotlin version, and how to fix it?

I have a simple Gradle project that includes the following configuration:
plugins {
kotlin("jvm")
}
This does different things when running with different gradle versions:
$ ./gradlew wrapper --gradle-version=7.5
$ ./gradlew --version
Kotlin: 1.6.21
$ ./gradlew wrapper --gradle-version=7.4.2
$ ./gradlew --version
Kotlin: 1.5.31
$ ./gradlew wrapper --gradle-version=7.2
$ ./gradlew --version
Kotlin: 1.5.21
OK, so Gradle is determining the version of the Kotlin plugin. Bonus questions: Why does Gradle do this? Is this version link documented somewhere?
I'd like to specify the Kotlin version myself. So I change my config:
plugins {
kotlin("jvm") version "1.6.10"
}
Now Gradle complains:
Plugin request for plugin already on the classpath must not include a version
I've checked, and nowhere else in my project do I define a Gradle version explicitly.
Main question: How do I make Gradle use a Kotlin version of my choice?
From my experience the below configuration works fine for a single-project Gradle project with most gradle version.
plugins {
kotlin("jvm") version "1.6.10"
}
Example: https://github.com/jimmyiu/demo-spring-cache. I changed the kotlin version from 1.6.21 to 1.7.10 which works fine.
Are you working on a multi-project Gradle project with buildSrc folder? Since buildSrc is a special subproject which build your custom kotlin-dsl which can be used by Gradle.
This can answer your bonus question: for simplicity, buildSrc should be built to something that can be understood by your project specified Gradle version, hence it must have the same kotlin version as your selected Gradle.
If you root project set the Kotlin version to a version that not align with your Gradle, buildSrc project cannot be built.
To properly configure this, you can:
// root project’s build.gradle.kts
plugins {
kotlin("jvm") version "1.6.10" apply false
}
// in each subproject (except buildSrc):
plugins {
java
kotlin("jvm")
}
About the output of ./gradlew --version, the Kotlin version showing in the output is the build information of that Gradle version, but not your project.
$./gradlew --version
------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------
Build time: 2020-08-25 16:29:12 UTC
Revision: f2d1fb54a951d8b11d25748e4711bec8d128d7e3
Kotlin: 1.3.72
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM: 11.0.15 (Private Build 11.0.15+10-Ubuntu-0ubuntu0.20.04.1)
OS: Linux 5.13.0-52-generic amd64
As an example, Gradle 6.6.1 is built by Kotlin 1.3.72 so that the buildSrc folder should use Kotlin 1.3.72, but the rest of your project can use another Kotlin version.
Reference:
detail explanation of buildSrc: Configure Kotlin extension for Gradle subprojects
suggested solution reference: https://discuss.kotlinlang.org/t/specifying-the-kotlin-version-in-gradle-multi-project-builds/23838
About apply false: https://docs.gradle.org/current/userguide/plugins.html#sec:subprojects_plugins_dsl

Android lint AnnotationProcessorOnCompilePath with ButterKnife

After upgrading to latest Android gradle plugin:
classpath 'com.android.tools.build:gradle:3.6.1'
Android lint (./gradlew lint) getting following warning:
Warning: Add annotation processor to processor path using annotationProcessor instead of implementation [AnnotationProcessorOnCompilePath]
implementation 'com.jakewharton:butterknife:10.2.1'
My app/build.gradle file:
dependencies {
[...]
implementation 'com.jakewharton:butterknife:10.2.1'
kapt 'com.jakewharton:butterknife-compiler:10.2.1'
}
Which is correct according to ButterKnife documentation: https://github.com/JakeWharton/butterknife#download
Hacky solution would be to suppress //noinspection AnnotationProcessorOnCompilePath for ButterKnife.
But how to fix this problem properly?
Apparently, it's a bug on Lint side. They say it will be fixed on the upcoming 4.0 release.
Source:
https://issuetracker.google.com/issues/140881211

react-native link adds compile to gradle script, but compile support soon gone

Whenever I use react-native link, it adds the following to my app gradle file.
Image from Android Studio Gradle File
Then when I compile this, Android Studio tells me that "compile" is being deprecated and won't be supported after 2018. My question is, what would the correct replacement be for, for instance, react-native-ionicons?
The section of the gradle file in question is as follows.
dependencies {
compile project(':react-native-vector-icons')
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.facebook.react:react-native:+'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
testImplementation 'junit:junit:4.12'}
It was due to react-native link not having been updated but as of React-Native 0.58.3 the linking procedure has been updated so that compile and api have been replaced with implementation. Which you can see at this pull request
You could also just change compile to implementation in your grade file.
However you may find that some of the build.gradle files for the dependencies still use compile and api, unfortunately they only way to fix warnings from those is to make a pull request on the dependency changing compile and api to implementation

Error:Unable to resolve version for dependency 'org.jetbrains.kotlin:kotlin-stdlib-jre7:jar'

I have a project written in java I am integrating kotlin with using gradle. I am trying to follow https://kotlinlang.org/docs/reference/using-gradle.html
My build.gradle
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7"
}
according to Kotlin Error : Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7 the library is only available in kotlin v1.1 and up. When I deploy this library and use it in my android project I get the error in the title.
you need to replace
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7"
with
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
Starting with Kotlin 1.1.2 and if you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions.
Use one of the following dependencies:
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"