Error: Failed to find 'ANDROID_HOME' environment variable. Try setting setting it manually.

这两天在全新mac虚拟机里搭开发环境,web前端和IOS开发环境都很顺利,但android环境着实让人恼火。明明在AndroidStudio中都可以正常新建工程、正常撸码调试、正常运行到真机。但是!!!命令行中就是不行!!!


记录如下:


打开终端,进入cordova工程目录

cd Desktop/demo 
cordova build android

报错:

Error: Failed to find 'ANDROID_HOME' environment variable. Try setting setting it manually.

Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.


提示是android环境变量没配好导致的。可执行如下命令验证:

echo $ANDROID_HOME

你会发现输出为空。如果android环境变量ok,执行以上命令会输出android环境变量信息。


接下来开始配置android环境变量。


打开终端,默认处于系统当前登陆用户目录下,

Last login: Fri Oct 13 18:43:15 on ttys003

➜  ~ pwd

/Users/xbrother


在此目录下编辑.bash_profile文件,执行以下命令(.bash_profile文件存在则打开,不存在则新建):

vi .bash_profile

在此文件中配置android环境变量。我的配置如下:

export ANDROID_HOME=/Users/xbrother/Library/Android/android-sdk-macosx
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export GRADLE_HOME=/Applications/Android\ Studio.app/Contents/gradle/gradle-3.3
export PATH=$PATH:$GRADLE_HOME/bin

其中xbrother为系统当前登陆账号。

不习惯在终端中编辑文本的话也可如下操作:

新建.bash_profile文件

touch .bash_profile

用文本框打开 .bash_profile文件

open -e .bash_profile

即可在文本框中编辑文件配置环境变量。

配置好环境变量后,执行以下命令才会生效:

source .bash_profile

完成以上配置后,可再次执行以下命令验证android环境变量是否ok:

echo $ANDROID_HOME

将会输出:

➜  ~ echo $ANDROID_HOME   

/Users/xbrother/Library/Android/android-sdk-macosx


配置ok。


注意!!!

如此配置ok后,在当前终端窗口执行需要android环境变量的操作完全ok,但在其他终端窗口或新建终端窗口中依然会报错android环境变量未配置!重启终端甚至重启系统均不行,这就意味着后续操作都只能在当前终端窗口中进行。这是我不能接受的。网上查了好久,并没有找到可行的解决办法,貌似大家都如上配置就直接ok了。各位大神如果知道原因和解决办法,还请不吝赐教。


cmake_minimum_required(VERSION 3.16) project(untitled VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appuntitled main.cpp ) qt_add_qml_module(appuntitled URI untitled VERSION 1.0 QML_FILES Main.qml ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. set_target_properties(appuntitled PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appuntitled MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appuntitled PRIVATE Qt6::Quick ) include(GNUInstallDirs) install(TARGETS appuntitled BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) 有什么问题吗 为什么会报错:-1: error: FAILURE: Build failed with an exception. :-1: error: * Where: Build file '/Users/qingguo/Desktop/nounou/yes/Helloworld/untitled/build/Qt_6_5_8_Clang_arm64_v8a-Debug/android-build/build.gradle' line: 17 * What went wrong: A problem occurred evaluating root project 'android-build'. > Failed to apply plugin 'com.android.internal.application'. > Android Gradle plugin requires Java 17 to run. You are currently using Java 11. Your current JDK is located in /Library/Java/JavaVirtualMachines/jdk-11.jdk/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. :-1: error: BUILD FAILED in 1s :-1: error: Building the android package failed! -- For more information, run this command with --verbose.
最新发布
04-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值