I am trying to add some simple JSON deserializatio...
# android
t
I am trying to add some simple JSON deserialization to our app; I'm trying to follow the instructions here: https://kotlinlang.org/docs/serialization.html. What's befuddling me is where I'm supposed to put these two build.gradle lines:
Copy code
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.7.10'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10'
}
There's two build.gradle files. Which one? I've tried placing it in both of them at various locations, and I always get errors about it needing to above or before one thing or another. Is the snippet/instructions up to date?
p
At the very top of the file in your app module’s
build.gradle
should work
j
I recommend you to use Parcelize: https://developer.android.com/kotlin/parcelize
🚫 3
Like Patrick said, the app module’s is the one located at the very top level.
t
Can you share more why (parcelize vs this one; I'd like to avoid a bunch of external dependencies, but these both look stock, is one or the other newer) @Jorge Cordero Camarena?
e
parcelize is not suited for general serialization. it is not stable across OS or app versions
👍 2
the default project created by Android Studio puts the app module as a submodule, not the root module
you should already have
plugins { id("org.jetbrains.kotlin.android" }
or
plugins { kotlin("android") }
near the top of your build script, just add
id("org.jetbrains.kotlin.plugin.serialization")
or
kotlin("plugin.serialization")
there as well
the plugin stanza needs to be one of the first things in the file - after
buildscript
if it exists, but before anything else (except comments and imports)
t
I have these 3 lines at the top of my file:
Copy code
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
I tried to recast this entry as something like:
Copy code
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'
But it didn't go for that. I did find that if I removed the first line, then it worked
c
It might help having some clarification on exactly what these various `.gradle`/`.gradle.kts` files are actually there for: •
settings.gradle
sits in the root of your repo and tells Gradle some basic info for configuring the entire project as a whole. Most notably, what subprojects you have (the
include(":app")
lines in it) • For every subproject listed in
settings.gradle
, a folder at the corresponding path (replace
:
with
/
) should have a
build.gradle
file. This file configures that specific subproject, and is typically where you’d add your
plugins { }
block or
apply plugin: ""
lines • Additionally, you’ll probably have one more
build.gradle
file at the root of the repo. While this is a subproject just like the others, you might also see some stuff here that applies plugins or makes other configurations to all other subprojects. Generally, this is not the place to add plugins such as
id 'org.jetbrains.kotlin.jvm'
or
id 'org.jetbrains.kotlin.plugin.serialization'