Specifying external annotations for the compiler

It seems like there used to be a way to produce compile time nullability errors using external annotations, as described in the following pages:

This was a few years ago. Since then, it seems like the -annotations compiler option was removed, same with Gradle kotlinOptions.annotations option. What happened? Is this due to the introduction of platform types (types with !)? Or because the compiler now recognizes direct annotations (e.g.: @org.jetbrains.annotations.NotNull)?

I think it would still make sense to produce compile time errors for externally annotated types. For example, some of the Java API appears as externally annotated in IntelliJ, like the File class. The constructor File(@NotNull String pathname) (where @NotNull is added externally) compiles without error when you do File(null as String?). But it would throw a compile-time error if the type was directly annotated.

Why is there no option to do the same with external annotations? Or did I miss something?

1 Like

External annotations are still there.
You can navigate to Java library source code and will find a hot-fix to add an external annotation.
You will be asked where to store the external annotations.

Unfortunately, this location is not stored in build.gradle.kts or even in the project files, but in a local, project-specific libraries.xml file (on Windows in USER\AppData\Local\JetBrains\IntelliJIdea...\external_build_system). Bad for sharing, bad for version control and likely to break on gradle updates. You can see it and change it in the project structure editor under libraries.

There is also a external annotations box under modules. This path is used for external annotations in your own java code (you have to enable external annotations in your settings).

I have not figured out how to add external annotations to pure Kotlin libraries.
I need it to add @NonNls annotations.

1 Like