【Android Studio】[File] - [Project Structure] - [Suggestions] で「Update」する dependencies の妥当性 💉

Suggestions に表示されてるので Update する。

androidx.navigation:navigation-compose が 2.7.0-beta02 で NG だが 2.7.0-beta01 で OK な件


androidx.navigation:navigation-compose:2.7.0-beta01

  ↓

androidx.navigation:navigation-compose:2.7.0-beta02

はい、NG です。

androidx.navigation:navigation-compose が 2.7.0-beta02 で NG だが 2.7.0-beta01 で OK な件

なんでや!?

 

💉 エラーメッセージ 「5 issues were found when checking AAR metadata」

エラーメッセージを見てみる。


5 issues were found when checking AAR metadata:


  1.  Dependency 'androidx.navigation:navigation-common:2.7.0-beta02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 8.0.1 is 33.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 34, then update this project to use
      compileSdk of at least 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).


  2.  Dependency 'androidx.navigation:navigation-runtime:2.7.0-beta02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 8.0.1 is 33.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 34, then update this project to use
      compileSdk of at least 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).

  3.  Dependency 'androidx.navigation:navigation-common-ktx:2.7.0-beta02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 8.0.1 is 33.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 34, then update this project to use
      compileSdk of at least 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).

  4.  Dependency 'androidx.navigation:navigation-runtime-ktx:2.7.0-beta02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 8.0.1 is 33.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 34, then update this project to use
      compileSdk of at least 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).

  5.  Dependency 'androidx.navigation:navigation-compose:2.7.0-beta02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

      :app is currently compiled against android-33.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 8.0.1 is 33.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 34, then update this project to use
      compileSdk of at least 34.

      Note that updating a library or application's compileSdk (which
      allows newer APIs to be used) can be done separately from updating
      targetSdk (which opts the app in to new runtime behavior) and
      minSdk (which determines which devices the app can be installed
      on).

長いですが要するに、


androidx.navigation:navigation-compose:2.7.0-beta02

が依存している以下、


androidx.navigation:navigation-common:2.7.0-beta02
androidx.navigation:navigation-runtime:2.7.0-beta02
androidx.navigation:navigation-common-ktx:2.7.0-beta02
androidx.navigation:navigation-runtime-ktx:2.7.0-beta02

は、


Android API 34

以上を必要としているので使えません。

ということのよう。

 

💉 なぜ私は、Android API 33 だったけか

「Flamingo」なのでそれが最新安定版。

androidx.navigation:navigation-compose が 2.7.0-beta02 で NG だが 2.7.0-beta01 で OK な件

Android Studio Flamingo | 2022.2.1 を使ってる場合、33 までです。

👉 Android Studio Flamingo | 2022.2.1  |  Android Developers hatena-bookmark

 

💉 まとめ

Android Studio の Suggestion ダイアログ

androidx.navigation:navigation-compose が 2.7.0-beta02 で NG だが 2.7.0-beta01 で OK な件


[File]

  |

[Project Structue]

  |

[Suggestions]

は、Android Studio までを含めた全ての依存性を考慮していない

Version Catalog (toml) ですが、なんだか不便です。

👉 【Android Studio Flamingo】dependencies をバージョンアップデートするときに確認するべき3つの相性 hatena-bookmark


Kotlin Serialization を使って JsonObject を作る方法

一番簡単に作る方法はどれなのか、と思いましたので。

サードパーティーは使わず、

すべて kotlinx.serialization で。

以下の JsonObject を作ります。


{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "Server-Sent Events とは"
    }
  ],
  "temperature": 0.7,
  "stream": false
}

 

👨‍🎨 文字列から作る

文字列内に JSON を記述しておいて変換します。


val str = """{
  |  "model": "gpt-3.5-turbo",
  |  "messages": [{"role":"user","content":"Server-Sent Events とは"}],
  |  "temperature": 0.7,
  |  "stream": false
  |}""".trimMargin()
val element = Json.parseToJsonElement(str)

println(element)
println(element.javaClass.simpleName)

// {"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"Server-Sent Events とは"}],"temperature":0.7,"stream":false}
// JsonObject

 

👨‍🎨 データクラスから作る

データクラスを先に作っておきます。


@Serializable
data class ChatRequest(
  val model: String,
  val messages: List<Message>,
  val temperature: Double,
  val stream: Boolean
)

@Serializable
data class Message(
  val role: String,
  val content: String
)

値をセットして変換します。


val data = ChatRequest(
  model = "gpt-3.5-turbo",
  messages = listOf(Message(role = "user", content = "Server-Sent Events とは")),
  temperature = 0.7,
  stream = false
)
val element = Json.encodeToJsonElement(data)

println(element)
println(element.javaClass.simpleName)

// {"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"Server-Sent Events とは"}],"temperature":0.7,"stream":false}
// JsonObject

 

👨‍🎨 Json エレメントビルダー で作る

ビルダー関数 buildJsonArray(), buildJsonObject() を使いながら DSL 形式で JSON の構造を定義していきます。


val element = buildJsonObject {
  put("model", "gpt-3.5-turbo")
  putJsonArray("messages") {
    addJsonObject {
      put("role", "user")
      put("content", "Server-Sent Events とは")
    }
  }
  put("temperature", 0.7)
  put("stream", false)
}

println(element)
println(element.javaClass.simpleName)

// {"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"Server-Sent Events とは"}],"temperature":0.7,"stream":false}
// JsonObject

👉 json-element-builders kotlinx.serialization/json.md at master · Kotlin/kotlinx.serialization hatena-bookmark

 

👨‍🎨 まとめ

文字列から作成する方法が直接的で簡単に書けますね!

テストコードで見かけることも多いように思います。

👉 kotlinx.serialization/json.md at master · Kotlin/kotlinx.serialization · GitHub hatena-bookmark



【Kotlin】kotlinx.serialization で JSON を自在に変換する 🤔

 

🤔 Kotlin Serialization Guide を眺めてみる

JSON 処理なら、ここらですよね。

どんな扱いになっているのか見てみましょうか。


👉 kotlinx.serialization/serialization-guide.md at master · Kotlin/kotlinx.serialization · GitHub hatena-bookmark

基本的な使い方「Chapter 1.Basic Serialization」として、以下のようなデータクラスを例に以下のサンプルコード。


@Serializable
class Project(val name: String, val language: String)


val data = Project("kotlinx.serialization", "Kotlin")
println(Json.encodeToString(data))

// {"name":"kotlinx.serialization","language":"Kotlin"}


val data = Json.decodeFromString<Project>("""
  {"name":"kotlinx.serialization","language":"Kotlin"}
""")
println(data)

// Project(name=kotlinx.serialization, language=Kotlin)

以下のようなイメージのことを書いてる。


                +-----------------------------------+                 
                |                                   |                 
                | Project(                          |                 
                |   name = "kotlinx.serialization", |                 
                |   language = "Kotlin"             |                 
                | )                                 |                 
                |                                   |                 
                +-----------------------------------+                 
                              |      ^                                
                              |      |                                
                              |      |                                
Json.encodeToString(Project)  |      |  Json.decodeFromString(String) 
                              |      |                                
                              |      |                                
                              v      |                                
               +------------------------------------+                 
               |                                    |                 
               | {                                  |                 
               |   "name":"kotlinx.serialization",  |                 
               |   "language":"Kotlin"              |                 
               | }                                  |                 
               |                                    |                 
               +------------------------------------+

汎用的にしてこうですか。


            +-----------------------------------+                          
            |                 T                 |                          
            +-----------------------------------+                          
                            |  ^                                           
                            |  |                                           
                            |  |                                           
Json.encodeToString(T)      |  |     Json.decodeFromString<T>(String)      
                            |  |                                           
                            |  |                                           
                            v  |                                           
           +------------------------------------+                          
           |               String               |                          
           +------------------------------------+

メソッド名的には、


シリアル化  → encode
逆シリアル化 → decode

となってます。

 

🤔 JsonElement

というのがあります。

👉 JsonElement hatena-bookmark

少し試してみます。変換しながら型を見ていきます。


// T
val t = Project(name = "kotlinx.serialization", language = "Kotlin")
println("$t ${t::class.simpleName}")
// Project(name=kotlinx.serialization, language=Kotlin) Project


// T -> JsonElement
val e = Json.encodeToJsonElement(t)
println("$e ${e::class.simpleName}")
// {"name":"kotlinx.serialization","language":"Kotlin"} JsonObject


// JsonElement -> String
val s = Json.encodeToString(e)
println("$s ${s::class.simpleName}")
// {"name":"kotlinx.serialization","language":"Kotlin"} String


// String -> JsonElement
val e2 = Json.parseToJsonElement(s)
println("$e2 ${e2::class.simpleName}")
// {"name":"kotlinx.serialization","language":"Kotlin"} JsonObject


// JsonElement -> T
val t2 = Json.decodeFromJsonElement<Project>(e2)
println("$t2 ${t2::class.simpleName}")
// Project(name=kotlinx.serialization, language=Kotlin) Project

JsonElement の型は JsonObject です。

図にします。


                  +-----------------------------------+                             
                  |                 T                 |                             
                  +-----------------------------------+                             
                                  |  ^                                              
                                  |  |                                              
                                  |  |                                              
  Json.encodeToJsonElement(T)     |  |  Json.decodeFromJsonElement<T>(JsonElement)  
                                  |  |                                              
                                  |  |                                              
                                  v  |                                              
                 +------------------------------------+                             
                 |            JsonObject              |                             
                 +------------------------------------+                             
                                  |  ^                                              
                                  |  |                                              
                                  |  |                                              
Json.encodeToString(JsonElement)  |  |  Json.parseToJsonElement(String)      
                                  |  |                                              
                                  |  |                                              
                                  v  |                                              
                 +------------------------------------+                             
                 |               String               |                             
                 +------------------------------------+

しかし、考えてみると、JsonObject って最近ではあまり見かけなくなった気がします。

 

🤔 まとめ

以上を図にしておきます。


👉 Json.md GitHub Gist hatena-bookmark

String 方向が encode(エンコード)serialize(シリアル化)

T 方向が decode(デコード)deserialize(逆シリアル化)