Here is the link

https://developer.android.com/studio/projects/add-kotlin

in which it says that if you want to split Kotlin files and Java files, then you need to add this

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

I added and created the director

enter image description here

Added test class

 class TestDeleteIt { fun test(iC: Context) = Toast.makeText(iC, "TTTEEESSSTTT", Toast.LENGTH_LONG).show() } 

And now I want to use it from Java class which is located in Java Directorate.

I write new TestDeleteIt() and he does not see it

The studio does not see such a directory. I added import kotlin.TestDeleteIt; the studio says there is no such directory

What did you do wrong?

  • Before using a class from another package, the package must be imported - Jarvis_J
  • @Jarvis_J so the fact is that he does not see such a package, that's what I added import kotlin.TestDeleteIt; studio says there is no such package - Aleksey Timoshchenko
  • I have one explanation so far: maybe you didn’t add the necessary lines there, look - Jarvis_J
  • @Jarvis_S do you mean gredl strings? - Aleksey Timoshchenko pm
  • The package must be declared in the Kotlin file package mypkg; for example (in this case, the coincidence of the package structure with the directory structure is not necessary, but is recommended in mixed projects). The package should be because Java does not support importing classes from the default package. - zRrr pm

1 answer 1

Imports (especially in another language) are often not seen, because they have not been synchronized. If you use gradle, update all dependencies in it
refresh-gradle

if maven, then update dependencies in maven in the same way.

or, if it doesn't help, reimport the module (ctrl-alt-shift-s -> modules -> + -> import module, specify the build.gradle / *. pom file, and delete the old module) import-gradle-module

The package structure begins inside the java and kotlin directory.

If the structure is:

src

  • main
    • java
      • ru.stackoverflow.myapp
        • Main.java
    • kotlin
      • ru.stackoverflow.myapp
        • SomePackage.kt
      • ru.stackoverflow.mysuperapp
        • MyKotlin.kt

then in Main.java should be
import ru.stackoverflow.mysuperapp.MyKotlin;
and for the class SomePackage.kt, there is no need to register imports, since the package is the same