154

edit: (aug-2016)

That question is from November 2013 (while Android Studio was still in Developer Preview mode),

Currently (AS v2.2, Aug-2016) during instalation AS asks to choose the SDK folder (or install on their default) and it automatically applies to which ever project you're opening.

That means any possible workaround or fix is irrelevant as the issue is not reproducible anymore.

original question:

we have this project with several modules that is already configured and executes correctly on another developer PC using a wrapper. I cloned the complete git submodules into my machine.

Below it's a directly print of my command line:

$ ./gradlew

FAILURE: Build failed with an exception.

* Where:
Build file '/home/budius/project_name/ActionBar-PullToRefresh/library/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':ActionBar-PullToRefresh:library'.
> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 6.378 secs

$ echo $ANDROID_HOME
/home/budius/Applications/android-studio/sdk
$ 

so, as you can see the ANDROID_HOME is there. What else do they want? What's wrong here.

running on Ubuntu 13.04

edit:

I already created a local.properties file with sdk.dir=<path> on the project root and it works. But that makes the code harder to port across systems n build server, so the question is still open.

Anyone knows why the ANDROID_HOME is not working and what to do to make it work?

Budius
  • 39,391
  • 16
  • 102
  • 144
  • 1
    Try to specify the path of your SDK in a local.properties file, under the root directory of your project. The file should have this: sdk.dir= – Gabriele Mariotti Nov 05 '13 at 23:28
  • 1
    Hi @GabrieleMariotti thanks for the suggestion. But I already did this and it works. But that makes the code harder to port across systems n build server. I would like to know if someone knows why the ANDROID_HOME is not working and what to do to make it work? I added this bit of info to the question. – Budius Nov 06 '13 at 09:25
  • I suggest you to post in this G+ community. https://plus.google.com/communities/114791428968349268860 It is an official Google Community. Usually Google team answers quickly. – Gabriele Mariotti Nov 06 '13 at 10:05
  • yeah, I'll give them a try. Thanks! – Budius Nov 06 '13 at 14:12
  • 2
    @Budius I had a similar case with the error message. The solution was to add the `settings.gradle` file to the project folder. – JJD Mar 16 '14 at 17:54
  • In your aug-2016 update you say "That means any possible workaround or fix is irrelevant as the issue is not reproducible anymore.". That's not true at all. It's true that running Android Studio first will paper over the problem because it creates local.properties, and after that gradlew works fine. But this is unhelpful to someone who checks out a project from VCS and would like to just run gradlew without ever bringing up the IDE. It shouldn't be necessary to even *install* an IDE. – Don Hatch Jan 16 '17 at 09:39
  • @DonHatch and the IDE is indeed not necessary. Your can manually create the local.properties and it will work. It's just that for the 99% it's done automatically by the IDE. – Budius Jan 16 '17 at 10:21

31 Answers31

93

In /my_current_project/, I created a file called local.properties and put inside:

sdk.dir=/my_current_path_to/sdk

In the console I need to do:

set ANDROID_HOME=/my_current_path_to/sdk
starball
  • 20,030
  • 7
  • 43
  • 238
u53r
  • 994
  • 7
  • 6
  • 10
    It don't fix the issue because the local.properties files should be "Local" and not under versioning. (Same problem as Budius : I want this local.properties file in my laptop to create build during development, But I don't want this file in my build machine because the sdks path is not the same !) – Tobliug Feb 03 '15 at 19:13
  • 7
    At least on windows it worked after backslashing string sdk.dir=D:\\Soft\\adt-bundle-windows-x86_64-20140702\\sdk – Cheburek Jul 07 '15 at 21:12
  • @Tobulug: you can control which files under version control and not add or unselect the local files – sivi Mar 04 '16 at 08:53
  • Using export instead of set fix it. – AugustoL Jul 18 '16 at 17:18
  • 1
    _set_ was wrong, I use *export*. But in this way, if you export this Variable, you don't need the local.properties file in your Projekt-Folder. To keep this setting on each reboot, add the export-commad to your ~/.bashrc – suther Oct 07 '16 at 08:19
  • Unfortunately, **gradlew** uses `local.properties` sdk path instead of $ANDROID_HOME environment variable. – IgorGanapolsky Nov 15 '16 at 16:20
52

On OSX, IntelliJ won't pick up the environment variables you set in your .bash_profile or .bash_rc etc...

Try this, substituting the value of your own sdk location:

launchctl setenv ANDROID_HOME /usr/local/opt/android-sdk

Then restart IntelliJ and Bob's your uncle.

Here is a reference to the problem, stated more generally: https://emmanuelbernard.com/blog/2012/05/09/setting-global-variables-intellij/

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
Matthew Daumen
  • 855
  • 8
  • 8
  • 4
    FYI this solution is for osx. – Matthew Daumen Sep 16 '15 at 21:37
  • 4
    Yeah worked for me on elcapitan. `launchctl setenv ANDROID_HOME ~/Library/Android/sdk` – agmcleod Dec 22 '15 at 05:02
  • 2
    Or you can simply proxy your environment variables like `launchctl setenv ANDROID_HOME $ANDROID_HOME` and `launchctl setenv JAVA_HOME $JAVA_HOME` – Vagif Apr 16 '18 at 23:19
  • 1
    Just FYI, for OSX this fix also helps for command line build via gradle, when the same error occurs while you are using shell other than bash (zsh for example). Just run this command and restart your terminal window. – interrupt Dec 04 '18 at 00:14
31

In my case settings.gradle was missing.

Save the file and put it at the top level folder in your project, even you can copy from another project too.

Screenshot reference:

enter image description here

Hope this would save your time.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • Or someone didn't add your submodule to the settings.gradle file. I had a junior on our team that didn't do this after adding a submodule. Not sure how their IntellIJ is configured but this wasted a non-trivial amount of my time. – Stevers Feb 17 '20 at 23:56
25

This works for me:

$ export ANDROID_HOME=/path_to_sdk/
$ ./gradlew
giopromolla
  • 519
  • 5
  • 14
  • 1
    you also can add this the above "export"-line to your ~/.bashrc or ~/.bash_profile to make it permanent even after reboot. – suther Jun 26 '18 at 06:46
19

The Android Gradle plugin is still in beta and this may simply be a bug. For me, setting ANDROID_HOME works, but we may be on different versions (please try again with the most recent version and let me know if it works or not).

It's also worth setting the environment variable ANDROID_SDK as well as ANDROID_HOME.

I have seen issues with this on some machines, so we do create local.properties in those cases - I have also noticed that the latest version of Android Studio will create this file for you and fill in the sdk.dir property.

Note that you shouldn't check local.properties into version control, we have added it to our gitignore so that it doesn't interfere with porting the code across systems which you rightfully identified as a potential problem.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51
  • 2
    Defining ANDROID_SDK didn't do anything for me. – Heath Borders Feb 03 '14 at 16:53
  • Which version of Gradle and the Android plugin are you using, and do you have a local.properties file? – ZoFreX Feb 03 '14 at 16:54
  • 2
    adding `sdk.dir=/path/to/android/sdk` worked for me. I'm running gradle as part of a project created with IntelliJ 13.0.2 – Heath Borders Feb 03 '14 at 16:55
  • Looks like this is an IntelliJ bug. http://youtrack.jetbrains.com/issue/IDEA-119361 – Heath Borders Feb 03 '14 at 19:50
  • There may also be a bug in IntelliJ or Android Studio, but the original question was about using Gradle from the command-line, which seems to have its own bug (may or may not be related to the issue you are seeing). Edit: Also, on my system IntelliJ does not inherit environment variables from the command-line, which might be your issue? – ZoFreX Mar 05 '14 at 16:38
  • create a file *local.properties* in the same folder as build.gradle and add inside *sdk.dir=/path/to/android/sdk* worked for me. – Raymond Chenon Oct 08 '14 at 21:51
  • Why is **ANDROID_SDK** variable needed? – IgorGanapolsky Nov 15 '16 at 16:21
17

For whatever reason, the gradle script is not picking up the value of ANDROID_HOME from the environment. Try specifying it on the command line explicitly

$ ANDROID_HOME=<sdk location> ./gradlew
Andrew Marshall
  • 1,469
  • 13
  • 14
14

I faced the same issue, though I had local.properties file in my main module, and ANDROID_HOME environment variable set at system level.

What fixed this problem was when I copied the local.properties file which was in my main project module to the root of the whole project (i.e the directory parent to your main module)

Try copying the local.properties file inside modules and the root directory. Should work.

joecizac
  • 1,077
  • 2
  • 13
  • 14
  • 1
    I thought **local.properties** file is auto-generated by the IDE. Why the need to manually copy or move this file to other directories? – IgorGanapolsky Nov 15 '16 at 16:22
13

I came across the same problem when opening a cloned git repository. The local.properties file is automatically added to the .gitignore file as it is specific to the build environment of each machine and is therefore not part of the repo.

The solution is to import the project instead of just opening it after you have cloned it from git, this forces android studio to create the local.properties file specific to your machine:

File >> Import Project >>

Bulwinkel
  • 2,111
  • 25
  • 23
12

MAC OSX:

  1. Open up Terminal and edit the file:

~/.bash_profile

to add:

export ANDROID_HOME=~/Library/Android/sdk export PATH=${PATH}:${ANDROID_HOME}/tools export PATH=${PATH}:${ANDROID_HOME}/platform-tools

  1. Run:

    source ~/.bash_profile

  2. Restart the Terminal and Android Studio

8

How to do it on MAC OSX:

1) Open up Terminal, and Edit: vi ~/.bash_profile If there is no file there, just add it.

2) Add (Change to YOUR USER NAME and add this):

#Java var home: JAVA_HOME="/usr/libexec/java_home" ANDROID_HOME="/Users/<YOUR USER NAME>/Library/Android/sdk"

3) Run source ~/.bash_profile.

4) Run echo $JAVA_HOME; echo $ANDROID_HOME;

5) If your output is:

/usr/libexec/java_home /Users/<YOUR USER NAME>/Library/Android/sdk

So you are good.

and RESTART android studio!

And, Make sure that you have java :)

java -version

And gradle :)

gradle --version

Adam Delarosa
  • 901
  • 1
  • 10
  • 20
  • in think you are missing: `export ANDROID_HOME` – To Kra Nov 07 '16 at 19:15
  • 1
    I set `ANDROID_HOME` and then execute `source ~/.bash_profile`, this is permeant (execute `source` is good if you don't want to log out and in or reboot). In your case, with `export ANDROID_HOME`, after a restart or login, the environment variable `ANDROID_HOME` in mac will be gone. – Adam Delarosa Nov 07 '16 at 22:52
7

Copy the local.properties to root folder and run again.

4

This worked for me (Ubuntu):

Add ANDROID_HOME=/path/to/android-sdk to /etc/environment.

Reboot.

everyman
  • 3,377
  • 1
  • 34
  • 33
3

in windows, I set ANDROID_HOME=E:\android\adt-bundle-windows-x86_64-20131030\sdk Then it works as expect.

When in Linux, you need to set sdk.dir.

The script uses two different variables.

richard
  • 1,845
  • 1
  • 20
  • 30
  • 1
    I added it, added in location.properties file but still facing same issue – Suneel Prakash May 16 '15 at 19:30
  • 1
    On Windows, make sure you escape the backslashes in the path. I had to enter the path like `sdk.dir=C:\\Users\\username\\AppData\\Local\\Android\\Sdk` – Xsasan Feb 10 '17 at 21:10
2

I have the same problem, seems the sample code can not find the android environment, instead to try to fix that I just remove the sample code from settings.gradle and then the installation goes fine.

after that just import the project in eclipse and that's all :)

2

In Linux, try to run studio.sh from a terminal and set the ANDROID_HOME in this terminal. This worked for me.

Naren
  • 2,706
  • 1
  • 21
  • 15
2

If you are using windows plantform, please try run Android Studio as Administrator

Megoc
  • 21
  • 1
1

Just delete the sdk.dir inside the local.preoperties file and set the ANDROID_HOME environment variable . It worked for me.

PraveenMax
  • 717
  • 9
  • 18
1

export ANDROID_HOME=/xxx/xxx/ in shell, then use it by System.env.ANDROID_HOME in gradle file.

PS: don't forget the 'export' keywords to make the ANDROID_HOME global.

Jindowin
  • 31
  • 2
  • The `export` in this answer is important to note. I had `ANDROID_HOME` defined, and was even adding that to `PATH`, but if you don't `export ANDROID_HOME`, gradle won't be able to find it. – lase Nov 02 '17 at 16:13
1

Your local.properties file might be missing. If so add a file named 'local.properties' inside /local.properties and provide the sdk location as following.

sdk.dir=C:\Users\\AppData\Local\Android\Sdk

kinath_ru
  • 4,488
  • 3
  • 21
  • 26
1

I have set the ANDROID_HOME = [PATH_OF_MY_ANDROID_SDK] to my environment variable. That solution works for me.

Pravin Abhale
  • 395
  • 2
  • 4
  • 15
0

I have just solved the exact same issue by adding the ANDROID_HOME as a system wide variable. In Ubuntu it should be in /etc/profile or in a shell script file in /etc/profile.d/

Then logout and login again, now Gradle should recognize the ANDROID_HOME variable.

Joe3112
  • 968
  • 8
  • 10
0

I came across a similar problem. Somehow, I did not have a build folder in my project. By copying this folder from another project to my project I was having an issue with, this fixed this problem.

Horatio
  • 1,695
  • 1
  • 18
  • 27
0

Installing Build-Tools 23.0.1 instead of 23.0.2 fixed this issue for me.

TheBetterJORT
  • 808
  • 9
  • 22
0

solutions:

1 add "sdk.dir=path_of_sdk"

2 execute gradlew with evn variable like following:

$ANDROID_HOME=path_of_sdk ./gradlw

RoFF
  • 529
  • 5
  • 27
0

You said that versioning local.properties creates problems for you. I've hacked together a script which uses android command line tool to refresh the local.properties file across the machines that are involved in the production. The android update project command, besides the local.properties produces a lot of unwanted trash (at least for me) which is the reason for all those rm commands at the end of the script.

#!/bin/bash
scname="$0"
echo "${scname}: updating local properties..."
ln -fs src/main/AndroidManifest.xml
android update project -t 24 -p "$(pwd)"
echo "${scname}: ...done"
echo "${scname}: removing android update project junk ..."
rm -v project.properties
rm -v build.xml
rm -v proguard-project.txt
rm -v AndroidManifest.xml
echo "${scname}: ...done"

This script is the first thing we run on any new machine where we code. It has to be run in the root project directory. Of course, android studio may have a GUI way of dealing with this, but I wouldn't know as I use a different editor. I also can't claim that the solution is general, but it "Works For Me" (tm).

Mali Remorker
  • 1,206
  • 11
  • 20
0

I have faced with the same issue on Ubuntu(both local.properties and ANDROID_HOME was added), but build fail persisted. So workaround is to add following lines

export ANDROID_HOME=/home/<user>/Android/Sdk export PATH=$PATH:/home/<user>/Android/Sdk/tools

directly to the studio.sh script (inside /usr/local/android-studio/bin)

Maybe it will be helpful.

Lukas
  • 1,216
  • 12
  • 25
0

i encountered the same error but in my case i was cloning a project, the cloned project was built with Android API 22 which i did not install at the time(i had API 24 and 25 installed)........so i had to download the sdk tools for API 22

0

For Windows:

  1. Add ANDROID_HOME to the Environment Variables: ANDROID_HOME = C:/Users/YOUR_USERNAME/AppData/Local/Android/sdk
  2. Add %ANDROID_HOME%\platform-tools to the PATH.
J. Jerez
  • 704
  • 8
  • 6
0

On my system (Ubuntu 20.04 after two version upgrades from 19.04) the symptoms were as if gradle (4.4.1 installed from APT repos) ignored ANDROID_HOME environment variable, while picking up the sdk.dir value from local.properties if I created one.

The reason appeared to have been that java command referred to openjdk version "11.0.7". After I installed openjdk-8-jdk package and did update-alternatives --config java to make the default java be version 8 ("1.8.0_252"), gradle started working as expected.

Ruslan
  • 18,162
  • 8
  • 67
  • 136
0

My issue was that directory did not exist. The env vars were set correctly, but the underlying directory did not exist. After opening AS the first time and having it create the directory, everything worked.

echo $ANDROID_HOME
/Users/x/Library/Android/sdk

$ echo $ANDROID_SDK_ROOT
/Users/x/Library/Android/sdk

$ cd $ANDROID_HOME
-bash: cd: /Users/x/Library/Android/sdk: No such file or directory
tir38
  • 9,810
  • 10
  • 64
  • 107
-3

That question is from November 2013 (while Android Studio was still in Developer Preview mode),

Currently (AS v2.2, Aug-2016) during instalation AS asks to choose the SDK folder (or install on their default) and it automatically applies to which ever project you're opening.

That means any possible workaround or fix is irrelevant as the issue is not reproducible anymore.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • That's incorrect. I am currently trying to solve the same issue, still no luck, tried many possible solutions posted here. – AndroidDev Jan 31 '17 at 16:09