LoginSignup
6
2

More than 1 year has passed since last update.

Jetpack Composeのbuild.gradleをKotlin 1.7.10に対応させる。

Last updated at Posted at 2022-09-10

業務でSwiftUIを1年半ほど触っていたのですが、最近Jetpack Composeに触り始めて、そのCodelabの充実とモジュールの柔軟さに衝撃を受けております。

Firebaseを使いたい

趣味で開発しているアプリのデータベースにFirebaseを使いたいなと思い、調べていたところ、Firebaseから素晴らしい公式のサンプルプロジェクトが出ていました。こちらを写経しつつ自分のアプリに組み込もうとしていたら、ビルドで詰まりました。

エラー内容

どうやらどこかのmetaデータのKotlinバージョンと実際のバージョンが合っていない模様。

/Users/(username)/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.0/(hash)/kotlin-stdlib-common-1.7.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: 
Module was compiled with an incompatible version of Kotlin. 
The binary version of its metadata is 1.7.1, expected version is 1.5.1.

デフォルトではKotlin 1.5.31

なんでか「Empty Compose Activity」で自動的に設定されるCompose Compilerのバージョンは1.1.0-beta01、Kotlinのバージョンは1.5.31となっております。そのままではFirebase用のimplimentationを色々追加した後だと、ビルドが通りません。

結論:build.gradle(Project)

Kotlinのバージョンを1.7.10に上げて、それに合わせてCompose Compilerのバージョンを1.3.0にします(20220911時点で最新安定版)
KotlinとCompose Compilerの対応関係はこちらを参照。

buildscript {
    ext {
        compose_version = '1.3.0' // <- 変更箇所1
    }
    ....
}
plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false // <- 変更箇所2

}

task clean(type: Delete) {
    delete rootProject.buildDir
}

結論: build.gradle(App)

この部分がこの記事の肝です。compose_versionの箇所を具体値(1.2.1)に変更します。(compose_versionだと1.3.0になるが、これに該当するものが(20220911時点で)ないため -> 参考)

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation "androidx.compose.ui:ui:1.2.1" // <- 変更箇所1
    implementation "androidx.compose.material:material:1.2.1" // <- 変更箇所2
    implementation "androidx.compose.ui:ui-tooling-preview:1.2.1" // <- 変更箇所3
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    implementation 'androidx.activity:activity-compose:1.5.0-alpha01'
    implementation "androidx.navigation:navigation-compose:2.5.0-alpha01"
    implementation 'androidx.preference:preference-ktx:1.1.1'

    ....

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.2.1"  // <- 変更箇所4
    debugImplementation "androidx.compose.ui:ui-tooling:1.2.1" // <- 変更箇所5
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.2.1" // <- 変更箇所6
}

まとめ

デフォルトで古いバージョンになるの、何故なんでしょう...。
build.gradle関連むずいです。

6
2
2

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
6
2