3

In the official Kotlin coding conventions (https://kotlinlang.org/docs/reference/coding-conventions.html) they recommend to us to omit root package in directory structure. It is not comfortable, when you create a new file in your kotlin source folder and then edit the package name, because it did not generated automatically.

Alexey Nikitin
  • 604
  • 7
  • 22
  • Recent versions of the Kotlin plugin (starting from 1.2.20 I think) do generate the package name when you create new Kotlin files in the root folder, if all other files in that folder have the same package declaration. – yole Jul 04 '18 at 08:23

3 Answers3

4

From a Reddit comment:

I guess you're supposed to use the package prefix enter image description here in the project structure, but sadly that feature is only available in IDEA and not Android Studio.

I agree it's weird that it's not documented more explicitly, I've only found https://www.jetbrains.com/help/idea/creating-and-managing-modules.html. You may want to consider raising the issue on http://discuss.kotlinlang.org/.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Thanks for information about package prefix! But is there some explanation why do we need omit the root package directory? Why is it good for project source code? I heard about one advantage: refactoring root package does not move files from one directory to another - but I dont think that it is advantage – Alexey Nikitin Jul 04 '18 at 07:47
  • I don't think there is any "need", it's just to get a simpler and easier-to-navigate directory structure with no disadvantages once you set package prefix (unless you use tools assuming complete correspondence between directory and package structure and which can't be taught this difference). – Alexey Romanov Jul 04 '18 at 07:59
  • @AlexeyRomanov But how can I set the package prefix by using build tools (e.g. Gradle)? Because once I refreshed my gradle project, the package prefix got reset. – Sola Nov 25 '18 at 11:22
  • @Sola I don't know. You may want to make it a separate question here or at https://discuss.kotlinlang.org/. – Alexey Romanov Nov 25 '18 at 12:42
1

Looks like support for packagePrefix was added to idea-ext gradle plugin in version 0.5 : https://github.com/JetBrains/gradle-idea-ext-plugin/wiki/DSL-spec-v.-0.5#package-prefix-requires-idea-20191

alshan
  • 136
  • 3
-3

I think this rule is inherited from Java code style where using of default package is a bad practice.

You can check this topic to obtain more information about default package in Java.

Ivan Samborskii
  • 240
  • 1
  • 4
  • Yes, defualt package is a bad practice, but in case of Koltin coding conventions, they did not recommend use defualt package, they recommend omit root package in directory structure and define package directive in your source code files like root package is exists – Alexey Nikitin Jul 04 '18 at 06:47
  • 2
    Yes, this has absolutely nothing to do with default package. – Alexey Romanov Jul 04 '18 at 07:13