[Android]Unresolved reference: appcompat

question

I created a Kotlin class, then imported and synchronized the dependency implementation ("androidx.appcompat:appcompat:1.6.1"), but the Class still prompted an error or Unresolved reference: appcompat .

code show as below:

package com.example.gatestdemol 
import androidx.appcompat.app.AppCompatActivity 

class KTBNActivity: AppCompatActivity { 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
} 

I was speechless. This question stuck with me for a long time.

 

solve

Unresolved reference: appcompat errors usually mean that the Kotlin compiler cannot find the specified class or package.

To resolve this issue, try the following steps:

1. Make sure the import is correct:

Check that you have imported AppCompatActivity correctly in your code. Normally, when you inherit from AppCompatActivity, the IDE should automatically import the correct package. If not, you can add the import statement manually:

import androidx.appcompat.app.AppCompatActivity

2. Check that dependencies are synchronized correctly:

Even if you add dependencies, sometimes Gradle needs to be resynced. In Android Studio, click the Sync Now prompt or use  File -> Sync Project with Gradle Files.

3. Check the project structure:

Make sure there are no issues with your project structure. In the left panel of Android Studio, check that the src folder structure under the app module is correct.

4.Invalidate Caches / Restart:

If the problem persists, try invalidating the cache and restarting Android Studio. Select  File -> Invalidate Caches / Restart and then select  Invalidate and Restart .

5. Check Android Studio and Gradle versions:

Make sure your Android Studio and Gradle plugins are up to date. Sometimes, outdated tools can cause synchronization and compilation issues.

6. Check Gradle Console:

During the sync or build process, check the Gradle Console output for any error messages.

Note : There may also be network reasons that cause problems with dependent synchronization. Then re-synchronize after surfing the Internet safely. I encountered this reason

If none of the above steps resolves your issue, you may need more detailed error information to further diagnose the issue. In this case, you may want to check the IDE's logs or ask the community for help.

Guess you like

Origin blog.csdn.net/u012881779/article/details/134375269