【Android Studio】FAILURE: Build failed with an exception.の解決法

【Android Studio】FAILURE: Build failed with an exception.の解決法

この記事からわかること

  • Android Studio/Kotlin発生したビルドエラー解決する方法
  • FAILURE: Build failed with an exception./Google Analytics解析するには

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

環境

FAILURE: Build failed with an exception.

Android Studioでターミナルから./gradlew cleanコマンドを実行しようとした際に以下のFAILURE: Build failed with an exception.というエラーが発生しました。エラーログを確認してみるとgradleのバージョンがらみでエラーが起きているようですがAndroid StudioのGradleのバージョンを色々変更してみましたが、解決できませんでした。

$ ./gradlew clean


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'プロジェクト名'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:8.5.2.
     Required by:
         project :
         project : > com.android.application:com.android.application.gradle.plugin:8.5.2
      > No matching variant of com.android.tools.build:gradle:8.5.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but:
          - Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'javadocElements' declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'runtimeElements' declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
                  ・
                  ・
                  ・

各ファイルや環境の状況


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

  compileOptions {
      sourceCompatibility = JavaVersion.VERSION_1_8
      targetCompatibility = JavaVersion.VERSION_1_8
  }
  kotlinOptions {
      jvmTarget = "1.8"
  }
$ java -version
java -version
openjdk version "1.8.0_402"
OpenJDK Runtime Environment Corretto-8.402.08.1 (build 1.8.0_402-b08)

JDKのバージョン

「Settings」 > 「Build, Execution, Deployment」 > 「Build Tools」 > 「Gradle」>「Gradle JDK」から確認。設定はJava17になっていた

【Android Studio】JDK(Java Development Kit)の確認と変更方法!

JDKのバージョンが異なっていた?

以前記事にしましたがターミナルからのビルドではJDKが異なるため失敗することがあります。今回も多分それ絡みぽいのでJDKを統一するように変更してみます。

まず「build.gradle(Module:App)」内のJavaバージョンを17に変更しました。


  compileOptions {
      sourceCompatibility = JavaVersion.VERSION_17
      targetCompatibility = JavaVersion.VERSION_17
  }
  kotlinOptions {
      jvmTarget = JavaVersion.VERSION_17.toString()
  }

続いてjava -versionの結果が1.8.0_402だったのでこちらも17になるように変更していきます。以下コマンドでJava17をインストールします。

$ brew install openjdk@17

続いて以下コマンドでシンボリックを作成します。

$ sudo ln -sfn $(brew --prefix)/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

最後に.zshrcに以下を記述してパスを通せば完了です。


export PATH="/usr/local/opt/openjdk@17/bin:$PATH"
export JAVA_HOME=`/usr/libexec/java_home -v "17"`
export CPPFLAGS="-I/usr/local/opt/openjdk@17/include"

Javaのバージョンが変更されていれば成功です。この状態で./gradlew cleanを実行してみると正常に動作することを確認できました。

$ java -version
openjdk version "17.0.13" 2024-10-15
OpenJDK Runtime Environment Homebrew (build 17.0.13+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.13+0, mixed mode, sharing)

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

New Article

index