1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Flutter]Android Studioをアップデートしたら'Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.'と言われてエミュレーターでアプリが起動しない不具合の対処法(解決)

Posted at

原因(結論)

Android Studioをアップデートしたタイミングで、FlutterがAndroid Studio配下のJavaを認識するためのPATHが変わった。

エラー内容

表題の通り、Android Studioをアップデート したら以下のようなエラーが発生して、エミュレーターでアプリが起動しない不具合が発生した。

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     Your current JDK is located in  /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 10s

┌─ Flutter Fix ─────────────────────────────────────────────────────────────────┐
│ [!] You need Java 11 or higher to build your app with this version of Gradle. │
│                                                                               │
│ To get Java 11, update to the latest version of Android Studio on             │
│ https://developer.android.com/studio/install.                                 │
│                                                                               │
│ To check the Java version used by Flutter, run `flutter doctor -v`.           │
└───────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
翻訳:Android Gradleプラグインを実行するには、Java 11が必要です。現在、Java 1.8を使用しています。

と言われており、いくつかの対処法が書かれている。

調査

この辺りの記事が該当し、記事に従って対応していたが解消されず。

どちらの記事も、そもそもJava11が入っているのかを確認しており、/Applications/Android\ Studio.app/Contents/jre/Contents/Home/bin/java -version

を実行して確認している。

しかし、私の環境では/Applications/Android\ Studio.app/Contents/配下のjreが存在していない。(怪しい)

その代わりに/Applications/Android\ Studio.app/Contents/配下のjbrというディレクトリを辿っていった先のbinからjave versionを確認することができる。

jbrの先のjave versionでは

java version "1.8.0_361"
Java(TM) SE Runtime Environment (build 1.8.0_361-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)

このように出力され、この時点でFlutterがAndroid Studio配下のJavaを認識するためのPATHが間違っていると仮説を立てた。

仮説検証

/Applications/Android Studio.app/Contents/jre

にアクセスできるように以下の記事を参考にした。

cd /Applications/Android\ Studio.app/Contents
sudo ln -nfs jbr jre

を実行すると

ln: jre: Operation not permitted

参考記事と同じように出力された。

参考記事ではiTermにフルディスクアクセスの権限が無いと書かれているが、
私が利用しているターミナルはMac標準のターミナルなので設定からターミナルのフルディスクアクセスを許可した。

改めてもう一度実行してみる。

sudo ln -nfs jbr jre

ln: jre: Operation not permittedは表示されないので、jreにアクセスできるか確認。
image.png
無事アクセスできた!!

flutter doctor -v

を実行してみる

[✓] Flutter (Channel stable, 3.7.3, on macOS 13.1 22C65 darwin-arm64, locale ja-JP)
    • Flutter version 3.7.3 on channel stable at /Users/sasaki.ken/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 9944297138 (11 days ago), 2023-02-08 15:46:04 -0800
    • Engine revision 248290d6d5
    • Dart version 2.19.2
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/sasaki.ken/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode_official.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.1 22C65 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 110.0.5481.100

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)と記載されており問題なさそう。

これでエミュレーターでアプリを起動してデバッグできるようになりました〜

おわりに

今回の原因はおそらくAndroid Studioをアップデートしたタイミングで、FlutterがAndroid Studio配下のJavaを認識するためのPATHが変わったという仮説であっていたと思います。

発生したAndroid Gradle plugin requires Java 11 to run. You are currently using Java 1.8.というエラーで検索すると該当する記事がたくさん出てきました。

記事を見ているとさまざまな対処法がある中で、今回私が紹介したパターンはあくまでひとつのパターンでしかないと思います。

もし、参考になりそうなら試していただければと思います。
最後までご覧いただきありがとうございます!

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?