Меню

Unresolved reference android studio ошибка

When you’re writing code for Android application and Kotlin, you may frequently encounter a static error from your IDE saying unresolved reference for a specific keyword or variable.

For example, you can produce the error by calling a custom function that you haven’t defined in your code yet:

myFunction("Hello")
// Unresolved reference: myFunction

Or when you assign a variable without declaring them:

myStr = "Hello"
// Unresolved reference: myStr

To resolve the issue, you need to make sure that the referenced keyword (whether it’s a variable, a function, or an object) is available and defined before it’s called.

To declare a variable, use the val or var keyword. To declare a function, use the fun keyword:

val myStr = "Hello"

fun myFunction(str: String) = print(str)

myFunction(myStr)

// no issues

When coding an Android application, this error may happen when you import libraries that haven’t been added to your project yet.

For example, you may import the kotlinx library in your MainActivity.kt file as follows:

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

Without adding the kotlin-android-extensions in your build.gradle file, the import kotlinx line above will trigger the unresolved reference issue.

To resolve it, you need to add the kotlin-android-extensions to the build.gradle file plugins list as shown below:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

Please make sure that you add the id 'kotlin-android-extensions' line on the build.gradle file that’s inside your app module.

Once you sync the Gradle build, the error should disappear.

If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux.

The error should disappear after the build is completed.

To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you’re typing in the file points to.

It may be a function, a variable, or another construct of the language that you’ve yet to declare in the code.

Or it can also be external Android and Kotlin libraries that you haven’t added to your current project yet.

For example, you may encounter an error saying Unresolved reference: Volley as follows:

val queue = Volley.newRequestQueue(this)

// Unresolved reference: Volley

This is because the Volley library hasn’t been added to the Android project yet.

If you click on the red bulb icon on the code line with the issue, Android Studio has several suggestions to resolve the error, one of them helps you to import the dependency automatically:

In this case, you can simply click on the highlighted option above and Android Studio will resolve the problem for you.

But other libraries may not have such an option, so you need to import them manually in the build.gradle file of your app module.

Once you specify the import, don’t forget to sync the Gradle build again to download the library into your project.

Good luck on fixing the unresolved reference issue! 👍

I’m running Ubuntu 17.10, and I’ve just updated Android Studio from version 3.0.1 to 3.1. Here’s the version information in Help -> About:

Android Studio 3.1
Build #AI-173.4670197, built on March 22, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.0-37-generic

Unfortunately, while the IDE was able to resolve android-specific references before in my (first ever) Kotlin project, in the new version it appears unable to. Here’s a screenshot showing what I mean:

In Android Studio 3.1, it’s claiming that core Android functions such as setContentView() don’t exist, and keeps prompting me to create an abstract function for it — which I clearly don’t want to do. However, if I hit the build button, I get a BUILD SUCCESSFUL in 8s.

Why is my editor doing this, and how I get get Android-related functions and classes to resolve correctly again?

  • Build → Clean and then Build → Rebuild doesn’t help.
  • File → Invalidate Caches and Restart doesn’t help either
  • Neither does a manual Gradle sync.
  • A different project that’s written in Java instead of Kotlin works fine.
  • Link to the project I’m experiencing issues with (it’s open-source)

double-beep's user avatar

double-beep

4,84016 gold badges32 silver badges41 bronze badges

asked Mar 28, 2018 at 22:14

starbeamrainbowlabs's user avatar

14

  1. Exit Studio.
  2. Delete .idea/ (seems to me that’s most important), build/, app/build directories.
  3. Start Studio
  4. Set Settings -> Build -> Gradle settings back (last stable Gradle local distribution in my case).
  5. Clean&rebuild project.

Helps me.

answered Apr 1, 2018 at 7:12

Slava Glushenkov's user avatar

Slava GlushenkovSlava Glushenkov

1,3281 gold badge8 silver badges10 bronze badges

2

Delete {projectDir}/.idea/libraries, then go to File -> Sync Project with Gradle Files.

answered Apr 9, 2018 at 23:20

VikingBadger's user avatar

6

I had the same problem on Android Studio 3.2.1.

The solution was to use stable ‘com.android.tools.build:gradle:3.2.1’, not alpha…

In the project build.gradle change the version like the code below (or if there is a newer stable version)

dependencies {
  classpath 'com.android.tools.build:gradle:3.2.1'
} 

If this doesn’t solve your problem than in File >> choose Invalidate caches/Restart… and on the next dialog choose Invalidate and Restart

1 step

2 step

answered Oct 20, 2018 at 13:39

Dimitar's user avatar

DimitarDimitar

6297 silver badges6 bronze badges

3

I had a very similar issue:

Layout resources (ie: activity_view.xml) that worked fine would all of a sudden not be detected or show up in the autocomplete lists, etc.

I tried all these «delete .idea folder/invalidate caches/restart Android studio» solutions and nothing worked..

Solution that worked:

Turned out that an import statement of the entire R package (import android.R) had at some point been added to the import statements and was somehow creating the issue (for specific layout resources only for some bizarre reason)

Removing import android.R instantly resolved the issue, and putting it back recreated it instantly as well.

Hope that helps any that come across this post for the same/similar reason


answered Apr 15, 2020 at 15:45

SmokeyTBear's user avatar

1

Make sure you have not import android.R in any of your activities/fragments. This can happened when Android Studio tries to convert code from Java to Kotlin.

Delete this line and try to run your project.

enter image description here

answered May 16, 2020 at 20:22

Adriatik Gashi's user avatar

2

I had the same problem. None of the «fixes» listed above helped.

Just run in a terminal:

./gradlew --stop
./gradlew --rerun-tasks assemble{flavour}

For example:

./gradlew --stop
./gradlew --rerun-tasks assembleDevDebug

answered Oct 22, 2019 at 8:03

dbog's user avatar

dbogdbog

961 silver badge4 bronze badges

Goto File->Invalidate Cache/Restart works for me

answered May 18, 2020 at 7:29

shafeeq's user avatar

shafeeqshafeeq

1,4391 gold badge14 silver badges30 bronze badges

I tried all the solutions proposed here but they did not work. What worked for me was to disable and then re-enable the Kotlin plugin.

answered May 2, 2019 at 14:38

Diego Palomar's user avatar

Diego PalomarDiego Palomar

6,8782 gold badges29 silver badges42 bronze badges

4

It helped when I deleted Android Studio and installed again. The advices above didn’t help.

answered Feb 3, 2020 at 7:47

Anna's user avatar

AnnaAnna

5234 silver badges14 bronze badges

For me this step works :

I) Delete .idea folder from the android studio

II) Go to File > Invalidated caches/ Restarts

III) It will ask you to confirm and click on invalidate and restart.

IV) Go to Build > Clean project

V) Go to Build > Rebuild project

Try with this.

Happy Coding..!

answered Dec 27, 2018 at 8:54

Pranav P's user avatar

Pranav PPranav P

1,69519 silver badges38 bronze badges

What worked for me was to change the kotlin-gradle-plugin version to the same version of Kotlin plugin used in the IDE.

That is, in your top level build.gradle, find kotlin-gradle-plugin and change the version to the same version of IDE Kotlin plugin.

The IDE Kotlin plugin version can be found in File -> Settings -> Plugins -> Kotlin

enter image description here

answered Aug 5, 2021 at 4:04

Pai-Hsiang Huang's user avatar

I removed all cached filed from

HOME/.gradle/ -> cache/ and .tmp/

HOME/.android/ -> cache/

from project folder -> .gradle/

answered Aug 31, 2022 at 9:35

Davide's user avatar

DavideDavide

3,3411 gold badge20 silver badges21 bronze badges

1

If it happens to you after refactoring, and you’ve tried all the mentioned below (I will elaborate the list in a moment), try going to the class which has unresolved references and delete all its imports.
Then do the imports again, making sure you import the correct classes.

I tried the following which didn’t worked for me:

  • Invalidate Cache and restart Android Studio
  • Delete .build, app/build, and .idea
  • Sync Gradle files
  • Enable and Disable the Kotlin plugin.
  • Clean/Rebuild project
  • Restart my pc (MacOS).

answered Jan 1, 2020 at 9:15

Richard Miller's user avatar

Richard MillerRichard Miller

5661 gold badge7 silver badges17 bronze badges

1

What helped me was
Using AS 3.6.3
I go to build.gradle (module: app)
change the buildToolsVersion from «29.0.3» to «28.0.3»
sync my project and it worked

I think there was something wrong with the 29.0.3 version.

I uninstall the 29.0.3 version and install it again and use it.
29.0.3 version is working perfectly for me

answered May 3, 2020 at 18:53

petprog's user avatar

Read till the end 🙂

Surprisingly after so many answers, none of them worked for me.
I figured out the issue was with the android tools version and gradle distribution.

I upgraded classpath from

'com.android.tools.build:gradle:3.6.1'

to 'com.android.tools.build:gradle:4.0.1'

and also upgraded distributionUrl from

distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-all.zip

to distributionUrl=https://services.gradle.org/distributions/gradle-6.1.1-all.zip

So, if clean/build, invalidating cache, deleting .idea doesn’t work for you. Please try upgrading (or downgrading if your recent change caused this issue) these versions.

answered Aug 10, 2020 at 15:32

Rajkiran's user avatar

RajkiranRajkiran

15.6k24 gold badges74 silver badges113 bronze badges

I tried every other solution given here or anywhere else, what helped me
was changing the way my plugins were in the gradle file.

I changed it to this format:

plugins{
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'com.google.gms.google-services'
}

After this, invalidated caches and restarted, rebuilt the project and all was fine.
Amazing how this simple nothing results in a colossal error.

answered Mar 22, 2021 at 14:19

Luis's user avatar

LuisLuis

931 gold badge3 silver badges17 bronze badges

You can easily run into mysterious errors with Android Studio. Here is one.

Suddenly all references related to R.layout or R.id stopped to work and build failed with a message like this:

Compilation error. See log for more details
Unresolved reference: activity_main
Unresolved reference: url_edittext
Unresolved reference: update_button

The code looked like this:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

I tried clean, builds, rebuilds. Nothing helped. The problem was caused by simple import which accidentally occurred in Activity file:

import android.R

After removing this line the whole project was working again and even code hinting was correct.

Rating: 4.8/5. From 66 votes.

Please wait…

23. April 2019 at 22:21 — Development (Tags: Android, Android Studio, build, error, Kotlin).
Both comments and pings are currently closed.

I run the main branch of the official sample project well in Android Studio.

My Android Studio is 2020.3.1 Path 4, and Gradle version is displayed as Image 1.

Image 1
enter image description here

But I get the following error when I try to compile the end branch of the project.

E:Android_Studio_Sampleandroid-compose-codelabsNavigationCodelabappsrcmainjavacomexamplecomposerallyRallyActivity.kt: (31, 36): Unresolved reference: navArgument

How can I fix it?

BTW, I have read article.

I get the following error after I added implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0" to the end branch of the project

Your project has set android.useAndroidX=true, but configuration debugRuntimeClasspath still contains legacy support libraries, which may cause runtime issues.
This behavior will not be allowed in Android Gradle plugin 8.0.

And more, I still get the error «Unresolved reference: navArgument» when I added android.enableJetifier=true.

Added content

The project can run when I replace import androidx.navigation.compose.navArgument with import androidx.navigation.navArgument, could you tell me why?

I run the main branch of the official sample project well in Android Studio.

My Android Studio is 2020.3.1 Path 4, and Gradle version is displayed as Image 1.

Image 1
enter image description here

But I get the following error when I try to compile the end branch of the project.

E:Android_Studio_Sampleandroid-compose-codelabsNavigationCodelabappsrcmainjavacomexamplecomposerallyRallyActivity.kt: (31, 36): Unresolved reference: navArgument

How can I fix it?

BTW, I have read article.

I get the following error after I added implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0" to the end branch of the project

Your project has set android.useAndroidX=true, but configuration debugRuntimeClasspath still contains legacy support libraries, which may cause runtime issues.
This behavior will not be allowed in Android Gradle plugin 8.0.

And more, I still get the error «Unresolved reference: navArgument» when I added android.enableJetifier=true.

Added content

The project can run when I replace import androidx.navigation.compose.navArgument with import androidx.navigation.navArgument, could you tell me why?

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Unicum nero ошибка кофемолки
  • Unrecognized tag minecraft ошибка