ファイルの末尾に改行を追加するように Android Studio を設定するにはどうすればよいですか?

🧑‍💻 NewLineAtEndOfFile

👉 【AndroidStudio】detekt で JetpackCompose 記述をチェックする hatena-bookmark

つい忘れてしまうファイル最後の改行。


[ Settings... ]

  ↓

[ Editor ]

  ↓

[ General ]

  ↓

[ Ensure every saved file ends with a line break ]

ファイルの末尾に新しい行を追加するように Android Studio を設定するにはどうすればよいですか?

これで、ファイルを開いた時点で最終行の改行は自動で必ず付いている、自動保存なので。

てか、IDE デフォルトで ON でもいいように思う機能。

 

🧑‍💻 と思ったら付かないので「Keep tarailing spaces on caret line」を OFF に

「Keep tarailing spaces on caret line」 を OFF にしないと付きません。

あと、余計な末尾の空白行は、「Remove trailing blank lines at the end of saved files」を ON にすると自動で消してくれます。

 

🧑‍💻 自動保存のタイミングはいつなのか

AndroidStudio デフォルトの設定では、

アプリを切り替えたとき、

または、ビルトインのターミナルに切り替えたとき

に自動保存されるようです。

自動保存のタイミングはいつなのか

なので、上で述べた最終行まわりの自動処理はその時に行われます。

自動保存のタイミングはいつなのか

 

🧑‍💻 まとめ

自動保存時に、

ファイル末尾の改行がなかったら付ける。

ファイル末尾の無駄な空白行は削除する。

の AndroidStudio の設定は以下。


[ Settings... ]

  ↓

[ Editor ]

  ↓

[ General ]

  ↓

□ [ Keep tarailing spaces on caret line ] OFF
✅ [ Ensure every saved file ends with a line break ] ON
✅ [ Remove trailing blank lines at the end of saved files ] ON

こんな機能あるとは知らなかったです。便利です。

👉 【AndroidStudio】detekt で JetpackCompose 記述をチェックする hatena-bookmark


AndroidX が minSdkVersion 19 に移行

AndroidX ライブラリは、2023 年 10 月のリリースから、サポートされるデフォルトの最小 Android API レベル 19 (以前は 14) に移行します。Play ストアのチェックイン データによると、ほぼすべての Android ユーザーが API 19 以降のデバイスを使用しているため、API レベル 19 ではなくなりました。レガシーバージョンをサポートするために必要です。この変更は、AndroidX ライブラリがアプリ開発者の潜在的なユーザー数を最大化し、Google Play ServicesおよびAndroid NDKと連携するのに役立ちます。

現在、より低い minSdkVersion をサポートしている場合は、その値を 19 に増やし、以前のバージョンをサポートするようにコードをクリーンアップすることをお勧めします。ビジネス上の理由でそれができない場合は、AndroidX の以前のバージョンを使用し続けることをお勧めします。


👉 Android Developers Blog: AndroidX moving to minSdkVersion 19 hatena-bookmark

「API レベル 19」てのは、「Android OS 4.4 / Kitkat」です。

10年前ぐらいにリリースされました。


2023-10-17
+-----+------+--------------+-------+-------+----------+----------+
| API |   OS | CODE_NAME    |  WW % |  JP % | WW SUM % | JP SUM % |
+=====+======+==============+=======+=======+==========+==========+
|  19 |  4.4 | KITKAT       |  0.31 |  0.27 |    99.70 |    99.63 |
+-----+------+--------------+-------+-------+----------+----------+
|  21 |  5.0 | LOLLIPOP     |  0.43 |  0.22 |    99.39 |    99.36 |
+-----+------+--------------+-------+-------+----------+----------+
|  22 |  5.1 | LOLLIPOP_MR1 |  0.89 |  1.73 |    98.96 |    99.14 |
+-----+------+--------------+-------+-------+----------+----------+
|  23 |  6.0 | M            |  1.48 |  1.07 |    98.07 |    97.41 |
+-----+------+--------------+-------+-------+----------+----------+
|  24 |  7.0 | N            |  1.81 |  1.78 |    96.59 |    96.34 |
+-----+------+--------------+-------+-------+----------+----------+
|  25 |  7.1 | N_MR1        |  0.52 |  0.50 |    94.78 |    94.56 |
+-----+------+--------------+-------+-------+----------+----------+
|  26 |  8.0 | O            |  3.07 |  2.94 |    94.26 |    94.06 |
+-----+------+--------------+-------+-------+----------+----------+
|  27 |  8.1 | O_MR1        |  2.50 |  0.78 |    91.19 |    91.12 |
+-----+------+--------------+-------+-------+----------+----------+
|  28 |  9.0 | P            |  7.01 | 10.43 |    88.69 |    90.34 |
+-----+------+--------------+-------+-------+----------+----------+
|  29 | 10.0 | Q            |  9.07 |  9.95 |    81.68 |    79.91 |
+-----+------+--------------+-------+-------+----------+----------+
|  30 | 11.0 | R            | 18.73 | 13.09 |    72.61 |    69.96 |
+-----+------+--------------+-------+-------+----------+----------+
|  31 | 12.0 | S            | 18.93 | 19.03 |    53.88 |    56.87 |
+-----+------+--------------+-------+-------+----------+----------+
|  33 | 13.0 | T            | 34.92 | 37.75 |    34.95 |    37.84 |
+-----+------+--------------+-------+-------+----------+----------+
|  34 | 14.0 | U            |  0.03 |  0.09 |     0.03 |     0.09 |
+-----+------+--------------+-------+-------+----------+----------+

👉 Android OS version market share hatena-bookmark

もういいわな、世界でも、日本でも1%に満たないんだから。


Unresolved reference: BuildConfig

Android Gradle Plugin (AGP) 8.0 以降、BuildConfig ファイルの生成はデフォルトで無効になっており、必要なモジュールに対してのみ手動で有効にする必要があります。

モジュール別であれば、


// build.gradle.kts

android {
  buildFeatures {
    buildConfig = true
  }
}

すべてのモジュールに適用する場合は以下。


# gradle.properties

android.defaults.buildfeatures.buildconfig=true

Unresolved reference: BuildConfig



【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