ChatGPT解决这个技术问题 Extra ChatGPT

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable

I recently tried to import sample Android games I downloaded from Google's developer website. After importing them into Android Studio, I'm getting the following error:

Error: SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

What is this? I want to run the sample programs from Android Studio.

to the future visitors, please check this answer stackoverflow.com/a/56089502/7854303
this was happening with me in the first ever hello world, even the env variable was set correctly, what I did is, open the android folder in android studio and hit the run button(green play icon) and it worked, make sure your metro is running while you are doing this, i think it was happening because i didnt opened the android studio after installation when I opened the android folder in andriod studio I noticed it updated few things and now even npm run android is working fine

M
Marcel Hofgesang

Please follow below steps it work's for me:

Go to your react-native Project then go to android directory Create a file with following name:

local.properties

Open the file and paste your Android SDK path like below:

For windows users:

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

Replace UserName with your pc user name . Also make sure the folder is sdk or Sdk. In my case my computer user name is Zahid so the path look like:

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

For Mac users:

sdk.dir = /Users/USERNAME/Library/Android/sdk

Where USERNAME is your OSX username.

For Linux (Ubuntu) users:

sdk.dir = /home/USERNAME/Android/Sdk

Where USERNAME is your linux username(Linux paths are case-sensitive: make sure the case of S in Sdk matches)

In case if this doesn't work, add ANDROID_HOME variable in "Environment Variables" as C:\Users\USER\AppData\Local\Android\Sdk

https://i.stack.imgur.com/LDiPS.png


i am working with open cv what should i do to add NDK?
It helped. Actually this solution is hinted in the error wording itself. I use IntelliJ on Mac and in project structure I have SDK correct path. Why still do I need that in local.properties then?
if you're using eas build (expo build has been deprecated), this is no longer working, and you have to define the env variable in eas.json, also the path to the android SDK may be different (I don't have it in my home directory)
O
OneCricketeer

The project might be missing a settings.gradle file. Make sure that file exists from the project you are importing. If not add the settings.gradle file with the following :

include ':app'

Save the file and put it at the top level folder in your project.


How can I tell if my project is missing settings.gradle? Where is that file usually stored? Where do I type include ':app'? What exactly does include ':app' do/generate? Can you please add more detail to your answer?
@TotZam its stored in the root of your project
Found it. Thanks. For future reference, where would I run include ':app'? I'm quite new to Android and trying to find my way around.
You don't run include :app, if you are using Android Studio, that lets Android Studio add your project module so you can run it without further configuration. After you add the line in settings.gradle you will notice the Play button becomes active after gradle finishes building and downloading packages
Put your project folder name instead of 'app' if it's different. It works.
T
Tot Zam

I know this answer is late, but I hope it helps others.

The answer is simple. You have to just copy your local.properties file to the folder where project is stored and it will work like charm. But remember, it must be placed in the root folder where the project is stored.


But I have to copy it everytime? This seems like a hack than the correct approach
Its is the most easy solution for this problem as you dont have to change anything in project. There are also other solution in which you have to do change in code , but I think for such issue one cannot afford to waste much time.
It works, but Shouldn't Intelij generate this automatically ? tks !
This does not work for me. The file is present, but I still have the error
just open the android folder by android studio, when android studio opens the 'android' folder, local.properties will be created automatically.
C
CuriousSuperhero

Please follow bellow points it work's for me:

Go to your Project -> Android Create a file local.properties Open the file Paste your Android SDK path depending on the operating system: 4.a Windows sdk.dir = C:/Users/USERNAME/AppData/Local/Android/sdk 4.b Linux or MacOS sdk.dir = /home/USERNAME/Android/sdk Replace USERNAME with your user name


It is too late to comment, but i want to know how to do that in MacOS?
in MacOS you need to do same procedure just you need to find your Android SDK file path and paste it local.properties file...that's it
I just went to File > Project Structure and it said that SDK location is incorrect and fixed it for me on MacOS
in windows it should be with double slashes like this sdk.dir = C:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk
S
Stephen Kennedy

Check out in your local.properties file

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

properly write this format, and also check / slas using for path


I edited local.properties with this sdk.dir=C:\\Users\\sameer\\Android\\adt-bundle-windows-x86-20140702\\sdk, any error in this
yeah after C put \ ... Svae with thishope done. sdk.dir=C\:\\Users\\sameer\\Android\\adt-bundle-windows-x86-20140702\\sdk
This works nice with eclipse. Just create the file local.properties within the downloaded project and paste the sdk.dir . In my case it was : "sdk.dir=C\:\\adt-bundle-windows-x86_64-20140321\\sdk"
if anyone facing You have not accepted the license agreements... error after the above step see here stackoverflow.com/a/41078173/1778834
I have installed android on my windows 10 m/c but there is no such folder sdk.dir=C\:\\Users\\USERNAME\\AppData\\Local\\Android?
M
Michael Saffold

This problem is encountered when you try to import an Android Studio project from the ../app/build.gradle file.

Import the project by selecting the ../build.gradle file located in the root directory of your project.


M
Mjaustro

Here is a work around for the problem when you click "Run App" (green arrow) and get the following in the Edit Configuration dialog:

Error: Please select Android SDK

In Android Studio, do:

From the menu, choose File > Settings. In the settings dialog, go to Appearance & Behavior > System Settings > Android SDK. Look at the top for Android SDK Location, and click the Edit button Hit Next, Next, Finish to accept the defaults

This seems to save away the SDK location - even though nothing has changed - into some internal location. I inspected the .idea and .gradle folders but didn't see what Studio did to change a config file - but now I can run the app.

And to summarize the previous fixes - these are normally OK for a repo without build problems:

local.properties file is copied into the root folder by Studio. The path in the local.properties file has the correct path to the android SDK - in my case it is sdk.dir=C:\\android\\sdk (note that this path has a different format - Studio should write this file for you based on the Text Entry field in the Android SDK Settings dialog) settings.gradle file is present in the repo - and references the application folder (typically :app)


This is the only thing that helped me. Steps #3 and #4 are very important
This worked for me using IntelliJ with Android support installed.
This is the answer bro. Big Up maaan!
W
William Hu

I came across the same issue but a little bit different error message is

SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at "xxx"

MAC & ReactNative

Add local.properties

Find your Android SDK location /Users/yourMacUserName/Library/Android/sdk Create local.properties under rootProject/android/local.properties. Add sdk path into it sdk.dir = /Users/yourMacUserName/Library/Android/sdk

This normally works, but if you are working in a team with other team members, then yourMacUserName is different.

OR

Set ANDROID_SDK_ROOT variable

Edit your ~/.zshrc or ~/.bashrc or ... Add SDK path: export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk Open a new terminal tab or source ~/.zshrc echo $ANDROID_SDK_ROOT to test the print correct SDK path.

Alternatively, you also can add your path export PATH=${PATH}:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools to use some useful commands.


EAS builds: use eas.json under build => production (or other mode) make sure you have { "env": { "ANDROID_SDK_ROOT": "/your/path/to/androidSDK" }
P
Penny Liu

Go to your React-native Project -> Android Create a file local.properties Open the file paste your Android SDK path like below in Windows sdk.dir = C:\\Users\\USERNAME\\AppData\\Local\\Android\\sdk in macOS sdk.dir = /Users/USERNAME/Library/Android/sdk in linux sdk.dir = /home/USERNAME/Android/Sdk Replace USERNAME with your user name Now, Run the react-native run-android in your terminal

or

Sometimes project might be missing a settings.gradle file. Make sure that file exists from the project you are importing. If not add the settings.gradle file with the following :

include ':app'

Save the file and put it at the top level folder in your project.


g
gianlucaparadise

If you have this problem when you pull a react-native project, you just need to open the android project with Android Studio. Everything you need will be automatically created.

Open Android Studio

File -> Open

Choose the android folder under your react-native project folder

Wait for AndroidStudio to complete setup

You can now close Android Studio

OR

If you have installed the AndroidStudio command line launcher:

Run this in your react-native root folder

studio android/

Wait for AndroidStudio to complete setup

You can now close Android Studio


this answer is so underrated. I've come here twice now and it solved both the times. thanks a lot.
M
MHSFisher

In Linux:

If you have already downloaded the android SDK but its not being found.

The problem might be that the file local.properties needs to be inside the same directory as gradle stuff for gradle to find it when building and running adb.

For my react-native project using gradle I needed to put the local.properties file to Myprojectname/android/ folder.

As I had unzipped the SDK to Downloads so I just use that path in the file with a row like this:

sdk.dir=/home/USER/Downloads/android-sdk-linux

m
mumair

I resolved this issue by creating ANDROID_HOME environment variable as follows in windows.

ANDROID_HOME=C:\Users\<user_name>\AppData\Local\Android\sdk

Restart Android Studio it should build project!


I already had an ANDROID_SDK which points to the same dir, so for me it was ANDROID_HOME=$ANDROID_SDK
N
Nate

If you are trying to run Google android sample code, try to import the entire repository instead of an individual sample.

Here is instructions.html, included with the Google Calendar API sample code.

Import calendar-android-sample project Select "Import Project..." or File > Import Project... Select [someDirectory]/google-api-java-client-samples/build.gradle and click OK. Note: it will not work if you try to import [someDirectory]/google-api-java-client-samples/calendar-android-sample/build.gradle Select "Use local gradle distribution" with "Gradle home" of [someDirectory]/gradle-2.2.1 and click OK.


If anyone is trying to run one of the samples in the facebook-android-sdk, this tip helped me run it. Loading only one of the examples doesn't work.
A
Akshay Sharma

Just Remove .idea folder and import the project again. It's worked for me.


p
piet.t

There is not a single reason for this error.

settings.gradle may be missing or the content in it may be wrong. local.properties may be missing or the sdk path may be wrongly written.


what kind of content should be in the settings.gradle file?
@cabanni settings.gradle just includes the module in your application. By default it'll be 'app' module. Hope it helps/
Z
Zeeshan Mehdi

I had this error in flutter so i fixed it by going to android->app->build.gradle

and changing targetSdkVersion from 28 to 29 and compileSdkVersion to 29 from 28 and it worked for me


T
TAHA SULTAN TEMURI

In my case I was using linux and putting double quote around the path inside local.properties like

sdk.dir = "/root/Android/Sdk/"
export ANDROID_SDK_ROOT = "/root/Android/Sdk/"

So it should be

sdk.dir = /root/Android/Sdk/
export ANDROID_SDK_ROOT = /root/Android/Sdk/

and .bash_profile

https://i.stack.imgur.com/HJb25.png


I
Ilker Baltaci

create a local.properties file in your root directory of your project with the following content

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Oct 24 17:40:53 CEST 2017

sdk.dir=/Users/****/Library/Android/sdk

M
Muhaiminur Rahman

In my case, I had to close Project and open again. It worked fine. Like This

Close Project

https://i.stack.imgur.com/Brcpj.png

And again Open Project Again

https://i.stack.imgur.com/8EDWX.png


After adding sdk.dir path and also to ~/.bashrc, nothing worked. But then I closed and opened again and it worked. Thanks!
B
Bijendra Singh

Follow followings steps :

Create a file under 'android' folder with name 'local.properties' Add this line in file 'local.properties' as sdk.dir=/Users/bijendrasingh/Library/Android/sdk

Add here your android sdk path.


S
Stanley

put these two lines in your .bashrc file and run source ~/.bashrc

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

For linux I did this.(For the first line)

export ANDROID_HOME=/home/$USER/Android/Sdk

H
Hrishikesh Baidya

This solution actually works for me.. go to this pc -> properties -> advanced system settings -> environment variables -> then in system variable create new variable with name ANDROID_SDK_ROOT and value C:\Users{USERNAME(Replace it with your username}\AppData\Local\Android\Sdk

and make sure that if real android mobile using usb debugging is enabled. (very important)

then close cmd and restart it should work.


RESTART is the key.
A
Anubhav

There can be two different possibilities :

1). Either you SDK location is incorrect in local.properites file.

2). Or the file is missing, this can happen if you have cloned a project, so just create a local.properites file under Gradle Scripts foler, and then set up the sdk path.

Set up the correct sdk path like this :

sdk.dir=YOUR_PATH_TO_THE_SDK

For mac users the path should be:

/Users/USER_NAME/Library/Android/sdk

For windows users the path should be:

c:\Users\USER_NAME\AppData\Local\Android\Sdk 

A
Armen Hovhannisian

the best and the easiest way is to create new Android project move "app" folder from non working project to that newly made one and add the needed dependencies in the gradle of your new project and everything will work perfectly )


This corrected the issue on both the mac High Serria and Windows 10. Great answer and efficient.
M
M. Palsich

I got this error after freshly cloning a repository. I expected local.properties to be generated automatically, but it wasn't. I was able to generate it by re-importing the Gradle project.

File > Re-import Gradle Project

This helped. After "Re-import Gradle Project" was launched, the following 2 errors were printed to console which helped me to detect the problem: Invalid Gradle JDK configuration found, Project JDK is not specified. So everything started to work after I fixed the JDK path in Settings.
After JDK path was fixed I also had to restart the IDE with "File > Invalidate Caches / Restart ..." option.
t
tim4dev

If all else fails, copy the local.properties file to the root of the project directory. Simply.


t
touhid udoy

Anyone using Jenkins, might get it useful

You need to define a global variable name ANDROID_HOME with the value of the path to android sdk.

For mac, it is /Users/YOUR_USER_NAME/Library/Android/sdk


J
Junius L.

For Mac/Linux users

You need to add ANDROID_HOME to your path, add the following to your .bashrc || .zshrc || .profile file

# change $HOME to the path where you installed android Sdk
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools

Then run

$ source ~/.bashrc || .zshrc || .profile

Environment variables in bash_profile or bashrc?

Difference between .bashrc and .bash_profile


S
Sharp

I had a situation where I already had the local.properties file set up but I was still getting this error. Turns out, if your project has a submodule, you have to copy the local.properties into the submodule folder as well.


Working Solution :)
M
Max Droid

Check, 1. In Module settings, whether, SDK location is proper. 2. If Yes, check for local.properties file (Not the one placed inside app module, but the one placed outside the app module, at parent level). If not present add it with below lines inside it.

sdk.dir=/path/to/sdk/../Android/Sdk