Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 475
Wheel Builder Avatar asked Jan 04 '17 15:01

Wheel Builder


People also ask

Can we convert Android Java to Kotlin?

To convert Java code to Kotlin, open the Java file in Android Studio, and select Code > Convert Java File to Kotlin File. Alternatively, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file.

Can you use Kotlin and Java in the same project Android Studio?

Adding Java source code to an existing Kotlin projectIf you already have the Java classes, you can just copy them to the project directories. You can now consume the Java class from Kotlin or vice versa without any further actions. lets you call it from Kotlin like any other type in Kotlin.

Can Java be converted to Kotlin?

You can use both Android Studio and IntelliJ IDE software to convert Java code to Kotlin. If you are using a Kotlin based project in Android Studio or IntelliJ IDE software and try to copy/paste Java code, you should get a prompt to automatically convert Java code to Kotlin (as shown in the screenshot below).


1 Answers

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") } 
like image 127
kroegerama Avatar answered Sep 20 '22 06:09

kroegerama