Android Studio: Kotlin Serialization の使い方

難しいのは、Gradle の設定です。
こちらの設定を参考にしました。
[インターネットからデータを取得する](https://developer.android.com/codelabs/basic-android-kotlin-compose-getting-data-internet?hl=ja#0)

## プログラム
JSON に変化したストリングを、ログに出力します。

```kt:MainActivity.kt
package com.example.json04

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.serialization.json.Json
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

@Serializable
data class PlatformInfo(
  val platformName: String,
  val apiLevel: Int
)
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        println("*** aaa ***")

        @Serializable
        data class Data(val id: Int, val name: String)
        val datax = Data(1, "太郎")
        val json = Json.encodeToString(serializer<Data>(), datax)

        println(json)

        println("*** hhh ***")
    }
}
```

## Gradle Scripts
plugins を加えます。

```textbuild.gradle.kts (Project: json4)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.1.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("org.jetbrains.kotlin.plugin.serialization") version "1.8.10"

}
```

plugins と dependencies を加えます。

```textbuild.gradle.kts (Module: app)
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.serialization") version "1.8.10"
}

android {
    namespace = "com.example.json04"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.json04"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = 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"
    }
}

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.10.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
```

## Build

File -> Sync Project with Gradle Files

Build -> Build bundle(s)/ APK(s) ->  Build APK(s)

## 実行

右三角をクリック

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/de2db706-a191-154e-494a-499873b29484.png)

## 実行結果

View -> Tool Windows -> Logcat

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/52d3cb35-db41-f860-e669-bad9cbaf70ce.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/ef6cb191-d270-15c5-4b85-30ba6cc53fbc.png)

```text
2023-10-16 19:48:05.593 17858-17858 Compatibil...geReporter com.example.json04                   D  Compat change id reported: 210923482; UID 10189; state: ENABLED
2023-10-16 19:48:05.603 17858-17858 System.out              com.example.json04                   I  *** aaa ***
2023-10-16 19:48:05.614 17858-17858 System.out              com.example.json04                   I  {"id":1,"name":"太郎"}
2023-10-16 19:48:05.614 17858-17858 System.out              com.example.json04                   I  *** hhh ***
```