Q:

How to fix Gradle sync failed CreateProcess error=2 in Android studio? (Windows)

I'm running into this problem and searching on web I have not found solution right now.
I have tryed using different JDK location but problem persist. 
How to fix it?

log:

29 15:07:07,327 [e-1024-b02]   INFO - idea.project.IndexingSuspender - Starting batch update for project: Project 'C:\Users\lab\Desktop\appandroid\heartbeat-android-master' heartbeat-android-master 
  2018-08-29 15:07:08,173 [e-1024-b02]   INFO - idea.project.IndexingSuspender - Consuming IndexingSuspender deactivation event: BUILD_FINISHED 
  2018-08-29 15:07:08,173 [e-1024-b02]   INFO - idea.project.IndexingSuspender - Finishing batch update for project: Project 'C:\Users\lab\Desktop\appandroid\heartbeat-android-master' heartbeat-android-master 
  2018-08-29 15:07:08,174 [e-1024-b02]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished in 890ms 
  2018-08-29 15:07:08,179 [e-1024-b02]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
  2018-08-29 15:07:08,204 [e-1024-b02]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
  2018-08-29 15:07:08,239 [thread 150]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
  2018-08-29 15:07:08,253 [thread 150]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 14ms; general responsiveness: ok; EDT responsiveness: ok 
  2018-08-29 15:07:14,837 [e-1024-b02]   INFO - ide.actions.ShowFilePathAction - 
  Exit code 1 

event log:
 org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:ndkBuild'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'C:\Users\danilo\AppData\Local\Android\Sdk\ndk-bundle/ndk-build''
    at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:222)
    at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:202)
    at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:342)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:86)
    at org.gradle.internal.operations.BuildOperationIdentifierPreservingRunnable.run(BuildOperationIdentifierPreservingRunnable.java:39)
    ... 6 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'C:\Users\danilo\AppData\Local\Android\Sdk\ndk-bundle/ndk-build'
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
    at net.rubygrapefruit.platform.internal.WindowsProcessLauncher.start(WindowsProcessLauncher.java:22)
    at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:68)
    ... 7 more
Caused by: java.io.IOException: Cannot run program "C:\Users\danilo\AppData\Local\Android\Sdk\ndk-bundle/ndk-build" (in directory "C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app"): CreateProcess error=2, Impossibile trovare il file specificato
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
    ... 10 more
Caused by: java.io.IOException: CreateProcess error=2, Impossibile trovare il file specificato
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 11 more

Build.grandle:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId "com.prouast.heartbeat"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }

    // add a task that calls regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // relative path to jni source
                'NDK_LIBS_OUT=../jniLibs',
                '-j', Runtime.runtime.availableProcessors(),
                'all',
                'NDK_DEBUG=1'
    }

    task ndkClean(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // relative path to jni source
                'NDK_LIBS_OUT=../jniLibs',
                'clean'
    }

    // add this task as a dependency of Java compilation
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(['ndkBuild'])
    }

    clean.dependsOn(['ndkClean'])

    sourceSets.main {
        jni.srcDirs = []
        //jniLibs.srcDir 'src/main/libs'
    }

    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'org.apache.commons:commons-io:1.3.2'
    implementation 'com.android.support:appcompat-v7:23.1.1'
    implementation project(':libraries:opencv')

EDIT:
Following @Alex Cohn suggestion I've solved some problem but I'm running into others errors, following log errors file (what's wrong?):
Build command failed.

Error while executing process C:\Users\danilo\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\src\main\jni\Android.mk NDK_APPLICATION_MK=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\src\main\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=1 APP_PLATFORM=android-15 NDK_OUT=C:/Users/danilo/Desktop/appandroid/heartbeat-android-master/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\build\intermediates\ndkBuild\debug\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}

Android NDK: WARNING: APP_STL gnustl_shared is deprecated and will be removed in the next release. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.    

process_begin: CreateProcess(NULL, "", ...) failed.

../../../../sources/ffmpeg-2.2.3/android/arm/Android.mk: No such file or directory
Open File

Android NDK: Module FFmpegEncoder depends on undefined modules: avformat-55 avcodec-55 avutil-52 swscale-2    
Open File

*** Android NDK: Aborting (set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies)    .  Stop.
Open File

Build command failed.

Error while executing process C:\Users\danilo\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\src\main\jni\Android.mk NDK_APPLICATION_MK=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\src\main\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=0 APP_PLATFORM=android-15 NDK_OUT=C:/Users/danilo/Desktop/appandroid/heartbeat-android-master/app/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=C:\Users\danilo\Desktop\appandroid\heartbeat-android-master\app\build\intermediates\ndkBuild\release\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}

Android NDK: WARNING: APP_STL gnustl_shared is deprecated and will be removed in the next release. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.    

process_begin: CreateProcess(NULL, "", ...) failed.

../../../../sources/ffmpeg-2.2.3/android/arm/Android.mk: No such file or directory
Open File

Android NDK: Module FFmpegEncoder depends on undefined modules: avformat-55 avcodec-55 avutil-52 swscale-2    
Open File

*** Android NDK: Aborting (set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies)    .  Stop.
Open File

A:

Your gradle script was tuned for Linux, not for Windows. On Windows, you need "$ndkDir/ndk-build.cmd" instead of "$ndkDir/ndk-build".
But even better, if you use the Android Studio 3.2 or later, rewrite your gradle script to use the new NDK integration, see the official walkthrough.
Here is how build.gradle may look:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId "com.prouast.heartbeat"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'org.apache.commons:commons-io:1.3.2'
    implementation 'com.android.support:appcompat-v7:23.1.1'
    implementation project(':libraries:opencv')
}

android {
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
    defaultConfig {
        ndk {
            abiFilters 'armeabi-v7a'
        }
    }
}

Unfortunately, I cannot guarantee that your src/main/jni/Android.mk will work correctly on your Windows machine. Also, make sure that you use compatible versions of NDK and Gradle plugin.

