「Could not find androidx.compose.compiler:compiler:○-○-○」が出たときにやったこと

error-image android

はじめに

個人開発でアプリを作ろうと思い、Android Studioで新規にプロジェクトを作成した後とりあえず依存しているライブラリのバージョンを最新にしようとした際に以下のエラーが表示されたので、その対処法を残します。

Execution failed for task ':app:compileDebugKotlin'.
> Could not resolve all files for configuration ':app:kotlin-extension'.
   > Could not find androidx.compose.compiler:compiler:1.4.0-alpha01.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/androidx/compose/compiler/compiler/1.4.0-alpha01/compiler-1.4.0-alpha01.pom
       - https://repo.maven.apache.org/maven2/androidx/compose/compiler/compiler/1.4.0-alpha01/compiler-1.4.0-alpha01.pom
     Required by:
         project :app

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

環境

OS : macOS Monterey version 12.6
CPU : Apple M1 Max
Android Studio : Dolphin | 2021.3.1 Patch 1

エラーが出る前に行っていたこと

プロジェクト作成後にアプリケーションを起動し正常動作を確認したのち、アプリケーション側の build.gradle を確認したところ「androidx.compose」系のライブラリに以下の warning が表示されていることに気が付きました。

A newer version of androidx.compose.material:material than 1.1.0-beta01 is available: 1.4.0-alpha01

「androidx.compose」系のライブラリのバージョンの指定には、プロジェクト側の build.gradle に定義された 「compose_version」という変数を使用していますので、プロジェクト側の build.gradle を確認し、warning の通りに「compose_version」のバージョンを上げました。

buildscript {
    ext {
        -  compose_version = '1.1.0-beta01'
        + compose_version = '1.4.0-alpha01'
    }

上記がエラー前に行っていた作業です。

その後アプリを実行したところ、「Could not find androidx.compose.compiler:compiler:1.4.0-alpha01」などのエラーが発生しました。

原因

プロジェクト側の build.gradle に定義された 「compose_version」という変数は、「androidx.compose」系のライブラリのバージョンの指定だけではなくアプリケーション側の以下の箇所でも使用されています。

   composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }

コンパイラのバージョン指定にも使用されているのです。

ですが、以下の記事にあるように、「Compose Compiler」と「Compose Libraries」は異なるスケジュールでリリースされているようです。

Why can't I upgrade compose_version from compose_version = '1.1.0-beta01' to 1.2.1?
I create an project based the wizard Empty Compose Activity of Android Studio Chipmunk | 2021.2.1 Patch 1. It can be com...

ということで、プロジェクト側の build.gradle に定義された 「compose_version」を 1.4.0-alpha01 に変更してしまうとコンパイルのバージョン指定も変わってしまい、結果「そんなバージョンのコンパイラは Could not find だよ」と言われてしまったようです。

対応

上記の記事に書いてあることを実施していきます。

まずはプロジェクト側の build.gradle を変更します。
「compose_compiler」という変数の追加と、kotlin のバージョンを変更しています。

buildscript {
    ext {
        compose_version = '1.1.0-beta01'
        + compose_compiler = '1.3.2'
    }
}
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.5.31' apply false
    + id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}

「compose_compiler」のバージョンやコンパイラに対応した Kotlin のバージョンは こちら を参考に指定しています。

次にアプリケーション側の build.gradle の以下の箇所を変更します。

   composeOptions {
        -  kotlinCompilerExtensionVersion compose_version
        + kotlinCompilerExtensionVersion compose_compiler
    }

上記で対応は終了です。

上作業後なら「compose_version」の値を最新のバージョンに変更してもエラーは出てこないはずです。

メモ

Compose ライブラリのバージョンについては こちら に記載されてます。

タイトルとURLをコピーしました