当 运行 "ns run" & "ns doctor android" 时得到 "ANDROID_HOME environment variable is not set"

Getting "ANDROID_HOME environment variable is not set" when running "ns run" & "ns doctor android"

我正在使用 MacOS。我之前已经将 Android Studio 和 Xcode 用于我的一个 react-native 项目。我现在正在学习 SvelteNative。当我开始关注文档 here and here,并进入我需要 运行 ns doctor android 的步骤时,我收到此错误:

zeddrix@Zeddrixs-MacBook-Pro todoapp % ns doctor android
✔ Getting environment information 

TIP: To avoid setting up the necessary environment variables, you can use the Homebrew package manager to install the Android SDK and its dependencies.

There seem to be issues with your configuration.
✔ Getting NativeScript components versions information...
✔ Component nativescript has 8.1.2 version and is up to date.
⚠ Update available for component @nativescript/core. Your current version is 7.0.0 and the latest available version is 8.1.3.
⚠ Update available for component @nativescript/android. Your current version is 7.0.0 and the latest available version is 8.1.1.
✔ Javac is installed and is configured properly.
✔ The Java Development Kit (JDK) is installed and is configured properly.
✔ Xcode is installed and is configured properly.
✔ xcodeproj is installed and is configured properly.
✔ CocoaPods are installed.
✔ CocoaPods update is not required.
✔ CocoaPods are configured properly.
✔ Your current CocoaPods version is newer than 1.0.0.
✔ Python installed and configured correctly.
✔ The Python 'six' package is found.
✔ Xcode version 13.0.0 satisfies minimum required version 10.
✖ The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android. 
 To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory. 

✖ WARNING: adb from the Android SDK is not installed or is not configured properly.  
 For Android-related operations, the NativeScript CLI will use a built-in version of adb.
To avoid possible issues with the native Android emulator, Genymotion or connected
Android devices, verify that you have installed the latest Android SDK and
its dependencies as described in http://developer.android.com/sdk/index.html#Requirements 

✖ WARNING: The Android SDK is not installed or is not configured properly. 
 You will not be able to run your apps in the native emulator. To be able to run apps
in the native Android emulator, verify that you have installed the latest Android SDK 
and its dependencies as described in http://developer.android.com/sdk/index.html#Requirements 

✖ Cannot find a compatible Android SDK for compilation. To be able to build for Android, install Android SDK 28 or later. 
 Run `$ sdkmanager` to manage your Android SDK versions. 

✖ You need to have the Android SDK Build-tools installed on your system. You can install any version in the following range: '>=23 <=30'. 
 Run `$ sdkmanager` from your command-line to install required `Android Build Tools`. In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly. 

Cannot read property 'toLowerCase' of null
zeddrix@Zeddrixs-MacBook-Pro todoapp % 

我在 运行ning ns run:

时也得到了这个
zeddrix@Zeddrixs-MacBook-Pro todoapp % ns run
✔ Javac is installed and is configured properly.
✔ The Java Development Kit (JDK) is installed and is configured properly.
✖ The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android. 
 To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory. 

✖ WARNING: adb from the Android SDK is not installed or is not configured properly.  
 For Android-related operations, the NativeScript CLI will use a built-in version of adb.
To avoid possible issues with the native Android emulator, Genymotion or connected
Android devices, verify that you have installed the latest Android SDK and
its dependencies as described in http://developer.android.com/sdk/index.html#Requirements 

✖ WARNING: The Android SDK is not installed or is not configured properly. 
 You will not be able to run your apps in the native emulator. To be able to run apps
in the native Android emulator, verify that you have installed the latest Android SDK 
and its dependencies as described in http://developer.android.com/sdk/index.html#Requirements 

✖ Cannot find a compatible Android SDK for compilation. To be able to build for Android, install Android SDK 28 or later. 
 Run `$ sdkmanager` to manage your Android SDK versions. 

✖ You need to have the Android SDK Build-tools installed on your system. You can install any version in the following range: '>=23 <=30'. 
 Run `$ sdkmanager` from your command-line to install required `Android Build Tools`. In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly. 

Your environment is not configured properly and you will not be able to execute local builds.
Verify that your environment is configured according to the system requirements described at
https://docs.nativescript.org/environment-setup.html#macos-android.
zeddrix@Zeddrixs-MacBook-Pro todoapp % 

我尝试卸载 Android studio 然后重新安装它,因为我认为这可能是原因,但仍然没有。我没有对 Xcode 执行此操作,因为下载它需要很长时间,而且关于 ANDROID 的错误似乎与 Xcode.[=24= 没有联系]

然后我搜索解决有关 ANDROID_HOME 环境变量丢失或放错位置的问题。 This article 帮助了我。

所以我运行:

cd ~/

touch .bash_profile

open -e .bash_profile

然后把这些放在.bash_profile:

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/platform-tools

但仍然一无所获...我也不知道如何解决下面的下一个警告。帮助。

经过几天的等待和研究,我阅读了 official Android Studio docs for setting env vars。并看到:

(ANDROID_SDK_ROOT) Sets the path to the SDK installation directory. Once set, the value does not typically change, and can be shared by multiple users on the same machine. ANDROID_HOME, which also points to the SDK installation directory, is deprecated. If you continue to use it, the following rules apply: If ANDROID_HOME is defined and contains a valid SDK installation, its value is used instead of the value in ANDROID_SDK_ROOT. If ANDROID_HOME is not defined, the value in ANDROID_SDK_ROOT is used. If ANDROID_HOME is defined but does not exist or does not contain a valid SDK installation, the value in ANDROID_SDK_ROOT is used instead.

我知道我可以为 ANDROID_HOME 做到这一点。但是由于文档在这里说它已被弃用,所以我决定改为在 ANDROID_SDK_ROOT 上执行此操作。我打开了我的终端机 运行 这个:

set ANDROID_SDK_ROOT=Users\zeddrix\Library\Android\sdk\

在我执行此操作后,CLI 似乎正在等待另一个命令,所以我随后执行了此操作:

export ANDROID_SDK_ROOT=/Users/zeddrix/Library/Android/sdk

如果这不起作用,试试这个:

set ANDROID_HOME=Users\zeddrix\Library\Android\sdk\

然后:

export ANDROID_HOME=/Users/zeddrix/Library/Android/sdk

有关此 ANDROID_HOME 的错误已消失。现在我只剩下一个错误需要解决:

You need to have the Android SDK Build-tools installed on your system. You can install any version in the following range: '>=23 <=30'. 
 Run `$ sdkmanager` from your command-line to install required `Android Build Tools`. In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly.

中的第一条评论帮助我完成了。我去了 Android Studio 的 SDK Manager > SDK Tools,然后正如@Kevin McCarpenter 所说:

"Uncheck (check actually) 'Show Package Details' and then you can select a lower version. If you're not building for android 12 (and you probably aren't yet), you can uncheck '31.0.0' and check '30.0.3'. After that, go into the build.gradle in your app and change buildToolsVersion '31.0.0' to buildToolsVersion '30.0.3'"

然后我 运行 ns 运行 android (正如我 运行 ns 运行 --help 时所说),它终于成功了!