Kotlin unresolved reference springframework

How to fix unresolved reference issue in Kotlin and 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:

Or when you assign a variable without declaring them:

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:

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:

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:

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:

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:

Android Studio unresolved reference help action

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! 👍

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

VSCode Kotlin Spring error: unresolved reference

Reference : Java build tools in VS Code Question: I had Java code which looked like this: I converted it using Android Studio option ‘convert to Kotlin’ Here what is look like: The IDE now underlines the ‘add’ method here: It says: How can i fix this? UPDATE: Here is my transform function: Sample usage: Solution: Since you’r assigning a List<> to your valuesList it doesnt know add(). Change it to Better use Kotlins MutableList which is currently the same as Javas ArrayList but may be changed in later versions to a native method. First, I converted the Java code to Kotlin code by the automatic conversion feature of the Kotlin plugin for Android Studio.

VSCode Kotlin Spring error: unresolved reference

im trying to learn using Kotlin with Spring from here : https://spring.io/guides/tutorials/spring-boot-kotlin/

When im trying to run BlogApplication.kt it gives me this error :

[Running] cd "h:\Reshong\blog\src\main\kotlin\com\example\blog\" && kotlinc BlogApplication.kt -include-runtime -d BlogApplication.jar && java -jar BlogApplication.jar BlogApplication.kt:3:12: error: unresolved reference: springframework import org.springframework.boot.autoconfigure.SpringBootApplication ^ BlogApplication.kt:4:12: error: unresolved reference: springframework import org.springframework.boot.runApplication ^ BlogApplication.kt:6:2: error: unresolved reference: SpringBootApplication @SpringBootApplication ^ BlogApplication.kt:11:2: error: unresolved reference: runApplication runApplication(*args) ^ BlogApplication.kt:13:4: error: unresolved reference: setBannerMode setBannerMode(Banner.Mode.OFF) ^ BlogApplication.kt:13:18: error: unresolved reference: Banner setBannerMode(Banner.Mode.OFF) ^ [Done] exited with code=1 in 3.087 seconds 

I already tried a lot of googling and i dont find any result that helped me.

package com.example.blog import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @SpringBootApplication class BlogApplication fun main(args: Array) < runApplication(*args) < setBannerMode(Banner.Mode.OFF) >> 

Im still learning, so any help and advice is really appreciated.

Search springframework in Maven Repository, you can get the following page:

If there’s pom.xml in your project, copy the content shown below the option Maven to pom.xml ;

If it’s build.gradle.kts , copy the one in the option Gradle(Kotlin) to build.gradle.kts .

After adding all missing dependencies, rebuild the project and run again.

Reference : Java build tools in VS Code

How to fix «Unresolved reference » error in Kotlin Android, I’m new to kotlin and Andriod studio. I was following a tutorial and everything was going smoothly until I received a ton of «Unresolved reference errors» I looked at other peoples post concerning this problem but nothing seemed to help as I still receive multiple errors. I tried: Build -> Clean -> Build -> Rebuild. …

Kotlin unresolved reference :add after converting from Java code

I had Java code which looked like this:

 public static Map> createMultimap(@NonNull Collection values, @NonNull TransformFunction transformFunction) < Map> tmpMap = new HashMap<>(); for (VALUE value : values) < if (value != null) < KEY key = transformFunction.transform(value); ListvaluesList = tmpMap.get(key); if (valuesList == null) < valuesList = new ArrayList<>(); tmpMap.put(key, valuesList); > valuesList.add(value); > > return Collections.unmodifiableMap(tmpMap); > 

I converted it using Android Studio option ‘convert to Kotlin’

 fun createMultimap(values: Collection, transformFunction: TransformFunction): Map < val tmpMap = HashMap() for (value in values) < if (value != null) < val key = transformFunction.transform(value) var valuesList: List? = tmpMapKotlin unresolved reference springframework if (valuesList == null) < valuesList = ArrayList() tmpMap.put(key, valuesList) >valuesList.add(value) > > return Collections.unmodifiableMap(tmpMap) > 

The IDE now underlines the ‘add’ method here:

Here is my transform function:

interface TransformFunction

 private fun getRSSIMultimap(rssiEvents: Collection): Map < return CollectionsUtils.createMultimap(rssiEvents, object : CollectionsUtils.TransformFunction < override fun transform(locationRSSIEvent: LocationRSSIEvent): NexoIdentifier < return locationRSSIEvent.nexoIdentifier >>) > 

Since you’r assigning a List<> to your valuesList it doesnt know add().

 fun createMultimap(values: Collection, transformFunction: TransformFunction): Map < var valuesList: ArrayList? = tmpMapKotlin unresolved reference springframework 

Better use Kotlins MutableList which is currently the same as Javas ArrayList but may be changed in later versions to a native method.

 fun createMultimap(values: Collection, transformFunction: TransformFunction): Map < val tmpMap = HashMap() for (value in values) < if (value != null) < val key = transformFunction.transform(value) var valuesList: MutableList? = tmpMapKotlin unresolved reference springframework if (valuesList == null) < valuesList = mutableListOf() tmpMap.put(key, valuesList) >valuesList.add(value) > > return Collections.unmodifiableMap(tmpMap) > 

Kotlin - unresolved reference for constant

I'm trying to use the sample code of the new camera hardware API (android.hardware.camera2) for my android app. First, I converted the java code to kotlin code by the automatic conversion feature of the Kotlin plugin for Android Studio. Below is a piece of the converted code:

private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() < private fun process(result: CaptureResult) < when (mState) < STATE_PREVIEW ->< >// We have nothing to do when the camera preview is working normally. STATE_WAITING_LOCK -> < val afState = result.get(CaptureResult.CONTROL_AF_STATE) if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) < // CONTROL_AE_STATE can be null on some devices val aeState = result.get(CaptureResult.CONTROL_AE_STATE) if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) < mState = STATE_WAITING_NON_PRECAPTURE captureStillPicture() >else < runPrecaptureSequence() >> > STATE_WAITING_PRECAPTURE -> < // CONTROL_AE_STATE can be null on some devices val aeState = result.get(CaptureResult.CONTROL_AE_STATE) if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE || aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) < mState = STATE_WAITING_NON_PRECAPTURE >> STATE_WAITING_NON_PRECAPTURE -> < // CONTROL_AE_STATE can be null on some devices val aeState = result.get(CaptureResult.CONTROL_AE_STATE) if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) < mState = STATE_PICTURE_TAKEN captureStillPicture() >> > > 

I'm getting the unresolved reference error for:

CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED CaptureResult.CONTROL_AE_STATE_CONVERGED CaptureResult.CONTROL_AE_STATE_PRECAPTURE CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED 

But these constants are recognized in the original Java code. There's any solution for these errors?

These variables are declared in CameraMetadata. In kotlin static variables and methods cannot be called on subclasses.

Kotlin unresolved reference get Code Example, Unresolved reference: module koin kotlin. Unresolved reference: S android. Unresolved reference: string kotlin. Unresolved reference: v7 kotlin. unresolvede reference in kotlin. Unresolved reference: int kotlin. unresolved reference kotlin test. unresolved reference kotline. unresolved reference …

Unresolved reference: of kotlin

you maybe deleted this package com.example.yourappname

Unresolved reference: io ktor kotlin Code Example, unresolved reference launch kotlin. kotlin by Blue-eyed Batfish on Mar 24 2021 Comment. 0. xxxxxxxxxx. 1. fun main() <. 2. GlobalScope.launch < // launch a new coroutine in background and continue. 3.

Источник

Kotlin compiler returning: Unresolved reference: springframework in Spring 5.0

I'm trying to start playing around with using Kotlin together with Spring 5.0 however I am having issues with the Kotlin compiler not recognising any reference to Spring:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.1.1:compile (compile) on project kotlin-mvc-project: Compilation failure: Compilation failure: [ERROR] (file location):[7,12] Unresolved reference: springframework

I'm using the spring milestone version Spring 5.0.0.M5 and Kotlin version 1.1.1 (on both my kotlin-compiler and IntelliJ Kotlin plugin). There are no compile errors highlighted by the IDE in any of my Kotlin files but running the kotlin-compiler it seems to not see Spring 5.0 at all.

Does anyone have any ideas how to fix this? I'm using Maven for this project, I've attached my POM for reference:

  4.0.0 kotlin-mvc-project kotlin-mvc-project 1.0-SNAPSHOT 1.1.1 5.0.0.M5   spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false     org.jetbrains.kotlin kotlin-stdlib $ org.jetbrains.kotlin kotlin-compiler $ org.springframework spring-core $ org.springframework spring-web $ org.springframework spring-webmvc $ javax.servlet javax.servlet-api 3.1.0 provided   $/src/main/kotlin $/src/test/kotlin  kotlin-maven-plugin org.jetbrains.kotlin $  compile compile   test-compile test-compile        

Plog Avatar

asked Mar 21 '17 18:03

Plog

1 Answers

After help from @DmitrySenkovich in narrowing down that it was not an issue with my pom I investigated what could be going wrong in my local maven repository. I checked out the jars in my .m2 folder and they seemed a bit wrong and missing certain things. I tried wiping the maven repo but they kept being pulled back the same.

This issue was on a corporate machine and after doing some investigation it seems our maven settings.xml were configured to pull down dependencies from a company wide Artifactory and not maven central/any other repo.

Dependencies are supposed to be pulled down into this Arifactory from other repositories as required but for whatever reason (I assume because they were being pulled down from a repo other than maven central) the jars for the milestone Spring builds were not pulling down correctly. By removing these and manually adding the jars to Artifactory I was able to finally resolve the issue.

Plog Avatar

answered Sep 19 '22 02:09

Источник

Kotlin compiler returning: Unresolved reference: springframework in Spring 5.0

I'm trying to start playing around with using Kotlin together with Spring 5.0 however I am having issues with the Kotlin compiler not recognising any reference to Spring:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.1.1:compile (compile) on project kotlin-mvc-project: Compilation failure: Compilation failure: [ERROR] (file location):[7,12] Unresolved reference: springframework

I'm using the spring milestone version Spring 5.0.0.M5 and Kotlin version 1.1.1 (on both my kotlin-compiler and IntelliJ Kotlin plugin). There are no compile errors highlighted by the IDE in any of my Kotlin files but running the kotlin-compiler it seems to not see Spring 5.0 at all. Does anyone have any ideas how to fix this? I'm using Maven for this project, I've attached my POM for reference:

  4.0.0 kotlin-mvc-project kotlin-mvc-project 1.0-SNAPSHOT 1.1.1 5.0.0.M5   spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false     org.jetbrains.kotlin kotlin-stdlib $ org.jetbrains.kotlin kotlin-compiler $ org.springframework spring-core $ org.springframework spring-web $ org.springframework spring-webmvc $ javax.servlet javax.servlet-api 3.1.0 provided   $/src/main/kotlin $/src/test/kotlin  kotlin-maven-plugin org.jetbrains.kotlin $  compile compile   test-compile test-compile        

Источник

Читайте также:  Jquery add html element to div
Оцените статью