Flutter報錯:Android toolchain “but Android SDK not found at this location”

Flutter報錯:Android toolchain “but Android SDK not found at this location”

當前環境

  • macOS 10.13.6
  • Android Studio 3.5
  • Android Studio 3.5
  • Flutter 1.9.1+hotfix.2
  • Dart 2.5.0
$ flutter --version
Flutter 1.9.1+hotfix.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 2d2a1ffec9 (3 weeks ago) • 2019-09-06 18:39:49 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0

報錯“Android SDK not found at this location”

使用 flutter doctor 命令可以診斷當前的 flutter 環境的一些配置和工具的情況。

在本機環境中執行,報錯 Android SDK 不再指定的路徑中。

$ flutter doctor        
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.13.6 17G65, locale zh-Hans-CN)
[✗] Android toolchain - develop for Android devices
    ✗ ANDROID_HOME = /usr/local/opt/android-sdk
      but Android SDK not found at this location.
[✓] Xcode - develop for iOS and macOS (Xcode 10.1)
[✓] Android Studio (version 3.5)
[!] IntelliJ IDEA Ultimate Edition (version 2019.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Community Edition (version 2019.1.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.35.1)
[✓] Connected device (1 available)

解決辦法

可以看到提示信息裏有ANDROID_HOME的路徑:

ANDROID_HOME = /usr/local/opt/android-sdk

打開 Android Studio 查看配置 【Preferences】:

查看SDK的路徑: 【Apperance & Behavior】 > 【System Settings】 > 【Android SDK】

可以看到當前的 SDK 路徑是:

/Users/wangtom/Library/Android/sdk

而 flutter doctor 診斷提示的路徑 /usr/local/opt/android-sdk ,明細不一致。

現在我們將環境變量 ANDROID_HOME 的路徑改成和 Android Studio 的SDK路徑一樣:

$ export ANDROID_HOME="/Users/wangtom/Library/Android/sdk"

注意: 該配置修改僅在本次開機生效,如果重啓了電腦需要再次執行。
如果想長期生效請在 ~/.bash_profile 文件中添加該環境變量。

查看一下配置變量:

$ echo $ANDROID_HOME
/Users/wangtom/Library/Android/sdk

再重新執行 flutter doctor 繼續診斷。這次不提示SDK路徑對了,提示 一些證書沒有同意。

$ flutter doctor 

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.13.6 17G65, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 10.1)
[✓] Android Studio (version 3.5)
[!] IntelliJ IDEA Ultimate Edition (version 2019.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Community Edition (version 2019.1.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.35.1)
[✓] Connected device (1 available)

安裝提示執行 flutter doctor --android-licenses 命令,同意每個正式即可。

// 每個證書選擇 y 都同意即可
$ flutter doctor --android-licenses
... 
Accept? (y/N): y

2/5: License android-sdk-preview-license:
Accept? (y/N): y

3/5: License google-gdk-license:
...

再次執行flutter doctor驗證

最後再次執行 flutter doctor 驗證一下,Android toolchain 也驗證正常了。

$ flutter doctor                   
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.13.6 17G65, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 10.1)
[✓] Android Studio (version 3.5)
[!] IntelliJ IDEA Ultimate Edition (version 2019.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Community Edition (version 2019.1.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.35.1)
[✓] Connected device (1 available)

! Doctor found issues in 2 categories.

還有提示 IntelliJ IDEA Ultimate EditionIntelliJ IDEA Community Edition 兩個軟件的警告,因爲也沒打算用這2個軟件來開發 Flutter, 忽略即可。

Reference

[EDN]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章