Cannot run appium Could not find aapt Please set the ANDROID_HOME environment variable - even though it is set

I have these capabilities:

enter image description here

but when I try to call AndroidDriver

public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
	super(remoteAddress, substituteMobilePlatform(desiredCapabilities,
			ANDROID_PLATFORM));
	this.setElementConverter(new JsonToAndroidElementConverter(this));
}

I get this error:

Original error: packageAndLaunchActivityFromManifest failed. Original error: Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path. (WARNING: The server did not provide any stacktrace information)


org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: packageAndLaunchActivityFromManifest failed. Original error: Could not find aapt 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: 2.58 seconds
Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
System info: host: 'eladb-macbookpro2.roam.corp.google.com', ip: '100.105.45.66', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.5', java.version: '1.8.0_77'
Driver info: com.waze.mobileautomation.common.appiumdrivers.android.MyAndroidDriver
	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:423)
	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:595)
	at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
	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:242)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
	at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:37)
	at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:160)
	at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:168)
	at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:69)
	at com.waze.mobileautomation.common.appiumdrivers.android.MyAndroidDriver.<init>(MyAndroidDriver.java:38)
	at com.waze.mobileautomation.common.appiumdrivers.AppiumDriverFactory.createAndroidDriver(AppiumDriverFactory.java:269)
	at com.waze.mobileautomation.sdk.setup.PartnerSetupManager.createAndroidPartner(PartnerSetupManager.java:75)
	at com.waze.mobileautomation.sdk.setup.PartnerSetupManager.installAppAndStartIt(PartnerSetupManager.java:56)
	at com.waze.mobileautomation.partner.SanityIntegrationDebug.bOnboardAsDriverTest(SanityIntegrationDebug.java:56)
	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:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

what am i missing?

echo $ANDROID_HOME leads to my sdk

I have tried to add

androidCapabilities.setCapability("appWaitActivity", "com.example.eliran.myapplication.MainActivity");

My ANDROID_HOME is set to my sdk folder.

but it didn’t help. What can it be?

You don’t mention the IDE you are using, but I can see it’s intellij. Hopefully this will help:

Did you remember to export your ANDROID_HOME variable in the shell you used to spawn Appium?

I’m not sure what do you mean?

My ANDROID_HOME is set to my sdk folder.

like this:
❯ echo $ANDROID_HOME [10:54:50]
/Applications/adt/sdk

here is the folder content

I use intellij
but set the ANDROID_HOME in my .zshrc file

So you stackoverflow post is relevant?

In your test make a system call to ‘echo $ANDROID_HOME’. If you find it’s not set, you need to make intellij read your environment variables. In other words, intellij (like any ide) can ignore your environment variables. Follow the steps in that link to make it read them, or set them explicitly in the ide itself.

Okay, that looks right. The way environment variables work is that they are inherited by child processes when you spawn a new process (such as spawning the Appium server process).

Command line shells often have an additional feature for their variables: exporting. Shell variables that are not exported will NOT be passed onto child processes through inheritance. Shell variables that are exported will be inherited by child processes. Make sure that when you set your ANDROID_HOME variable in your .zshrc file that you’re also running the command export ANDROID_HOME after setting it.

For example, let us suppose you did not export the ANDROID_HOME variable. If you spawn your Appium server through your zsh command line, then Appium would not inherit the ANDROID_HOME variable, and thus Appium would not have an ANDROID_HOME variable to read, which leads to the aapt error.


What wreed is saying is that if you spawn Appium through IntelliJ (that is, the Appium process is a child of your Java test process), you need to make sure your tests’ launch configuration includes the ANDROID_HOME environment variable set. AFAIK, there is no need to worry about exporting or unexporting variables outside of command line shells.

i double check and reset my terminal and intellij

Now it works.
might have been a sync issue.
Sorry for the bother.

my env is ubuntu16.04, Appium desktop, configuration is {
“platformName”: “Android”,
“platformVersion”: “8.0”,
“deviceName”: “xiaomi”,
“appPackage”: “com.ansen.webview”,
“appactivity”: “com.ansen.webview.MainActivity”,
“autoGrantPermissions”: “True”,
“automationName”: “uiautomator2”
}

I still met this error, but I set ANDROID_HOME to env. How can I set ANDROID_HOME into my configuration?

Don’t forget to set the SDK path here as shown in the screenshot