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

后端 未结 7 909
挽巷
挽巷 2021-01-31 01: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 +=         


        
相关标签:
7条回答
  • 2021-01-31 02:16

    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.

    0 讨论(0)
  • 2021-01-31 02:16

    On Linux

    In your modules gradle file ex: app.gradle

    sourceSets{ 
        main.java.srcDirs += 'src/main/kotlin/' 
    }
    
    0 讨论(0)
  • 2021-01-31 02:22

    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.

    0 讨论(0)
  • 2021-01-31 02:22

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

    0 讨论(0)
  • 2021-01-31 02:25

    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"
    }
    
    0 讨论(0)
  • 2021-01-31 02:40

    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)

    0 讨论(0)
提交回复
热议问题