react-native 'sdk.dir' - React Native android build failed.SDK location not found




please does (18)

I have error when i start running android

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

Answers

Copy your system's other android project local.properties and paste in android folder of React-native project it will work.

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

There are two gradle files, change the one inside your C:\myapp\android\build.gradle and not in the parent directory myapp.

Replace 23.0.1 with your version of Android Build Tool installed in my case 25.0.0

Run react-native run-android to see your app in emulator.


On Ubuntu, where you have to get SDK separately from the Android Studio strange the path will work with Android Studio but with React Native, you'll need to create a file name local.properties.

For example inn ~/AwesomeProject/android and insert this line:
sdk.dir=/path/to/sdk/in/your/computer


The best solution I can find is as follows:

  1. Download Android Studio and SDK of your choice (Even if you think you don't need it trust me that you would need it to release the apk file and some manual changes to the android code).
  2. File > New > Import , point to the location where your react native android project is.
  3. If it ask you to download any specific SDK then please download the same. It can ask you to update gradle etc... Please keep on updating where required.
  4. If you have an existing Android SDK and you know the version then all you have to do is match that version under build.gradle of your android project.

This is how the gradle file will look like:

If everything has gone well with your machine setup and you can compile the project using the Android Studio then nothing will stop you to build your app through react-native cli build android command.

With this approach, not only you will solve the problem of SDK, you will also resolve many issues related with your machine setup for Android development. The import will automatically find SDK location and create local.properties. Hence you don't need to worry about manual interventions.


If you are on windows escape (add backlashes to) the backslashes and the colon in the android/local.properties file. If its not there then create it

sdk.dir = C\:\\Android\\sdk

This worked for me .

I am taking Stephen Grider's React Native on Udemy and one of the students posted this in Lecture 50. Pasted verbatim in the command line (w/o '$' of course).

$ export "ANDROID_HOME=/usr/local/opt/android-sdk" >~/.bash_profile

Copy your system's other android project local.properties and paste in android folder of React-native project it will work.


Fixing SDK Path Errors on Linux Distributions


Your project is not finding the SDK set in the Project Settings via the path provided, so the project needs to be pointing in the right direction, and you have a couple options to accomplish this, but let's start by finding the correct SDK folder first.


Finding your SDK


From Package Manager


If you are using a package-based JRE install like Java 8 JRE via a the custom PPA in a Debian-based distribution, or used OpenJDK, the directory will most likely be /usr/lib/jvm/java-8-oracle (or whatever version you have chosen). You can find this path set in the JAVA_HOME environment variable:

$ echo $JAVA_HOME
 /usr/lib/jvm/java-8-oracle

Manual Download


If you have installed via the Java JRE/SDK download, it will be wherever you placed the folder, e.g. /home/username/jdk-8u121_1.8.0 (or something similar).

Scripted installs may even add the SDK to /opt/java-8-oracle, but as of this writing, the Java Install instructions leave the folder placement in your hands.


Android Studio 2.2


Lastly if you are using Android Studio, the SDK is placed where you have set it to place the SDK files downloaded through the Studio's SDK Manager. By going to File > Settings > Appearance & Behavior > System Settings > Android SDK, you can find the SDK location setting at the top with the option to change, and the default folder being ~/Android/Sdk.

Setting the SDK Path

Now that we know where the SDK is, it is time to let react-native know.


Option 1: Set with local.properties


This method is very simple, and all that is needed is creating the file local.properties in the following folder, where Project_Folder is the base of your React-Native application:

Project_Folder/Android/App

In your local.properties, you want to set the variable sdk.dir to the path of your SDK folder:

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

While this is the easiest way to remedy the issue, it is not the best, especially if you work with multiple projects. For every project, you will have to create this file for every project, and if the folder changes plan on changing this file for each project's local.properties.

Once the file is saved, rerun the react-native run-android command.


Option 2: Settings Folders with Environment Variables


The other option is to set the SDK folders to the local environment variables that React-Native looks for by default. To do so, we will add the ANDROID_HOME path. As a bonus we can also add our android tools in the same place. We need to add this to rather the .bash_profile or .profile files in our home directory, which are loaded last, so we can make sure that nothing else is overriding the settings we make. Since these are in your home directory, there is no need to edit with escalated privileges.

**$ vim ~/.profile**  (use nano or pico if you prefer)

In the file add the following lines replacing the SDK folder with the one you found above. I will be using the Android Studio folder as an example:

export ANDROID_HOME="~/Android/Sdk"
export PATH=$PATH:~/Android/Sdk/tools:~/Android/Sdk/platform-tools

Save the file, then apply the profile settings:

$ source ~/.profile
$ echo $ANDROID_HOME
/home/dbrown/Android/Sdk

If the Sdk folder shows correctly, congratulations! Re-run your react-native command, and watch the app build process continue forward.


If you still have issues


If for some reason you receive the error "> failed to find target with hash string 'android-23' in: your/sdk/folder, you need to make sure that the project's build version and the version of the SDK installed match (React-Native defaults to 23.0.1). You can find the default version in Project_Folder/android/app/build.gradle, and then searching for buildToolsVersion. You can of course change the version from this file, or in Android Studio go to File > Project Structure to change the Project SDK. I would not suggest changing this version unless you absolutely know what you are doing, otherwise install the SDK version the project is looking for.


echo "sdk.dir = /Users/$(whoami)/Library/Android/sdk" > android/local.properties


Just open folder android in project by Android Studio. Android studio create necessary file(local.properties) and download SDK version for run android needed.


Check out in your local.properties file:

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

if the format is proper or not


Make sure you have the proper emulator and Android version installed. That solved the problem for me.


If you are using Ubuntu, just go to android directory of your react-native project and create a file called local.properties and add android sdk path to it as follow:

sdk.dir = /home/[YOUR_USERNAME]/Android/Sdk

  • Go to the android/ directory of your react-native project
  • Create a file called local.properties with this line:

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

    Where USERNAME is your OSX username


DO NOT UPDATE local.properties, it is definitely a bad practice.

This is how I fixed it. Add the following in ~/.bash_profile or ~/.bashrc.

export ANDROID_HOME=/usr/local/share/android-sdk
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/build-tools/23.0.1:$PATH

Note: I used brew cask to install Android SDK following these instructions.


For Windows Users

One simple solution is to set the SDK path to the Enviroment Variables list.

  1. Head over to the Enviromnent variables. (Your PC > Properties )
  2. Create New System variables
  3. Set the Variable name to ANDROID_HOME
  4. Set the Variable value to your Android Sdk Location
  5. Press OK (3 times for 3 dialog boxes)

Note:

You Android Sdk is generally located in

C:\Users\YourUserName\AppData\Local\Android\Sdk

Make sure to close your current terminal because your terminal holds all of your pre-existing Environment Variables.


You must write correct full path. Don't use '~/Library/Android/sdk'

vi ~/.bashrc

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

source ~/.bashrc

Guys try this in onCreate method of your AppController.java

  CookieHandler.setDefault(new CookieManager());

Hope it'll save time of developers. I have wasted four hours in debugging and searching appropriate solution.





android react-native