Appium ANDROID_HOME 环境变量问题

再见树林 · 2017年01月19日 · 最后由 arsenalley 回复于 2018年06月07日 · 4454 次阅读

版本:appium1.6.3

用 mvn test -Dtest=com.saucelabs.appium.AndroidTest 运行 sample-code-master/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidTest.java 测试,会报错:

Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path

可是 echo $ANDROID_HOME 却是有的,是/Users/myname/Library/Android/sdk。

有木有人知道什么原因啊,已经被这个问题折腾好几天了😂

共收到 20 条回复 时间 点赞

附上 stacktrace:

Running com.saucelabs.appium.AndroidTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.722 sec <<< FAILURE!
apiDemo(com.saucelabs.appium.AndroidTest) Time elapsed: 0.651 sec <<< ERROR!
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 253 milliseconds
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:51)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:144)
at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:47)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:114)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:132)
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:97)
at com.saucelabs.appium.AndroidTest.setUp(AndroidTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

附上 bash_profile 的内容:

JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME
export ANDROID_HOME=/Users/myname/Library/Android/sdk
export PATH=$M2:$PY3:$PATH:$MYSQL:$JAVA_HOME:$ANDROID_HOME:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$JAVA_HOME/bin

这个是用户环境变量配置文件,如果修改后需要重新登录或者使用 source .bash_profile 使其生效
如果你的每个用户都需要使用这个 ANDROID_HOME 配置,将其加在系统环境配置文件/etc/profile,修改后同样需要使用 source /etc/profile 使其生效

我怀疑你用的可能不是自带的 Terminal。。是不是 zsh?另外可以用 appium-doctor 看看哪个环境变量不对。

#4 楼 @da_sheng 是装了 zsh,appium-doctor 检查都 ok

#3 楼 @kuan8848 已经 source 了,echo $ANDROID_HOME 都有的

在脚本里面使用 adb 的全路径

在脚本里面把 PATH ,ANDROID_HOME 都打印出来,看看内容是什么。
你现在情况是读取不到你当前用户的环境变量

#5 楼 @ciaowood zsh 和 bash 用的不是一个配置文件

换个变量名试试 ,例如把 'android_home' 改为 'android'

adb 的环境变量没有配好吧

echo % 对应变量% 看看路径是不是对。。。

ANDROID_HOME 必须设置为系统变量,不能是用户变量。我也遇到过这个问题,就是这样解决的

你如果用 zsh,那你的配置文件就不是 bash_profile,跟我之前遇到的问题,一毛一样。。。你上网查一下 zsh 配置文件, 改那个就好了。我现在的电脑不用 zsh,找不出那个配置的文件名字了。。

17楼 已删除

#16 楼 @alex_sheng 没错,就是这个,多谢大家!

Daa盛 回复

确实是这样的,解决了困扰我 1 天的问题,

再见树林 回复

您好,在吗?

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册