81

I am completely new to Android development and just installed Android Studio. I'm doing a very basic HelloGL2 exercise, and I opened up the HelloGL2.iml file. I tried running it, but it said that the gradle project sync failed and that the NDK is not configured. In the simplest terms (to a complete beginner in Android), how do I fix this?

Karina H
  • 811
  • 1
  • 6
  • 3
  • I'm going to assume you've found this code lab: https://codelabs.developers.google.com/codelabs/android-studio-jni/#0 which step-by-step answers how to get 'Hello World' working with Android Studio and the NDK. – Morrison Chang Dec 03 '16 at 04:41
  • 3
    Can't post an answer, but for anyone who's running an unconventional setup and might need to provide paths manually: ANDROID_NDK_HOME = "YOUR_PATH_TO_LOCATE_NDK_BUNDLE", where the path should be something like /path/to/Sdk/ndk-bundle –  Apr 20 '18 at 06:52

14 Answers14

106

disclaimer: I work on Android Studio

With newer version (3.6+) of Android Gradle Plugin, please download with "NDK (Side by side)" option from the SDK manager and then specify ndkVersion in the build.gradle with the full version of NDK that is locally available from your machine.

For example, after you downloaded NDK 20.1.5948944 under "NDK (Side by side)" from the SDK manager, the downloaded NDK would be, by default, under ~/Android/Sdk/ndk/20.1.5948944. Then in your module level build.gradle, you need something like the following.

android {
    compileSdkVersion 29


    defaultConfig {
        // ...
    }

    buildTypes {
        // ...
    }

    externalNativeBuild {
        // ...
    }

    ndkVersion "20.1.5948944" // <<==== Here
}
tgeng
  • 1,768
  • 2
  • 15
  • 20
  • 1
    Thank you! I was still getting an error because the version the SDK manager downloaded was different than the one specified in the project I tried to open. Changing the ndkVersion in the build.gradle to match the one on disk fixed the error for me. – Izabela Orlowska Jan 22 '20 at 11:50
  • Fixed it for me too, so thank you! It's a pity the Android Studio does not handle this automatically, while it does for a lot(!) of other things. – fredvanl Feb 27 '20 at 12:33
  • Why does it ask me for NDK in a project where NDK is not needed or used? Will this force all who work on project to download and sync NDK version requested by AS? – Chapz Mar 02 '20 at 07:53
  • Hi @Chapz I am not able to reproduce the issue on a fresh machine (no NDK installed) with AS and AGP 3.6.1. Could you [file a bug](https://developer.android.com/studio/report-bugs#studio-bugs) with more detailed information? – tgeng Mar 02 '20 at 17:57
  • This should be the selected answer. – b005t3r Mar 03 '20 at 08:42
  • @protossor I could reproduce this with latest version of everything and the sample android c++ project in android studio. it worked after putting in the line with the `ndkVersion`. thanks! – Niklas Rosencrantz Mar 03 '20 at 10:55
  • Hi @grrigore, could you [file a bug](https://developer.android.com/studio/report-bugs#studio-bugs) with more detailed information? – tgeng Mar 12 '20 at 20:05
  • Hello @protossor, I managed to make it work, however, it was necessary to uninstall NDK, CMake and LLDB and reinstall them. Also some `Invalidate caches and restart` were involved. – grrigore Mar 13 '20 at 07:48
  • > Could not find method ndkVersion() for arguments [20.1.5948944] on object of type com.android.build.gradle.AppExtension. – Isaac Urbina Mar 18 '20 at 16:37
  • Hi @IsaacUrbina, what version of Android Gradle plugin are you using? – tgeng Mar 18 '20 at 17:35
  • @protossor I had to install the NDK (obsolete) from SDK Manager and then it worked without adding the ndkVersion in gradle – Isaac Urbina Mar 19 '20 at 20:39
  • Hi @IsaacUrbina, it sounds like you are using an old version of Android Gradle plugin (specified in your top level `build.gradle` file). `ndk-bundle` is deprecated and will be removed in future. – tgeng Mar 19 '20 at 21:00
  • PS It worked by specifying the path of ndk as mentioned, but in build.gradle I just commented out the ndk.version... – giorgio79 Mar 19 '21 at 12:15
  • I don't know if you still work on Android Studio, but just a quick feedback, it would be REALLY good if there was a good documentation about where and how the NDK should be setup, since there are so many differences between different AGP versions... – Bernardo do Amaral Teodosio Apr 28 '23 at 20:19
  • Hi I no longer work on this. Can you [file a bug](https://developer.android.com/studio/report-bugs#studio-bugs)? – tgeng Apr 28 '23 at 20:50
79

I double-checked that I already had the latest NDK installed (so that wasn't the solution for me). Next I did the following: Android Studio Menu -> File -> Project structure -> SDK Location. Here, no NDK was selected, so I selected the latest version. This solved the problem.

enter image description here

This fix only works locally for this particular machine and for this project by writing your choice to the local.properties file in your project's root.

UPDATE: There's also a way to set this globally via ANDROID_NDK_HOME environment variable. This is how I invoke building my project on a build server:

export ANDROID_SDK_ROOT=/home/jenkins/Android/Sdk/; export ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk/21.0.6113669; ./gradlew assembleRelease
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • 1
    This should be the right answer because SDK manager wasn't available when this error was happening. I had to select the default NDK path and it worked fine for me. This is Android Studio version 3.6.2. – Karthik Kannan May 26 '20 at 15:35
42

You can install these components using the SDK Manager:

  1. From an open project, select Tools > Android > SDK Manager from the main menu.
  2. Click the SDK Tools tab.
  3. Check the box next to NDK, CMake, and LLDB
  4. Click apply

(Using Android Studio 3.6.1)

Edit: Following above process on later versions of Android Studio still gives an NDK not installed error during the build process. This answer given by Violet Giraffe solved the issue. Once the necessary files are installed, go to File > Project Structure > SDK and select the NDK version.

See this guide for more details.

TrampolineTales
  • 808
  • 3
  • 14
  • 31
Umair
  • 638
  • 1
  • 5
  • 11
14

For above Android Studio 4.0, paste this in local.properties:

ndk.dir=C\:\\Users\\USER\\AppData\\Local\\Android\\Sdk\\ndk\\21.3.6528147

For Mac Users, paste this in local.properties:

ndk.dir=/Users/USERNAME/Library/Android/sdk/ndk/23.1.7779620 
Manasvi
  • 450
  • 7
  • 9
12

worked !!

  1. Go to SDK manager > android sdk(left side) > SDK Tools
  2. download or if availble then update to NDK(side_by_side) and CMake

3. now, do OK or Apply 4. Get back and click on "Install NDK '21.0.6113669' and sync project" in your failed build.

Happy coding :)

UTTAM
  • 319
  • 3
  • 4
7

I ran into this error while trying to run SQLite Android Bindings. It still uses a very old gradle configuration (included below).

TLDR

Please take protossor's advice and use NDK (Side by side) and set an ndkVersion in your build.gradle. Only use this on very old projects.

For old projects, you must install the obsolete NDK from the SDK manager. In Android Studio, open the SDK Manager and choose the SDK Tools tab. Uncheck Hide Obsolete Packages, and then scroll to the bottom to find NDK (Obsolete).

More Details

Android Studio, SDK Manager, SDK Tools Tab, Hide Obsolete Packages Unchecked, NDK (Obsolete) checked

I ran ./gradlew assembleRelease per the instructions, and I got the following:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':sqlite3'.
> NDK not configured.
  Download it with SDK manager.

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

* Get more help at https://help.gradle.org

BUILD FAILED in 7s

I got a similar error in Android Studio when it attempted to configure the project. After I installed the NDK (Obsolete) package, everything configured properly.

SQLite's woefully out of date gradle configuration:

$ROOT/build.gradle:

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

$ROOT/sqlitetest/build.gradle:

android {
    compileSdkVersion 25

    defaultConfig {
        minSdkVersion 16
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
...
}
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
6

//Try this one 1 - Android Studio -> Tools -> SDK Manager -> select SDK Tools

2 - Check the boxes next to LLDB, CMake, and NDK

3 - Apply and press OK button .. enter image description here

Ijas Aslam
  • 61
  • 2
  • 1
4

with android studio 3.0 + this issue is approached differently.

Go to File > Settings > Android SDK > and select the SDK entry and update. It should fix the issue.

the other approach to the same problem is when you do a gradle project sync it will automatically identify the issue of requiring version upgrade and provide you clickable link in the console. Through these steps it might also prompt you to update other dependencies, such as CMake.

Gautam
  • 1,030
  • 13
  • 37
4

open android->local.properties (If this file is not exist, create local.properties file in android folder) and add

ndk.dir=/Users/{{username}}/Library/Android/sdk/ndk-bundle #in mac
ndk.dir=C:\\Users\\{{username}}\\AppData\\Local\\Android\\sdk\\ndk-bundle #in Windows

Check ndk-bundle folder is exists in android sdk folder. Otherwise install the ndk(side by side) using sdk manager in android studio then rebuild the app

ANDROID STUDIO -> tools -> SDK manager -> SDK tools -> ndk (side by side)

or

ANDROID STUDIO -> preferences -> appearance & behavior -> system settings -> android SDK -> SDK tools -> ndk (side by side)
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
3

if you see this error you may have not any NDK in your SDK.

so you better download it from hereand copy all folders and files in your SDK.

also you can go; open project>select tools[in upper page]>Android>SDK Manager[from main menu]>click SDK Tools tab>and check the box next to NDK

if you have not NDK in SDK Tools tab,you can download automatic in this folder

A.Bahrami
  • 228
  • 3
  • 9
3

NDK(side by side) 21.x.xxxxx is the key to solution.

Go to the SDK tools as mentioned in above/other answers, then you may need to experiment with ndk version.

Click show package details to know what versions are available.

Also, you may need to add something like this in the local.properties. Like ndk.dir=/home/username/Android/Sdk/ndk/21.x.xxxxx

PS: you may also need to upgrade the gradle plugin depending upon which version you are using

My env: MAC M1

  • This answer worked for me. I could not select an NDK version from the project structure menu, and it *looked like* the NDK was already installed in the SDK manager. It wasn't until i used the "Show Package Details" checkbox that i realized that the installed NDK version was different than the one in my build.gradle file. :-/ Thanks! – jetsetter Sep 10 '21 at 17:18
2

For Linux:
Create file local.properties:

ndk.dir=/home/username/Android/Sdk/ndk/21.3.6528147

Now close the project and import it again.

mortalis
  • 2,060
  • 24
  • 34
ranojan
  • 819
  • 8
  • 11
  • 1
    Warning: `NDK was located by using ndk.dir property. This method is deprecated and will be removed in a future release. Please delete ndk.dir from local.properties and set android.ndkVersion to [20.0.5594570] in all native modules in the project.` – mazunki Mar 23 '21 at 10:23
0

My version is a little bit different :

From an open project, select Tools > Android > SDK Manager from the main menu. In Appearance & behavior > System Settings > Android SDK I have to Click the SDK Tools tab. Check the box next to NDK Click apply

0

Make sure you add the version which is mentioned in SDK manager in your gradle.build file under android directory like this.

android { ndkVersion "25.1.8937393" // whichever version you have in SDK manager. }

Sriraksha
  • 459
  • 1
  • 8
  • 15