PC パソコン

Android Studio “Cannot invoke method android() on null object”: Causes and Solutions

When running Gradle Sync or building in Android Studio, you may see “Cannot invoke method android() on null object”. This error occurs when Gradle cannot evaluate the android block correctly.

Error message

A problem occurred evaluating project ':app'. Cannot invoke method android() on null object

Main causes

Typical causes include syntax errors in build.gradle, placing the android block in the wrong Gradle file, failing to apply the Android Gradle Plugin to the target module, or malformed Gradle code added by plugins used with Flutter, React Native, or Expo.

Check the syntax

In an actual reported case, the plugin application line and the android block were accidentally joined together, which caused the error. Separate the statements correctly and check line breaks, braces, and quotation marks.

Check the Gradle file location

The android block is normally written in the Android module file such as app/build.gradle or app/build.gradle.kts. Make sure it has not accidentally been moved to the root Gradle file.

Check the file name and line number shown in the error log, and inspect the lines immediately before it as well. A syntax error on a previous line can be the real cause.

Check the Android Gradle Plugin

Confirm that com.android.application or com.android.library is applied to the target module.

Check recently added plugins

In Flutter, React Native, and Expo projects, plugins or prebuild steps may modify Gradle files. If the error started immediately after adding a package, revert that change temporarily and check whether the project builds again.

Steps to check

  1. Check the Gradle file and line number shown in the error.
  2. Inspect line breaks, braces, and quotation marks, including the preceding lines.
  3. Confirm that the android block is in the module-level Gradle file.
  4. Confirm that the Android Gradle Plugin is applied.
  5. Review recently added Gradle settings and plugins.

Check the detailed log

Use Gradle’s stack trace option to identify which Gradle file and line failed during evaluation. After making corrections, run Gradle Sync and clean the project if necessary before building again.

The cause is not always the android() call itself. A syntax problem immediately before it or code inserted by an external plugin may be responsible.

Summary

If “Cannot invoke method android() on null object” appears, check the syntax of build.gradle, the location of the android block, whether the Android Gradle Plugin is applied, and any plugin or generated code added immediately before the problem began. Checking these items in order makes it easier to isolate the cause.