PC パソコン

Android Studio: Causes of “Execution failed for task” and How to Fix It

When you build an app in Android Studio,
Execution failed for task ':app:...'
may be displayed and the build may stop.

This error indicates that a problem occurred while Gradle was executing a specific build task.

However,
the message
Execution failed for task
itself does not necessarily indicate the direct cause of the error.
The actual cause is often described immediately after it
or in the content displayed after
Caused by:.

Possible causes include
Kotlin or Java compilation errors,
XML resource errors,
Manifest problems,
dependency conflicts,
DEX conversion failures,
code generation errors,
signing settings,
and mismatched Java or Gradle versions.

This article explains the main causes of
Execution failed for task
in Android Studio
and how to identify the actual cause from Build Output and resolve it step by step.

What Is Execution failed for task?

Execution failed for task
is a message displayed when one of the build tasks being executed by Gradle fails.

When building an Android app,
many tasks are executed in sequence,
including compilation,
resource processing,
Manifest merging,
conversion to DEX,
and generation of APK or AAB files.

If one of these tasks fails,
a message such as the following is displayed.

Execution failed for task ':app:compileDebugKotlin'.

Alternatively,
a different task name may be displayed as follows.

Execution failed for task ':app:processDebugResources'.

Point:
Execution failed for task
is not the type of error itself,
but often indicates the result that
“this Gradle task failed to execute.”
To investigate the actual cause,
it is important to check the task name
and the detailed error message displayed below it.

Typical Error Messages

Execution failed for task ':app:compileDebugKotlin'.

Execution failed for task ':app:compileDebugJavaWithJavac'.

Execution failed for task ':app:processDebugResources'.

Execution failed for task ':app:mergeDebugResources'.

Execution failed for task ':app:mergeDebugNativeLibs'.

Execution failed for task ':app:checkDebugDuplicateClasses'.

Execution failed for task ':app:mergeExtDexDebug'.

Execution failed for task ':app:packageDebug'.

Depending on the task name,
you can narrow down the process in which the problem occurred to some extent.

First, Check the Cause Shown Below Execution failed for task

When this error is displayed,
it is important not to determine the solution by looking only at the single line
Execution failed for task.

Build Output may show a more specific cause below it.

Execution failed for task ':app:compileDebugKotlin'.

> Compilation error. See log for more details

Also,
Caused by:
may be displayed as follows.

Execution failed for task ':app:someTask'.

Caused by: Some specific error message

In this case,
the part corresponding to
Some specific error message
is the actual cause that needs to be fixed.

When
Execution failed for task
is displayed,
check Build Output from the top
and look for the first specific error that occurred.
Later errors may have occurred as a chain reaction caused by the first error.

Narrow Down the Cause from the Task Name

Looking at the task name displayed after
Execution failed for task
makes it easier to determine which process failed.

For example,
task names such as the following may be displayed.

:app:compileDebugKotlin
:app:compileDebugJavaWithJavac
:app:processDebugResources
:app:mergeDebugResources
:app:checkDebugDuplicateClasses
:app:mergeExtDexDebug
:app:packageDebug

For
compileDebugKotlin,
prioritize checking Kotlin code.
For
compileDebugJavaWithJavac,
prioritize Java code.
For
processDebugResources
or
mergeDebugResources,
prioritize resource-related issues.

Cause 1: Kotlin Compilation Error

If an error such as the following occurs,
Kotlin code compilation has failed.

Execution failed for task ':app:compileDebugKotlin'.

In this case,
the task name alone does not reveal the specific cause.

If you check slightly above in Build Output,
a specific error output by the Kotlin compiler may be displayed.

Unresolved reference: SampleClass

Type mismatches,
incorrect function calls,
syntax errors,
and other problems may also be the cause.

In this case,
fix the first error displayed by the Kotlin compiler
rather than
Execution failed for task.

Cause 2: Java Compilation Error

If a task such as the following fails,
check for Java code compilation errors.

Execution failed for task ':app:compileDebugJavaWithJavac'.

Causes may include spelling mistakes in Java code,
errors referencing classes or methods,
type mismatches,
and missing imports.

Check the line containing
error:
in Build Output
and fix the first Java compilation error.

Cause 3: There Is an Error in XML or Resources

If a task such as the following fails,
there may be a problem with XML or Android resources.

Execution failed for task ':app:processDebugResources'.

Or,

Execution failed for task ':app:mergeDebugResources'.

Possible causes include incorrect XML syntax,
references to nonexistent resources,
duplicate resource names,
and invalid file names.

Check the XML files in the
res
folder
and the resource names displayed in Build Output.

Check Whether You Are Referencing a Nonexistent Resource

If XML or another file references a resource that does not exist,
the resource processing task may fail.

@string/sample_text

In this case,
check whether
sample_text
is actually defined.

<string name="sample_text">Sample</string>

Cause 4: There Is a Problem with the Manifest

If a problem occurs while merging or processing AndroidManifest.xml,
a Gradle task may fail.

If a message such as
Manifest merger failed
appears in Build Output,
check for Manifest conflicts or configuration errors.

Manifest merger failed

In this case,
fix the problem based on the detailed Manifest merger error
rather than
Execution failed for task.

Cause 5: Dependencies Are Conflicting

Gradle dependency conflicts may cause a task to fail,
such as when the same class is included in multiple libraries.

A task name such as the following may be displayed.

Execution failed for task ':app:checkDebugDuplicateClasses'.

In the details,
an error such as the following may be displayed.

Duplicate class com.example.SomeClass found in modules ...

In this case,
check the duplicated libraries,
remove unnecessary dependencies,
or exclude the duplicated dependency if necessary.

Check Gradle Dependencies

If dependencies are thought to be the cause,
check the Gradle dependency tree.

./gradlew app:dependencies

On Windows,
you can run it as follows.

gradlew app:dependencies

Check whether the same library has been added through multiple paths
or whether different versions are mixed.

Cause 6: DEX Conversion Has Failed

Android apps include a process that converts compiled code into DEX format.

If a problem occurs during this process,
a task such as the following may fail.

Execution failed for task ':app:mergeExtDexDebug'.

Possible causes include
library conflicts,
duplicate classes,
and compatibility problems.

Check the detailed error message
and identify the library or class causing the DEX conversion to fail.

Cause 7: The Java Version Does Not Match

If the Java version required by the Android Gradle Plugin or Gradle
does not match the Java version actually being used,
a Gradle task may fail.

Check whether Build Output displays an error related to the Java version.

Check the Gradle JDK setting in Android Studio
and the combination of the Android Gradle Plugin and Gradle you are using.

Cause 8: There Is a Problem with the Gradle or Android Gradle Plugin Version

If the combination of versions of
Gradle,
Android Gradle Plugin,
Kotlin,
Java,
and other components is incompatible,
an error may occur when executing a task.

In particular,
if
Execution failed for task
occurs immediately after updating Android Studio
or the Android Gradle Plugin,
check the combination of versions.

If Build Output specifically shows the required Gradle or Java version,
modify the settings according to that information.

Cause 9: Code Generation Has Failed

If code is automatically generated by a library or build tool,
Execution failed for task
may be displayed when the code generation task fails.

In this case,
check the task name and Build Output
and see whether there is an error in the input data or settings
before the code generation process.

As a result of generated classes not being created,
Unresolved reference
or
Cannot resolve symbol
may also occur during subsequent compilation.

Cause 10: There Is a Problem with Signing Settings

When creating an APK or AAB,
a problem with signing settings may cause a task related to package generation to fail.

Check whether the keystore path,
alias,
password,
signingConfig,
and other settings are correct.

In particular,
if
Execution failed for task
occurs only for a release build,
check the signing settings for release.

Cause 11: Files or Directories Cannot Be Accessed

A task may also fail
if Gradle cannot read a required file
or cannot write to the output destination.

Check whether messages such as
Permission denied,
File not found,
or
Access denied
are displayed in Build Output.

Check the file path,
whether the file exists,
access permissions,
and other related settings.

Cause 12: Disk Space Is Insufficient

During a build,
free space is required to save the Gradle cache,
intermediate files,
APK or AAB files,
and other data.

If disk space is insufficient,
file creation or writing may fail
and a Gradle task may stop.

Check the available storage space
and remove unnecessary files if space is insufficient.

Cause 13: The Error Occurs Only in a Specific Build Variant

A project may build successfully with
debug
but produce
Execution failed for task
only with
release.

In this case,
check settings that differ between debug and release.

src/main/
src/debug/
src/release/

Settings that differ by build type,
such as signing settings,
ProGuard or R8,
source set,
buildConfigField,
and resources,
may be the cause.

Cause 14: R8 or Code Shrinking Has Failed

If R8 or another code shrinking process is used in a release build,
the task may fail during that process.

If the debug build succeeds but only the release build fails,
check whether an R8- or ProGuard-related error is displayed in Build Output.

Fix the problem based on the detailed error message,
such as Missing class,
rule settings,
or compatibility with libraries.

Cause 15: There Is a Native Library Conflict

If native libraries included in JNI,
AAR,
or other components are duplicated,
a task such as the following may fail.

Execution failed for task ':app:mergeDebugNativeLibs'.

Check the details in Build Output
to see whether the same
.so
file is included in multiple libraries.

Check dependencies and packaging settings
and identify the cause of the duplicated file.

Cause 16: Gradle Sync Itself Has Not Completed Successfully

If you build while problems remain in Gradle Sync,
dependencies or project settings may not be loaded correctly
and a build task may fail.

If an error is displayed during Gradle Sync,
fix the Sync problem before the build error.

After Sync completes successfully,
run the build again.

Run Gradle Sync

If you change
build.gradle,
build.gradle.kts,
Version Catalog,
plugin settings,
or other related settings,
run Gradle Sync.

If an error occurs during Sync,
check its contents and fix it.

Run Clean or Rebuild

If the error remains even after fixing the code or settings causing the problem,
old build results or intermediate files may be affecting the project.

From the
Build
menu in Android Studio,
perform the equivalent of Clean or Rebuild
available in the version of Android Studio you are using.

After that,
build again and check the result.

Check Detailed Logs from the Command Line

If it is difficult to determine the cause from Android Studio’s Build Output alone,
you can run Gradle from the command line to check more details.

./gradlew assembleDebug --stacktrace

On Windows,
run it as follows.

gradlew assembleDebug --stacktrace

If more detailed logs are needed,
you can also use options such as the following.

./gradlew assembleDebug --info

Or,

./gradlew assembleDebug --debug

Since the amount of log output increases,
normally check
--stacktrace
first.

Check the Beginning of the Error and Caused by

In long Build Output,
multiple
Execution failed for task
messages may be displayed.

In this case,
check the first specific error that occurred
and the contents of
Caused by:.

If you try to fix only the errors displayed later,
you may overlook the root cause.

If Execution failed for task Still Cannot Be Resolved

If you do not know the cause,
check each item in order based on the task name and Build Output.

  1. Check the task name displayed after Execution failed for task.
  2. Check the first specific error that occurred in Build Output.
  3. If Caused by: is displayed, check its contents.
  4. Check whether there are Kotlin or Java compilation errors.
  5. Check whether there are XML or resource errors.
  6. Check whether there are Manifest-related errors.
  7. Check whether there are conflicts or duplicates in Gradle dependencies.
  8. Check whether there are DEX conversion-related errors.
  9. Check the versions of Java, Gradle, and the Android Gradle Plugin.
  10. Check whether the code generation process has failed.
  11. If the problem occurs only in release, check signing settings and R8.
  12. If you are using a Native Library, check for duplicates.
  13. Check whether Gradle Sync has completed successfully.
  14. Check whether there are problems with file access or disk space.
  15. If necessary, run Clean or Rebuild.
  16. If you cannot determine the cause, check the details with --stacktrace.

When Many Execution failed for task Messages Are Displayed

Multiple
Execution failed for task
messages may be displayed in Build Output.

In this case,
it does not necessarily mean that every task has a separate problem.

For example,
an initial compilation error or resource error may prevent later tasks from running,
causing multiple errors to be displayed in a chain.

If multiple
Execution failed for task
messages are displayed,
it is important not to look only at the last error,
but to find the first specific error that occurred in Build Output.
Fixing one root cause may resolve multiple task errors at the same time.

Summary

Android Studio’s
Execution failed for task
is displayed when Gradle fails to execute a specific build task.

However,
this message alone often does not identify the specific cause,
so you need to check the task name,
Build Output,
Caused by:,
and other details.

Main causes include
Kotlin or Java compilation errors,
XML or resource errors,
Manifest problems,
Gradle dependency conflicts,
DEX conversion failures,
mismatched Java or Gradle versions,
code generation,
signing settings,
R8,
and Native Library issues.

Even if multiple task errors are displayed,
they may be chained from one fundamental build error.

When resolving
Execution failed for task,
it is important to separately check
“which task failed”
and
“why that task failed”
rather than focusing only on the error message itself.
Checking the first specific error in Build Output
makes it easier to narrow down the cause.