Kotlin files not able to use R.java

I created a new layout file in the appropriate resource folder. After syncing, when I try to reference the layout file, i.e. R.layout.activity_test, R is an "unresolved symbol". When I try to build in AS it fails.

The interesting thing is that if I manually import the R file and use it in code, when I try to build on command line, it works. The other thing is when I try to access the layout file using R in a Java file, that also works. So I know R.java is being generated.

I tried creating a totally new project, and R is accessible in Kotlin files.

What is the problem here?

Things I have tried:

  • Cleaned then built

  • Invalidated Caches / Restart

  • Blow away .gradle folders and recreate indexes

Other info:

  • AS v3.0.1

  • Tried both Kotlin v1.1.2-4 and v1.1.60

  • Gradle v4.2.1

  • Gradle Plugin v3.0.0

Invalidate Caches/Restart worked for me.

Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/me.eugeniomarletti.kotlin.metadata/kotlin-compiler-lite/1.0.3-k-1.2.40/a16e967c276379eaf6e1fa03af77664f003db947/kotlin-compiler-lite-1.0.3-k-1.2.40.jar (version 1.2)
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.20/eb2a232734e09fcd1b958a5c7520a93c6de38b32/kotlin-stdlib-1.3.20.jar (version 1.3)
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.20/7d7934e26ce34da1a0a8d00e38038d7cf3375e89/kotlin-stdlib-common-1.3.20.jar (version 1.3)

android - Kotlin files not able to use R.java, Add the following under android section in your application module: android { sourceSets { main.java.srcDirs += 'src/main/kotlin' } }. This lets Android Studio  The interesting thing is that if I manually import the R file and use it in code, when I try to build on command line, it works. The other thing is when I try to access the layout file using R in a Java file, that also works. So I know R.java is being generated. I tried creating a totally new project, and R is accessible in Kotlin files.

Add the following under android section in your application module:

android {
  ...
  sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
  }
}

This lets Android Studio know that the kotlin directory is a source root, so when the project model is loaded into the IDE it will be properly recognized. Alternatively, you can put Kotlin classes in the Java source directory, typically located in src/main/java.


https://kotlinlang.org/docs/reference/using-gradle.html#android-studio

R is unresolved - Android, This error appear in both files in onCreate() method This error mean you unsuccessfully build resource , please check all your resource have something Error, so R class static not created Have had the same issue too, had to use a stable gradle plugin for R to be This change made the fix possible. Converting a Kotlin file to Java file involves two steps i.e. compiling the Kotlin code to the JVM bytecode and then decompile the bytecode to the Java code. Steps to convert your Kotlin source file to Java source file: Open your Kotlin project in the Android Studio. Then navigate to Tools -> Kotlin -> Show Kotlin Bytecode.

If you have a warning in your project please fix it. in my case my Gradle version had a version Compile warning. the App runs fine but the R class was not recognized, so I change the gradle version and fix the warnings and all works well.

Kotlin files not able to use R.java, Kotlin files not able to use R.java - android. The interesting thing is that if I manually import the R file and use it in code, when I try to build on command line,​  Example – Kotlin Create File – File.createNewFile() File.createNewFile() creates a new file if it does not exist already and returns Boolean value of true. If the file does exist already at the path provided, the method returns false. The file created is empty and has zero bytes written to it. Using File.createNewFile() is the best and safest

Android Projects error on R.java · Issue #7 · fwcd/kotlin-language , all of it's layout files into a R.java file that is compiled into the resulting apk file. I've not delved into why yet - I'm assuming that this language server doesn't allow Changing the classpath is possible and would require tweaking of I create a simple HelloKotlin project directly using AndroidStudio that  Therefore, you have to import an extension function/property wherever you want to use it (since it isn't carried along with the instances of the class). For the same reason, you can not override extension members - you can reimplement them for subtypes, but the resolution happens at compile-time based on the static type of the expression you're invoking it on.

How to Rectify R Package Error in Android Studio, How To Resolve the Package R Does Not Exist Error? errors about the R.class, like package R dost not exist or R cannot be still does not take effect, you should check your AndroidManifest.xml file The simplest way to get data is to use provided by java api also know Waiting for Coroutines in Kotlin. Package kotlin.io. IO API for working with files and streams. Types. JVM. 1.0. FileTreeWalk. This class is intended to implement different file traversal methods. It

What is the use of R.java in Android Studio, and how do you use it , R.java is an automatically generated class which stores information about resources It basically makes the connection between the XML files and Java. All resources located in res folder are mostly .xml files, which will not be understood by java compiler. Is it possible to develop Android apps without Android Studio? You can use Kotlin along with the Java programming language in your applications without needing to migrate all your code to Kotlin. Support for multiplatform development. You can use Kotlin for developing not only Android but also iOS, backend, and web applications. Enjoy the benefits of sharing the common code among the platforms.

Comments
  • I have the same problem, did you find any solution?