Hi, folks! Can I automatically opt-in to my Kotlin...
# community-support
j
Hi, folks! Can I automatically opt-in to my Kotlin features when applying my custom Gradle plugin? I have a custom annotation:
Copy code
@RequiresOptIn(message = "This API belongs to ${Plugins.RUN}")
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class IntelliJPlatformRun
And a task:
Copy code
@IntelliJPlatformRun
abstract class RunIdeTask : JavaExec() {}
Which is registered within my plugin:
Copy code
@OptIn(IntelliJPlatformRun::class)
abstract class IntelliJPlatformPlugin : Plugin<Project> {
    override fun apply(project: Project) {
      // register RunIdeTask
    }
}
In the
build.gradle.kts
I apply this plugin with:
Copy code
plugins {
  id("org.jetbrains.intellij.platform") version "..."
}
and the
runIde
task is present, but when trying to reconfigure it with:
Copy code
tasks {
    runIde {
        splitMode = false
    }
}
I get
[OPT_IN_USAGE_ERROR] This API belongs to org. jetbrains. intellij. platform. run
unless I explicitly
@OptIn
again in the client’s code. I’m looking for a contract to let me use such classes without effort when I apply the plugin that already has relevant opt-ins.
1
v
This question might be better suited as the Kotlin Slack or your Kotlin colleagues. But I guess there is not. As far as I understand that Kotlin feature, you marked your plugin with
@OptIn
, so when anything in the plugin is used, the opt-in requirement does not propagate, except if the consumer uses an API that has a class that requires opt-in in its signature. But with the last snippet you showed, you directly use an opt-in requiring type / method and so need to explicitly opt-in in that code. It might be not a proper use-case for opt-in requiring if you want the consumer to use the type. If for whatever reason you want to prevent others to add new tasks of that type, you probably need to instead make the constructor
internal
and instantiate the task class manually, but then you also loose the decorations Gradle would add.
j
Using opt-ins here was an attempt for modularizing the project without using actual modules. Well, that wasn’t the greatest idea anyway. Dropped it. 🙂
👌 1
And folks from the Kotlin team just confirmed you cannot automatically opt-in for features within the Gradle build script.