PC パソコン

Android Studio: Causes of “Cannot resolve symbol” and How to Fix It

When writing code in Android Studio,
Cannot resolve symbol
may be displayed, and class names, variable names, method names, and other elements may appear in red.

This error occurs when Android Studio cannot correctly recognize a symbol written in the code.

There are various causes,
including simple spelling mistakes,
missing import statements,
missing Gradle dependencies,
moved classes or methods,
dependencies between modules,
failed resource generation,
and failed Gradle Sync.

This article explains the main causes of
Cannot resolve symbol
in Android Studio
and how to check the symbol causing the error and resolve the problem step by step.

What Is Cannot resolve symbol?

Cannot resolve symbol
is an error displayed when Android Studio cannot resolve a class,
variable,
method,
field,
or other element specified in the code.

For example,
even if you are using a class called
SampleClass,
Android Studio may be unable to find that class.

SampleClass sample = new SampleClass();

In this case,
the following error may be displayed for
SampleClass.

Cannot resolve symbol 'SampleClass'

Point:
Cannot resolve symbol
indicates a state in which Android Studio
“does not know what that name refers to.”
You need to check not only the name causing the error,
but also imports,
dependencies,
scope,
modules,
the state of Gradle,
and other related factors.

Typical Error Messages

Cannot resolve symbol 'SampleClass'

For a method,
it may be displayed as follows.

Cannot resolve method 'sampleMethod'

In Android development,
symbols such as the following may also fail to be recognized.

Cannot resolve symbol 'R'
Cannot resolve symbol 'BuildConfig'
Cannot resolve symbol 'RecyclerView'
Cannot resolve symbol 'AppCompatActivity'

The cause you should check differs depending on which symbol cannot be resolved.

First, Check the Symbol Name Causing the Error

When
Cannot resolve symbol
is displayed,
first check the name causing the error.

Cannot resolve symbol 'SampleClass'

In this case,
check where
SampleClass
is defined.

The place you need to investigate differs depending on whether it is
a class you created yourself,
a class from the Android SDK,
a class from AndroidX,
or a class from an external library.

Cause 1: The Name Is Misspelled

The most basic cause is a spelling mistake in a class name,
variable name,
method name,
or other identifier.

For example,
if the actual class name is
SampleClass
but it is written as follows.

SampleClas sample = new SampleClas();

Correct it to the proper class name.

SampleClass sample = new SampleClass();

Java and Kotlin are case-sensitive,
so check that the name matches exactly.

Cause 2: An import Statement Is Missing

If the class you want to use exists in another package,
Cannot resolve symbol
may occur because the required import statement has not been added.

For example,
when using
Intent,
an import such as the following is required.

import android.content.Intent;

In Android Studio,
import candidates may be displayed
when you place the cursor on the class name causing the error
or use a quick fix.

Check Whether You Imported the Wrong Class

Even if an import statement exists,
you may have imported a different class with the same name from the one you intended to use.

Especially when multiple libraries contain classes with the same name,
check the package name in the import statement as well.

import com.example.library.SampleClass;

If the class you want to use exists in another package,
change the import to the correct one.

Cause 3: A Required Gradle Dependency Has Not Been Added

If you are using a class from an external library or AndroidX,
Cannot resolve symbol
may occur if the required Gradle dependency has not been added.

For example,
if you want to use a class from a certain library,
add the required dependency to the module’s
build.gradle
or
build.gradle.kts.

dependencies {
    implementation("com.example:sample-library:VERSION")
}

Specify the version of the library you want to use in
VERSION.

Check which library contains the class causing the error
and whether the required dependency has been added.

Run Gradle Sync After Adding Dependencies

If you change
build.gradle
or
build.gradle.kts,
run Gradle Sync.

Even if a dependency is written,
Android Studio may be unable to recognize the class
if Gradle synchronization has not completed successfully.

If another error is displayed during Sync,
fix that error first.

Cause 4: A Class or Method Was Removed After a Library Version Change

If
Cannot resolve symbol
occurs after updating a library,
the class or method you were using may have been changed,
moved,
or removed in the new version.

A class that was available in an older version
may have been moved to another package in a newer version.

Check the official documentation or change history of the library you are using
and modify the code to use classes or methods available in the current version.

Cause 5: A Class Was Deleted or Moved to Another Package

Cannot resolve symbol
may also occur if a class has been deleted during refactoring
or moved to another package.

Check whether an import referring to the old package remains.

import com.example.old.SampleClass;

If the class was moved,
modify the import to match the current package.

Cause 6: A Variable or Method Is Out of Scope

Even if a variable or method exists,
the symbol may not be resolvable
if it cannot be accessed from the current location.

For example,
this occurs when you try to use a local variable defined inside one method
from another method.

void sample() {
    String message = "Hello";
}

void anotherSample() {
    System.out.println(message);
}

In this example,
message
is a local variable inside
sample(),
so it cannot be referenced from
anotherSample().

If necessary,
move the variable or method to a scope where it can be referenced.

Cause 7: It Cannot Be Referenced Because of an Access Modifier

Depending on the access modifiers of classes,
methods,
fields,
and other elements,
they may not be accessible from another class or package.

For example,
if the target is defined as
private,
it cannot be directly referenced from outside the class in which it is defined.

private String message = "Hello";

Check whether the required access range is configured
and modify it according to the class design.

Cause 8: A Class in Another Module Cannot Be Referenced

In an Android project made up of multiple modules,
dependencies between modules are required
to use classes from another module.

For example,
if you want to use the
samplelibrary
module from the
app
module,
configure a dependency such as the following.

dependencies {
    implementation(project(":samplelibrary"))
}

Even if the module exists in the project,
classes from that module cannot be used
if a dependency allowing
app
to reference it has not been configured.

Cause 9: R Becomes Cannot resolve symbol

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

Cannot resolve symbol 'R'

R
is a class generated to reference resources in an Android app.

If
R
is not recognized,
another error may have occurred in an XML resource
rather than in the Java or Kotlin code itself.

Check whether there are errors in XML files inside the
res
folder,
whether resource names are correct,
and whether another resource error is displayed in Build Output.

Check Whether the Wrong R Has Been Imported

For errors related to
R,
also check whether an unintended
R
class has been imported.

Check the import section
and make sure that an
R
from a package different from your own app has not been added.

If there is an unnecessary import,
remove it
and make sure the app refers to the correct
R.

Cause 10: The Specified Resource Does Not Exist

Even if
R
itself is recognized,
only a specific ID or layout name may fail to be recognized.

R.id.sampleButton

In this case,
check whether
sampleButton
is actually defined in the XML.

android:id="@+id/sampleButton"

Also check the spelling of the resource name
and the layout file being referenced.

Cause 11: BuildConfig Becomes Cannot resolve symbol

In Android projects,
an error such as the following may occur.

Cannot resolve symbol 'BuildConfig'

BuildConfig
is a class generated by the Android build process.

Depending on the module you are using
and the Android Gradle Plugin settings,
you may need to check the
BuildConfig
generation settings.

If necessary,
check
buildFeatures
in the Android module.

android {
    buildFeatures {
        buildConfig = true
    }
}

Also check whether you are accidentally referring to
BuildConfig
from another module.

Cause 12: AndroidX and the Old Support Library Are Mixed

In older Android projects,
classes may not be resolved correctly
because AndroidX and the old Support Library are mixed.

For example,
check whether code using classes from the old Support Library remains.

import android.support.v7.app.AppCompatActivity;

If the project has migrated to AndroidX,
check whether you are using the corresponding AndroidX class.

import androidx.appcompat.app.AppCompatActivity;

Check the AndroidX Settings

In a project that has migrated to AndroidX,
also check
gradle.properties.

android.useAndroidX=true
android.enableJetifier=true

android.useAndroidX=true
is a setting for using AndroidX.

android.enableJetifier=true
has been used to convert some dependencies that refer to the old Support Library
for use with AndroidX.

If all dependent libraries support AndroidX,
check the settings according to the Android Gradle Plugin
and library configuration you are using.

Cause 13: AndroidX Classes Such as RecyclerView Are Not Recognized

For example,
an error such as the following may be displayed.

Cannot resolve symbol 'RecyclerView'

In this case,
check whether the AndroidX library required for RecyclerView
has been added to the dependencies.

Also check whether the correct package has been imported.

import androidx.recyclerview.widget.RecyclerView;

Cause 14: Generated Code Has Not Been Created

If you are referencing classes automatically generated by a library or build tool,
Cannot resolve symbol
may be displayed when code generation fails.

In this case,
check Build Output
and see whether an error related to code generation or a Gradle task
occurred before
Cannot resolve symbol.

Cannot resolve symbol
may not be the cause itself;
the symbol may not have been generated
as a result of a build error that occurred earlier.
In Build Output,
it is important to check from the first error that occurred.

Cause 15: Source Sets Such as debug and release Are Different

In Android projects,
source sets such as
debug,
release,
and Product Flavors
can be used in addition to
main.

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

If a class exists only in
debug,
that class cannot be referenced from a
release
build.

If
Cannot resolve symbol
occurs only for a specific build variant,
check the source set in which the class is located.

Cause 16: Gradle Sync Is Failing

If Android Studio cannot correctly load dependencies
or the project configuration,
many classes may become
Cannot resolve symbol.

If Gradle Sync is failing,
check the error displayed during Sync.

Fix problems with repositories,
library versions,
Gradle settings,
and other factors,
and make sure Gradle Sync can complete successfully.

Check Whether Gradle Sync Resolves the Problem

If
Cannot resolve symbol
is displayed immediately after adding a dependency
or changing Gradle settings,
run Gradle Sync.

After Sync completes successfully,
check whether Android Studio can now recognize the symbol.

Run Clean or Rebuild

If the error remains even after modifying the code or dependencies,
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 whether the error has been resolved.

When Only Android Studio Shows Red Error Text

Even when the actual Gradle build succeeds,
Cannot resolve symbol
may be displayed only in the Android Studio editor.

In this case,
the project information held by the IDE
and the state on the Gradle side may not match.

First run Gradle Sync,
reload the project,
and check whether the state is updated.

If the actual build is also failing,
the problem is not limited to the IDE display,
so check the compilation errors displayed in Build Output.

Difference Between Cannot resolve symbol and Unresolved reference

Cannot resolve symbol
and
Unresolved reference
have different wording,
but both occur when a name in the code cannot be resolved correctly.

Cannot resolve symbol
may be seen in Java code or in the Android Studio editor,
while Kotlin may display
Unresolved reference.

In both cases,
the points to check are similar,
including spelling,
imports,
dependencies,
scope,
and module configuration.

If Cannot resolve symbol Still Cannot Be Resolved

If you do not know the cause,
check each item in order based on the symbol causing the error.

  1. Check whether the symbol name is spelled correctly.
  2. Check whether uppercase and lowercase letters are correct.
  3. Check whether the required import statement has been added.
  4. Check whether you have imported the wrong class with the same name.
  5. Check whether the required Gradle dependency has been added.
  6. Check whether Gradle Sync has completed successfully.
  7. Check whether the target class or method exists in the version of the library you are using.
  8. Check whether the class or method has been moved to another package.
  9. Check whether the variable or method can be referenced from the current scope.
  10. Check whether an access modifier prevents it from being referenced.
  11. If you are using another module, check dependencies between modules.
  12. For R, check XML and resource errors.
  13. For BuildConfig, check generation settings and the module being referenced.
  14. Check whether AndroidX and the old Support Library are mixed.
  15. If the error occurs only in a specific build variant, check the source set.
  16. Check whether another error occurred earlier in Build Output.
  17. If necessary, run Clean or Rebuild.

When Many Cannot resolve symbol Errors Are Displayed

Many
Cannot resolve symbol
errors may be displayed at the same time within the project.

In this case,
it does not necessarily mean that every symbol needs to be fixed individually.

For example,
if Gradle Sync fails and an external library cannot be loaded,
many classes contained in that library
may all become unrecognized at once.

Also,
if resource generation or code generation fails,
many related symbols may become errors at the same time.

If many
Cannot resolve symbol
errors are displayed,
it is important to check for a common cause,
such as Gradle Sync failure,
dependency loading failure,
resource errors,
or code generation errors,
before fixing them one by one.

Summary

Android Studio’s
Cannot resolve symbol
occurs when Android Studio cannot correctly recognize
a class,
variable,
method,
field,
or other name specified in the code.

Main causes include
spelling mistakes,
missing imports,
missing Gradle dependencies,
library API changes,
problems with scope or access range,
dependencies between modules,
a mixture of AndroidX and the old Support Library,
and differences in source sets.

If generated classes such as
R
or
BuildConfig
are not recognized,
check whether there is an earlier error
in XML resources,
build settings,
or other related areas
rather than in the class itself.

If many
Cannot resolve symbol
errors occur at the same time,
first check for a common cause,
such as a Gradle Sync or dependency problem.

When resolving
Cannot resolve symbol,
it is important to check
“where that symbol is originally defined.”
Once you know where it is defined,
you can narrow down the problem to
imports,
dependencies,
scope,
modules,
build settings,
or other related areas.