Renaming "java" directory to "kotlin" in Android Studio

JavaAndroidKotlinAndroid Gradle-Plugin

Java Problem Overview


My android project is written 100% in Kotlin and I wanted to rename the java directory to kotlin to be consistent. I added

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

to gradle and renamed the directory. All compiles and works fine.

The only issue is that when I am in the project tab, in "Android" view the directory is still named "java" (if I switch to the project view in the dropdown, I do see "kotlin" tho).

What am I missing?

Java Solutions


Solution 1 - Java

Update (2)

If you are using

Android Studio Arctic Fox version 2020.3.1 Beta 2

and

Gradle Plugin version 7.0.0-beta02

you are good to go and can just put put your code in src/main/kotlin. Android Studio shows it as kotlin in the file tree. No need for adding the directory to the sourceSet as shown below.

Android Studio Arctic Fox file tree


Update (1)

Kotlin is finally coming to AndroidSourceSet. If you use the latest Android Gradle Plugin version 7.0 alpha, you can use:

android.sourceSets.all {
    kotlin.srcDir("src/$name/kotlin")
}

This works fine in Android Studio 3.6.2 and should be the most versatile solution until AndroidSourceSet starts supporting Kotlin directly. Just add the following snippet at the end of your app/build.gradle[.kts]:

android.sourceSets.all {
    java.srcDir("src/$name/kotlin")
}

Solution 2 - Java

Android Studio 3.6 has the same behavior. This is how I fixed it, in app/build.gradle:

// Register kotlin files
android.sourceSets {
     androidTest.java.srcDirs += "src/androidTest/kotlin"
     debug.java.srcDirs += "src/debug/kotlin"
     main.java.srcDirs += "src/main/kotlin"
     test.java.srcDirs += "src/test/kotlin"
}

Solution 3 - Java

I think this feature Android Studio. By default, the folder with the source code it is marked as "java". Even if you are in the "src/main" folder contains "kotlin" and "java".

Solution 4 - Java

On Linux

In your modules gradle file ex: app.gradle

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

Solution 5 - Java

even if it is supported in android studio, kotlin is still a plugin and v3.4 has the same behavior.

the solution:

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

in settings.gradle or local.properties works for me (windows)

Solution 6 - Java

Update:

After refactor renaming java to kotlin, syntax highlighting got broken, cannot CTRL + B to resources in source files.

So I opened an issue in the issue tracker even mentioned the android view issue after renaming.

So for now I will keep the source directory name as it is.


Actually you just have to rename the java directory to kotlin, by right clicking on the java directory in Project view and select

Refactor > Rename

and type the new directory name as kotlin

No need to add any code to gradle file.

(Works on android studio 3.5, Linux Mint)

After refactoring on Android view, If it is a Android app module or android library module your source directories (java/kotlin) would not show up, but in regular library modules it will show up as java. In Project view all things look as expected.

Solution 7 - Java

Simpler Solution Go into Windows Explorer find your src directory Inside you will find

androidTest/java
main/java
test/java

Rename those java directories to

androidTest/kotlin
main/kotlin
test/kotlin

Testing

Optionally remove the .idea directory

Re-load project from disk

Sync project with Gradle files

Rebuild Project and run Project

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWheel BuilderView Question on Stackoverflow
Solution 1 - JavakroegeramaView Answer on Stackoverflow
Solution 2 - JavaPer Christian HendenView Answer on Stackoverflow
Solution 3 - JavaIlyas ShafiginView Answer on Stackoverflow
Solution 4 - JavaIaroslav TernovyiView Answer on Stackoverflow
Solution 5 - JavaFrankView Answer on Stackoverflow
Solution 6 - Javauser158View Answer on Stackoverflow
Solution 7 - JavaCryptoCodeView Answer on Stackoverflow