Gradle Sync Error : ANDROID_HOME 与 ANDROID_SDK_ROOT 指向不一致

当你在 Android Studio 中 gradle sync 并报一下错误时:

Several environment variables and/or system properties contain different paths to the SDK.
Please correct and use only one way to inject the SDK location.


ANDROID_HOME: xxxxxx
ANDROID_SDK_ROOT: /Users/username/Library/Android/sdk


It is recommended to use ANDROID_HOME as other methods are deprecated

由于 ANDROID_HOME 与 ANDROID_SDK_ROOT 设置了不同路径导致的,

按道理只要 ANDROID_SDK_ROOT 设置正确即可,因为按照官方文档定义,两者关系如下:

  • 如果定义了 ANDROID_HOME 并且其中包含有效的 SDK 安装,系统会使用 ANDROID_HOME 的值而不是 ANDROID_SDK_ROOT 中的值。
  • 如果未定义 ANDROID_HOME,系统会使用 ANDROID_SDK_ROOT 中的值。
  • 如果定义了 ANDROID_HOME,但其中不存在或不包含有效的 SDK 安装,系统会使用 ANDROID_SDK_ROOT 中的值。

但既然报错了,就按照错误提示修改吧, 修改 ANDROID_HOME 的路径,以 MacOS 为例,在 bash 文件中添加 $ANDROID_HOME

vi ~/.bash_profile 
export $ANDROID_HOME=/Users/username/Library/Android/sdk

使之生效并验证

source ~/.bash_profile
echo $ANDROID_HOME

重启你报错的 Android Studio 然后重新 sync 工程,问题解决。

猜你喜欢

转载自blog.csdn.net/vitaviva/article/details/126475772