0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Android Studio: Kotlin Serialization の使い方

Last updated at Posted at 2023-10-16

難しいのは、Gradle の設定です。
こちらの設定を参考にしました。
インターネットからデータを取得する

プログラム

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

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 を加えます。

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 を加えます。

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

実行結果

View -> Tool Windows -> Logcat

image.png

image.png

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 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?