【AndroidStudio】Debug Console の ログ フィルター がないので fold する方法

Retrofit でテストしてみたらログが見づらい。

WARNING: An illegal reflective access operation has occurred

何なんですかね。

まずこれ。


WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by retrofit2.Platform (file:/Users/mersan/.gradle/caches/modules-2/files-2.1/com.squareup.retrofit2/retrofit/2.8.0/53fa357bd7538d2c4872bddf33654f113cf6652b/retrofit-2.8.0.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of retrofit2.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Groovy と Java のバージョンによる相性なのか、なんなのか、よくわからん。

👉 [GROOVY-8339] Fix warning "An illegal reflective access operation has occurred" - ASF JIRA hatena-bookmark
👉 [GROOVY-9103] CLONE - CLONE - Fix warning "An illegal reflective access operation has occurred" - ASF JIRA hatena-bookmark

これについて、Jake さんは問題ないと言っています。

The reflection works around a bug in the JDK which was fixed in 14 but it's only used for default methods. As it's only a warning, it's not preventing your call from working.

👉 Illegal reflective access · Issue #3341 · square/retrofit hatena-bookmark

あと、Retrofit のログも見づらい。


Apr 12, 2023 10:19:16 AM okhttp3.internal.platform.Platform log
INFO: Server: cloudflare
Apr 12, 2023 10:19:16 AM okhttp3.internal.platform.Platform log
INFO: CF-RAY: 7b67a3d78f9f2647-NRT
Apr 12, 2023 10:19:16 AM okhttp3.internal.platform.Platform log
INFO: alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
Apr 12, 2023 10:19:16 AM okhttp3.internal.platform.Platform log
INFO: <-- END HTTP

まあ、Level.NONE にすれば消えるのだが、通信状態は見えなくなるし。


val interceptor = HttpLoggingInterceptor().apply {
  level = if (BuildConfig.DEBUG) Level.BODY else Level.NONE
}

👉 【Retorofit】コピペで使える NetworkModule【Dagger Hilt】 hatena-bookmark

 

■ Filter messages in Android Studio debug console

こんな機能あったんですね。

Right click on the line you want to hide, on the popup menu click "Fold lines like this". You'll be prompted a window where you can edit the folded lines content.

Ps. Folded lines content work the way contains string method does.

👉 Filter messages in Android Studio debug console - Stack Overflow hatena-bookmark

隠したい行で右クリックから Fold Lines Like This です。

折り畳まれて、とりあえずは見やすくなりました!

まだまだ使ったことのない知らない機能がたくさんありますわあ。


ChatGPTアプリで表を出力させる - Markdown Text for Android JetpackCompose

ChatGPT は Markdown 出力することができますよね。

それを Jetpack Compose で表示させましょう。

ChatGPTアプリで表を出力させる - Markdown Text for Android JetpackCompose

すごく分かりやすいですね。



以下のライブラリを使いました。

👉 jeziellago/compose-markdown: Markdown Text for Android Jetpack Compose 📋. hatena-bookmark

利用記述はシンプルに作成されています。


@Composable  
fun ComplexExampleContent() {  
  MarkdownText(
    modifier = Modifier.padding(8.dp),
    markdown = markdown,
    textAlign = TextAlign.Center,
    fontSize = 12.sp,
    color = LocalContentColor.current,
    maxLines = 3,
    fontResource = R.font.montserrat_medium,
    style = MaterialTheme.typography.overline,
  )  
}

続いて、Markdown で Mermaid や Planet UML で図を出力もできそうです。

ちなみに、このライブラリは、さらに以下のライブラリたちに順に依存しています。

👉 Markwon/app-sample at master · noties/Markwon hatena-bookmark
👉 CommonMark hatena-bookmark



Android JetpackCompose での OptionMenu - Alternative for OptionMenu in JetpackCompose

Android JetpackCompose で TopAppBar に入れるべく「メニューボタン」をどうするか。

こんな感じになりました。

今現在は、この Box + DropDownMenu でいこうかと思っています。

以下記事から。

A DropdownMenu behaves similarly to a Popup, and will use the position of the parent layout to position itself on screen. Commonly a DropdownMenu will be placed in a Box with a sibling that will be used as the 'anchor'.

DropdownMenu は Popup と同様の動作をし、親レイアウトの位置を利用して画面上に配置されます。DropdownMenu
は「アンカー」として使用される兄弟を持つ Box に配置されます。

👉 android - Jetpack compose popup menu - Stack Overflow hatena-bookmark


@Composable
fun MenuButton() {

  val context = LocalContext.current
  var expanded by remember { mutableStateOf(false) }

  Box {

    IconButton(onClick = { expanded = true }) {
      Icon(Icons.Filled.MoreVert, null)
    }

    DropdownMenu(
      expanded = expanded,
      onDismissRequest = { expanded = false },
    ) {
      DropdownMenuItem(
        onClick = {
          expanded = false
          context.startActivity(
            Intent(Intent.ACTION_VIEW, Uri.parse("https://platform.openai.com/account/usage"))
          )
        }
      ) {
        Text(text = "Usage")
      }
      Divider()
      DropdownMenuItem(onClick = {
        expanded = false
        context.startActivity(
          Intent(Intent.ACTION_VIEW, Uri.parse("https://platform.openai.com/docs/api-reference"))
        )
      }) {
        Text(text = "API reference")
      }
    }

  }

}

こんなところでしょうか。

TopAppBar にセットするなら、以下のように action へ。


TopAppBar(
  title = { Text("Simple TopAppBar") },
  navigationIcon = {
    IconButton(onClick = { /* doSomething() */ }) {
      Icon(Icons.Filled.Menu, contentDescription = null)
    }
  },
  actions = {
    MenuButton()
  }
)

汎用性を上げるなら引数を考慮して、labelaction のペアのリストを渡してループ、などするとよいです。

👉 DropdownMenu | androidx.compose.material  |  Android Developers hatena-bookmark



Kotlin で Jetpack Compose, Retrofit を使って API リクエスト、JSON パース して、グリッド表示するコードをファイル別に表示して

Kotlin で Jetpack Compose と Retrofit を使って API リクエスト、JSON パースして、グリッド表示するためには、以下のようにファイルを分けることができます。

 

1. API リクエストを行うための Retrofit インターフェースを定義するファイル


interface ApiService {

    @GET("/photos")
    suspend fun getPhotos(): List<Photo>
}

 

2. API レスポンスを表すデータクラスを定義するファイル


data class Photo(
    val id: Int,
    val title: String,
    val url: String
)

 

3. API レスポンスをパースして、表示するための画面を定義するファイル


@Composable
fun PhotoGrid() {
    val photos by remember { mutableStateOf<List<Photo>>(emptyList()) }

    LaunchedEffect(Unit) {
        val apiService = Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com")
            .addConverterFactory(Json {
                ignoreUnknownKeys = true
            }.asConverterFactory("application/json".toMediaType()))
            .build()
            .create(ApiService::class.java)

        photos = apiService.getPhotos()
    }

    LazyVerticalGrid(cells = GridCells.Adaptive(minSize = 128.dp)) {
        items(photos) { photo ->
            Card(
                modifier = Modifier.padding(4.dp),
                shape = RoundedCornerShape(8.dp),
                elevation = 4.dp
            ) {
                Column(
                    Modifier
                        .fillMaxWidth()
                        .height(128.dp)
                        .padding(8.dp)
                ) {
                    Text(
                        text = photo.title,
                        style = MaterialTheme.typography.subtitle1,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                    Spacer(modifier = Modifier.weight(1f))
                    CoilImage(
                        data = photo.url,
                        contentScale = ContentScale.Crop,
                        modifier = Modifier.fillMaxSize()
                    )
                }
            }
        }
    }
}

 

4. 上記の画面を表示するための Activity もしくは Fragment を定義するファイル


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            PhotoGrid()
        }
    }
}

以上のように、ファイルを分けることで、API リクエストやパース、表示を分離し、コードを見やすく、メンテナンスしやすくすることができます。

 

■ 感想

ただ、一行問いかけただけ。

ChatGPT で、ここまで書けるとは。



【Android】Version Catalog + JetpackCompose UI + BOM

Compose の BOM。

【Android】Version Catalog + JetpackCompose UI + BOM
👉 Google's Maven Repository hatena-bookmark

公開されているので使ってみます。

少し公式ドキュメントで調べておきます。

BOM の使用は義務ですか?

いいえ。個々の依存関係バージョンを手動で追加することもできます。しかし、すべての最新の安定版を同時に使用することが容易になるので、BOM を使用することをおすすめします。

Compose Compiler ライブラリが BOM に含まれていないのはなぜですか?

Compose Kotlin コンパイラ拡張機能(androidx.compose.compiler)は、Compose ライブラリのバージョンでなく、Kotlin コンパイラ プラグインのバージョンにリンクされており、Compose の他の部分とは異なる頻度でリリースされます。したがって、お使いの Kotlin バージョンと互換性のあるバージョンを使用してください。

👉 Using the Bill of Materials  |  Jetpack Compose  |  Android Developers hatena-bookmark

公式サンプルコードを少し変更したものを準備しておきます。


# libs.versions.toml

[libraries]
compose-ui-bom = { module = "androidx.compose:compose-bom", version = "2023.01.00" }


// build.gradle

dependencies {
  implementation platform(libs.compose.ui.bom)
}

👉 BOM to library version mapping  |  Jetpack Compose  |  Android Developers hatena-bookmark

 

■ 実装

実装としては、以下でいけました。


# libs.versions.toml

[libraries]

compose-ui-bom = { module = "androidx.compose:compose-bom", version = "2023.01.00"}
compose-material = { module = "androidx.compose.material:material" }
compose-material-icons = { module = "androidx.compose.material:material-icons-extended" }
compose-ui = { module = "androidx.compose.ui:ui" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }


// build.gradle

dependencies {

  // compose-ui
  implementation platform(libs.compose.ui.bom)
  implementation libs.compose.material
  implementation libs.compose.material.icons
  implementation libs.compose.ui
  implementation libs.compose.ui.tooling.preview

}

 

■ まとめ

BOM を使っていなかったのですが、エラーが出始めたので、使うことにしました。

「整合性の適正化」と「グループ化」の機能を持っている「BOM」は、Kotlin, Firebase などでも公開されています。

便利に利用するべきでしょう。

しかし、AndroidStudio のコレ。

JetpackCompose UI で BOM

「null」 の表示。

どうにかなりませんかね。

👉 今はまだ必要な Android ライブラリ の Bill of Materials(BOM) のURLs hatena-bookmark