Renaming “java” directory to “kotlin” in Android S

2020-05-18 04:40发布

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?

7条回答
▲ chillily
2楼-- · 2020-05-18 05:01

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"
}
查看更多
看我几分像从前
3楼-- · 2020-05-18 05:04

I have the same. It happens only in "Android" view. When I navigate to that folder in Finder it's named 'kotlin'. Looks like it's a bug in the Android studio. enter image description here

查看更多
该账号已被封号
4楼-- · 2020-05-18 05:04

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)

查看更多
SAY GOODBYE
5楼-- · 2020-05-18 05:12

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".

查看更多
Deceive 欺骗
6楼-- · 2020-05-18 05:23

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")
}
查看更多
ら.Afraid
7楼-- · 2020-05-18 05:24

On Linux

In your modules gradle file ex: app.gradle

sourceSets{ 
    main.java.srcDirs += 'src/main/kotlin/' 
}
查看更多
登录 后发表回答