@@ -88,6 +88,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
8888
8989 var languageVersion: String? = null
9090
91+ /* * Use the new experimental K2 compiler */
92+ var useK2: Boolean by default { false }
93+
9194 /* * Additional string arguments to the Kotlin compiler */
9295 var kotlincArguments: List <String > = emptyList()
9396
@@ -126,6 +129,7 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
126129 args.allWarningsAsErrors = allWarningsAsErrors
127130 args.reportOutputFiles = reportOutputFiles
128131 args.reportPerf = reportPerformance
132+ args.useK2 = useK2
129133
130134 if (languageVersion != null )
131135 args.languageVersion = this .languageVersion
@@ -191,7 +195,16 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
191195 } else {
192196 emptyList()
193197 }
194- args.pluginClasspaths = (args.pluginClasspaths ? : emptyArray()) + arrayOf(getResourcesPath())
198+ args.pluginClasspaths = (args.pluginClasspaths ? : emptyArray()) +
199+ /* * The resources path contains the MainComponentRegistrar and MainCommandLineProcessor which will
200+ be found by the Kotlin compiler's service loader. We add it only when the user has actually given
201+ us ComponentRegistrar instances to be loaded by the MainComponentRegistrar because the experimental
202+ K2 compiler doesn't support plugins yet. This way, users of K2 can prevent MainComponentRegistrar
203+ from being loaded and crashing K2 by setting both [compilerPlugins] and [commandLineProcessors] to
204+ the emptyList. */
205+ if (compilerPlugins.isNotEmpty() || commandLineProcessors.isNotEmpty())
206+ arrayOf(getResourcesPath())
207+ else emptyArray()
195208 }
196209
197210 val compilerMessageCollector = PrintingMessageCollector (
0 commit comments