DEV Community

kaede
kaede

Posted on • Updated on

Android 基礎 -- Part 02 Jetpack Compose で自動生成したプロジェクトのライブラリを調べて更新する

why

https://www.youtube.com/watch?v=r869VVmj26E&list=PLgAI_5b_7BdRaBPAD5RMrzjoSwefDXpkw&index=2

MokeLab さんのこの動画を参考にすると
プロジェクト作成でテンプレートから作っただけだと、全然最新のライブラリを使っていないらしい。

なので 2022-01-02 時点での最新に更新してみる。


ルートとアプリの build.gradle という設定ファイルを確認する

springboot と同じく、Gradle で管理されているプロジェクトなので
build.gradle という設定ファイルがある。

Image description

Android の場合は

  • プロジェクトルートの設定ファイル ( Project: HelloJetpack )
  • そのアプリの設定ファイル ( Module: HelloJetpack.ap )

の 2 つがある



Compose 1.3.2 と追従する Kotlin 1.7.20 への更新


ルートの build.grade で compose_ui を 1.1.1 から 1.3.2 に更新

buildscript {
    ext {
        compose_ui_version = '1.1.1'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
Enter fullscreen mode Exit fullscreen mode

トップレベルだから、これらの設定はサブプロジェクトやモジュールに引き継がれるとコメントから解釈した。

まず Jetpack Compose のバージョンを確認する

https://developer.android.com/jetpack/androidx/releases/compose#versions

Android 公式を見ると 1.3.2 が最新の安定版だと書いてある。

buildscript {
    ext {
        compose_ui_version = '1.3.2'
    }
}
Enter fullscreen mode Exit fullscreen mode

なので compose_ui を 1.3.2 に変更してビルドしてみる

      :app is currently compiled against android-32.
Enter fullscreen mode Exit fullscreen mode

するとモジュールの android-32 と競合するとエラーが出た

      Recommended action: Update this project to use a newer compileSdkVersion
      of at least 33, for example 33.
Enter fullscreen mode Exit fullscreen mode

最新の 33 の Compile Software Dev Kit に変更の推奨メッセージが出ている。


アプリの build.gradle で compileSdk を 32 から 33 に更新

なのでひと足先に、アプリの compileSdk を

android {
    namespace 'com.example.hellojetpack'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.hellojetpack"
        minSdk 31
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }
Enter fullscreen mode Exit fullscreen mode

32 から 33 に変えて、再度ビルドしてみる。

> Task :prepareKotlinBuildScriptModel UP-TO-DATE

BUILD SUCCESSFUL in 1s
Enter fullscreen mode Exit fullscreen mode

すると無事にビルドすることができた。

Image description

targetSdk も 32 から 33 に更新する。
32 のままだと、Spring の TODO があるときのような警告が出ているので。


ルートの build.grade で kotlin を 1.6.10 から 1.7.21 にする

https://kotlinlang.org/docs/releases.html#release-details

kotlin の公式サイトを見ると、1.8 が最新だが、試験的な機能が入っているので安定版ではないように見える。

なのでそれの次に新しい 1.7.21 をいれる。
1.7.21 の欄には Android Studio Dolphin では 1.7.21 のプラグインを入れると Android Studio 自体がアップデートされると書いてある。

Android Studio がアップデートされることに問題を感じないので、1.7.21 を入れることにする。

plugins {
// ...
    id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
}
Enter fullscreen mode Exit fullscreen mode

apply false にしておくと、モジュールで別のバージョンを指定したときに優先してくれるらしい。

これでルートで 1.7.21 にしてビルドすると

> Task :app:compileDebugKotlin FAILED

e: This version (1.1.1) of the Compose Compiler requires 
Kotlin version 1.6.10 

but you appear to be using Kotlin version 1.7.21 
which is not known to be compatible.

Enter fullscreen mode Exit fullscreen mode

コンパイルデバック Kotlin で落ちた。
アプリで compose 1.1.1 のコンパイラが使われているらしい。
そして compose 1.1.1 では Kotlin 1.6.10 が要求されている。


アプリの composeOption を 1.1.1 から 1.3.2 に更新

    composeOptions {
        kotlinCompilerExtensionVersion '1.1.1'
    }
Enter fullscreen mode Exit fullscreen mode

アプリの compose のオプションが 1.1.1 のままだった
ルートに合わせて 1.3.2 に更新してビルドする

e: This version (1.3.2) of the Compose Compiler requires
 Kotlin version 1.7.20 

but you appear to be using Kotlin version 1.7.21 
which is not known to be compatible.  

Enter fullscreen mode Exit fullscreen mode

今度は、compose 1.3.2 が Kotlin 1.7.20 を要求していて
1.7.21 では使えないとでた。


ルートの kotlin を 1.7.21 から 1.7.20 に更新

plugins {
// ...
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}
Enter fullscreen mode Exit fullscreen mode

なので、kotlin を下げてビルドして Gradle Sync する

Image description

すると、今度こそエラーなしでビルドと実行できた。



app の依存を更新

現状を確認

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material: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:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}
Enter fullscreen mode Exit fullscreen mode

初期生成時では

android x の

  • ktx
  • lifecycle-runtime
  • activity-compose
  • compose.material
  • test.ext
  • test.espresso
  • compose.ui

などが入っていて

compose.ui のみが

implementation "androidx.compose.ui:ui:$compose_ui_version"
Enter fullscreen mode Exit fullscreen mode

ルートで定義された compose_ui のバージョンを受け取っている。
なので更新が必要ない。

Image description

他は更新しろと警告が出ている。
なので更新する。


全て Alt Enter でガイドに従って更新する。

Image description

Alt Enter で警告のハイライトをされている場所から推奨するバージョンに変更できる。なので変更する。
IntelliJ で Interface から実装を生やすのと同じ操作だ。

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
// ...
}
Enter fullscreen mode Exit fullscreen mode

compose って名前がついているから全て 1.3.2 かと思ったが
1.3.1 から 1.6.1 までバラバラだった。

ビルド

ビルドすると、無事に通った。

Latest comments (0)