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

ソフトウェアの開発において、依存関係は重要な要素の一つです。一つのライブラリが別のライブラリに依存している場合、最新バージョンのアップデートを行う際には、注意が必要です。

今回は、依存関係をアップデートする際に留意しておくべきことについて解説します。

で、[File] - [Project Structure] からのこの画面。

This project uses Gradle Version Catalogs. There are some limitations. Learn more.

【Android Studio Flamingo】dependencies をアップデートするときに確認しておくべきこと

[Suggestions] 画面だけで完全に更新できませんよね。

「プラグイン、ライブラリ同士の互換性は考慮されていないものがある。」

「提案されない表示されないライブラリがある。」

ということで、どこに注意しながら新しいものに更新していくとスムーズに進むか、という話です。

 

🔗 Android Studio と Android Gradle Plugin

Android Studio は、Android アプリ開発に必要な開発環境を提供する統合開発環境です。

Android Gradle Plugin (AGP) は、Android Studio のビルドシステムに使用されるツールであり、ビルドプロセスを自動化するために必要です。

Android Studio と Android Gradle Plugin
👉 Android Gradle plugin and Android Studio compatibility - Android Studio Flamingo | 2022.2.1  |  Android Developers hatena-bookmark

Android Studio で使用している Android Gradle Plugin のバージョンを確認するには、プロジェクトの build.gradle ファイルを開き、以下のように dependencies ブロック内に記述されている AGP のバージョン番号を確認します。


dependencies { 
  classpath 'com.android.tools.build:gradle:x.y.z' 
  // ... 
}

または、build.gradle に記述がない Version Catalog 記述の Android Gradle Plugin のバージョンは、以下の、どちらかのプラグインID のバージョン(共通) です。


com.android.application

com.android.library

👉 【Plugin DSL】Android Gradle Plugin のバージョンを調べる方法 hatena-bookmark
👉 【Plugin DSL】「com.android.tools.build:gradle」の記述は不要? hatena-bookmark

Gradle Version Catalogs では以下のようになります。


# libs.versions.toml 

[versions]
android-gradle-plugin = "8.0.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }


// buid.gradle

plugins {
  alias libs.plugins.android.application
}

また、最新の AGP バージョンを確認するには、Google's Maven Repository を参照します。
Google's Maven Repository
👉 com.android.application.com.android.application.gradle.plugin - Google's Maven Repository hatena-bookmark

 

🔗 Android Gradle Plugin と Gradle

Gradle は、オープンソースのビルドツールであり、Java や Kotlin、Groovy などのプログラミング言語に対応しています。Android Studio のビルドシステムには、Gradle が採用されています。

AGP は、Gradle に依存しており、AGP のバージョンアップに伴い、Gradle のバージョンもアップデートする必要があります。

また、Gradle のバージョンアップには、Android Studio のバージョンアップも伴う場合がありますので、注意が必要です。

Android Gradle Plugin は Gradle に依存しているため、AGP をアップデートする場合には、Gradle も同時にアップデートする必要があります。

Android Gradle Plugin と Gradle
👉 Update Gradle - Android Gradle plugin release notes  |  Android Studio  |  Android Developers hatena-bookmark

Gradle のバージョンを確認するには、プロジェクトの gradle/wrapper/gradle-wrapper.properties ファイルを開き、以下のように distributionUrl に記述されている Gradle のバージョン番号を確認します。


distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

また、最新の Gradle バージョンを確認するには、Gradle 公式サイトの Gradle Distributions を参照します。
Gradle Distributions
👉 Gradle Distributions hatena-bookmark

 

🔗 Kotlin と Jetpack Compose Compiler

Kotlin は、Android アプリ開発に必要なプログラミング言語の一つです。Jetpack Compose Compiler は、Kotlin をベースにした Jetpack Compose ライブラリのコンパイラです。

Jetpack Compose は、Android のユーザーインターフェースを作成するための新しい方法を提供するライブラリであり、Kotlin と密接に関連しています。

https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility
👉 Compose to Kotlin Compatibility Map  |  Android Developers hatena-bookmark

Jetpack Compose Compiler のバージョンをアップデートする場合には、Kotlin のバージョンも確認し、必要に応じてアップデートする必要があります。

Kotlin と Jetpack Compose Compiler の依存関係を確認するには、以下のように Kotlin と Compose Compiler のバージョン番号を確認します。


# libs.versions.toml 

[versions]
compose-compiler = "1.4.7"
kotlin = "1.8.21"

[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }


// build.gradle

plugins {
  alias libs.plugins.kotlin.android  // 'kotlin-android'
  alias libs.plugins.kotlin.kapt     // 'kotlin-kapt'
}

android {
  composeOptions {
    kotlinCompilerExtensionVersion libs.versions.compose.compiler.get()
  }
}

また、最新の Kotlin と Jetpack Compose Compiler のバージョンを確認するには、それぞれ以下を参照します。

👉 Maven Repository: org.jetbrains.kotlin » kotlin-gradle-plugin hatena-bookmark
👉 androidx.compose.compiler.compiler - Google's Maven Repository hatena-bookmark

 

🔗 まとめ

Android Studio のビルドシステムには、Android Gradle Plugin が使用されており、AGP は Gradle に依存しています。

また、Kotlin と Jetpack Compose Compiler、そして SDK のライブラリ同士にも依存関係があります。

最新バージョンにアップデートする際には、それらのつながりを確認し、必要に応じて一括でアップデートすることをおすすめします。

コアな部分は Stable 最新バージョンで揃える、とすると、以下が、今現在ベストでしょう。


Android Studio
Flamingo | 2022.2.1

  ↕︎

Android Gradle Plugin
8.0.1

  ↕︎

Gradle
8.1.1


Kotlin
1.8.21

  ↕︎

Jetpack Compose Compiler
1.4.7

すぐ忘れるので libs.versions.toml にコメントで貼りつける。


[versions]
# Android Studio - AGP - Gradle
# Kotlin - Compose Compiler
#
# Android Studio version	Required plugin version
# Giraffe | 2022.3.1	3.2-8.1
# Flamingo | 2022.2.1	3.2-8.0
# https://developer.android.com/studio/releases#android_gradle_plugin_and_android_studio_compatibility
#
# Plugin version	Minimum required Gradle version
# 8.2	8.1
# 8.1	8.0
# 8.0	8.0
# https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
#
# Android Studio :
# https://developer.android.com/studio
#
# Android Gradle Plugin :
# https://maven.google.com/web/index.html#com.android.application:com.android.application.gradle.plugin
#
# Gradle :
# https://services.gradle.org/distributions/
android-gradle-plugin = "8.0.1"

# Compose Compiler Version	Compatible Kotlin Version
# 1.4.7	1.8.21
# 1.4.6	1.8.20
# 1.4.5	1.8.20
# 1.4.4	1.8.10
# https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility
#
# compose-compiler :
# https://maven.google.com/web/index.html?authuser=0#androidx.compose.compiler:compiler
#
# kotlin :
# https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
# https://central.sonatype.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin/
compose-compiler = "1.4.7"
kotlin = "1.8.21"

コメント内URLからリンクをブラウザを開くのは、⌘ (command) + click で。



Android Studio Flamingo 🐦 で Retrofit が「r8 java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType」

はい、でました。


r8 java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

開発環境が変化した時におこる minify / proguard まわりのエラーは面倒ですね😭

 

🐦 R8 full mode がデフォルトに

そのままで「R8 full mode」だそうです。

R8 full mode by default

The last behavior change: R8 is now in full mode by default, enabling app size reductions and performance improvement. You shouldn’t need to update anything for this change, but if you encounter build or runtime failures you should double-check that your keep rules are configured correctly. For guidance on how to configure the keep rules, see Shrink, obfuscate, and optimize your app.

👉 Shrink, obfuscate, and optimize your app  |  Android Studio  |  Android Developers hatena-bookmark
👉 5 ways to prepare your app build for Android Studio Flamingo release | by Boris Farber | Android Developers | Apr, 2023 | Medium hatena-bookmark

R8 がより強く広く効いて不具合を起こす、ってことか。

 

🐦 解決方法

以下を proguard-rules.pro に追加します。Retrofit 公式に載ってました。


# Keep generic signature of Call, Response (R8 full mode strips signatures from non-kept items). 
-keep,allowobfuscation,allowshrinking interface retrofit2.Call 
-keep,allowobfuscation,allowshrinking class retrofit2.Response 
  
# With R8 full mode generic signatures are stripped for classes that are not 
# kept. Suspend functions are wrapped in continuations where the type argument 
# is used. 
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

👉 After enable R8 full mode getting ParameterizedType error · Issue #3751 · square/retrofit hatena-bookmark

まずは、ひとつ解決できました。

いろいろありそうです、Flamingo。


Android Studio Flamingo | 2022.2.1 安定版公開される

更新の通知が来たので入れてみました。

👉 Android Developers Blog: Android Studio Flamingo is stable hatena-bookmark
👉 Android Studio Flamingo | 2022.2.1  |  Android Developers hatena-bookmark

しかし、UIデザインはそのままでした。楽しみにしてたのに。


👉 【Android Studio】Giraffe や Flamingo の New UI がシンプルで素晴らしい件 hatena-bookmark
👉 New UI | IntelliJ IDEA Documentation hatena-bookmark

設定画面から「New UI」は消えている。

しかし、適用する方法はある。


👉 Android Studio Flamingo 隠された新デザイン「New UI」を有効化する2つの方法 🐦 hatena-bookmark



【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 です。

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

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


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 で、ここまで書けるとは。