Add Android project level compiler options DSL

Works the same as JVM project compiler options DSL. 'android
.kotlinOptions' now configures compilerOptions from extension basically
behaving same way as newly added DSL.

^KT-57159 Fixed
This commit is contained in:
Yahor Berdnikau
2023-03-10 21:11:08 +01:00
committed by Space Team
parent edc6194737
commit c17668a225
48 changed files with 100 additions and 760 deletions
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
import kotlin.io.path.appendText
@DisplayName("Project level compiler options DSL")
class CompilerOptionsProjectIT : KGPBaseTest() {
@DisplayName("Jvm project compiler options are passed to compilation")
@@ -213,4 +214,43 @@ class CompilerOptionsProjectIT : KGPBaseTest() {
}
}
}
@DisplayName("Project level DSL is available in android project")
@AndroidGradlePluginTests
@GradleAndroidTest
fun androidProject(
gradleVersion: GradleVersion,
agpVersion: String,
jdk: JdkVersions.ProvidedJdk
) {
project(
"AndroidSimpleApp",
gradleVersion,
buildJdk = jdk.location,
buildOptions = defaultBuildOptions.copy(androidVersion = agpVersion, logLevel = LogLevel.DEBUG)
) {
buildGradle.appendText(
//language=Groovy
"""
|
|kotlin {
| compilerOptions {
| javaParameters = true
| }
|}
""".trimMargin()
)
build("compileDebugKotlin") {
assertTasksExecuted(":compileDebugKotlin")
val compilationArgs = output.lineSequence().first { it.contains("Kotlin compiler args:") }
assert(compilationArgs.contains("-java-parameters")) {
printBuildOutput()
"Compiler arguments does not contain '-java-parameters': $compilationArgs"
}
}
}
}
}
@@ -1,51 +0,0 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
dependencies {
implementation project(':Lib')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation'junit:junit:4.13.2'
}
android {
compileSdkVersion 22
sourceSets {
main.java.srcDirs += 'src/main/java2'
main.kotlin.srcDirs += 'root/kotlin'
test.kotlin.srcDirs += 'src/test/kotlin'
}
defaultConfig {
minSdkVersion 7
targetSdkVersion 22
jackOptions { enabled true }
}
buildTypes {
jnidebug.initWith(buildTypes.debug)
jnidebug {
applicationIdSuffix ".jnidebug"
jniDebuggable true
}
}
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
publishNonDefault true
kotlinOptions {
noJdk = true
}
}
@@ -1,59 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.test.androidalfa
import android.content.Intent
import android.os.Bundle
import android.app.Activity
import android.view.Menu
import android.view.View
import android.widget.Button
import org.jetbrains.kotlin.gradle.test.androidalfa.R
import lib.*
open class MainActivity2: Activity() {
protected override fun onCreate(savedInstanceState: Bundle?): Unit {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
libUtil()
var next: Button = findViewById(R.id.Button02) as Button
next.setOnClickListener(object: View.OnClickListener {
public override fun onClick(view: View): Unit {
val intent: Intent = Intent()
setResult(Activity.RESULT_OK, intent)
finish()
}
})
}
public override fun onCreateOptionsMenu(menu: Menu?): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true
}
}
fun foo() {
bar()
}
fun bar() {
}
@@ -1,30 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle
import android.app.Application
import android.test.ApplicationTestCase
class InternalDummyApplicationTest : ApplicationTestCase<Application>(Application::class.java) {
init {
val dummy = InternalDummy("World")
assert("Hello World!" == dummy.greeting) { "Expected: 'Hello World!'. Actual value: ${dummy.greeting}" }
// Check that the Java sources from the tested variant are available
val bar = foo.FooJavaClass()
}
}
@@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.jetbrains.kotlin.gradle.test.androidalfa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.jetbrains.kotlin.gradle.test.androidalfa.MainActivity"
android:label="@string/title_activity_main_activity1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.jetbrains.kotlin.gradle.test.androidalfa.MainActivity2"
android:label="@string/title_activity_main_activity2" >
</activity>
</application>
</manifest>
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;
import bar.*;
public class FooJavaClass {
void f() {
new FooKotlinClass();
new BarJavaClass();
new BarKotlinClass();
}
}
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo
import bar.*
class FooKotlinClass {
fun f() {
FooJavaClass()
BarJavaClass()
BarKotlinClass()
}
}
@@ -1,22 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle
internal class InternalDummy(private val name: String) {
internal val greeting: String
get() = "Hello $name!"
}
@@ -1,52 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.test.androidalfa;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import org.jetbrains.kotlin.gradle.test.androidalfa.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MainActivity2.class);
startActivityForResult(myIntent, 0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bar;
import foo.*;
public class BarJavaClass {
void f() {
new BarKotlinClass();
new FooJavaClass();
new FooKotlinClass();
}
}
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bar
import foo.*
class BarKotlinClass {
fun f() {
BarJavaClass()
FooJavaClass()
FooKotlinClass()
}
}
@@ -1,24 +0,0 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Activity 1" />
<Button android:text="Next"
android:id="@+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
@@ -1,24 +0,0 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Activity 2" />
<Button android:text="Next"
android:id="@+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
@@ -1,6 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
@@ -1,6 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
@@ -1,5 +0,0 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidAlfa</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_main_activity1">MainActivity1</string>
<string name="title_activity_main_activity2">MainActivity2</string>
</resources>
@@ -1,20 +0,0 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
@@ -1,28 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle
import org.junit.Assert
import org.junit.Test
class InternalDummyTest {
@Test
fun testInternalDummy() {
val dummy = InternalDummy("World")
Assert.assertEquals("Hello World!", dummy.greeting)
}
}
@@ -1,18 +0,0 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
dependencies {
implementation files('libs/android-support-v4.jar')
// unused but needed for IncrementalCompilationMultiProjectIT.testAndroid to check if non-local dependency affects IC
implementation 'io.reactivex:rxjava:1.1.9'
implementation 'com.loopj.android:android-async-http:1.4.9'
}
android {
compileSdkVersion 22
defaultConfig {
minSdkVersion 7
targetSdkVersion 22
}
}
@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.jetbrains.kotlin.gradle.test.android.libalfa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.jetbrains.kotlin.gradle.test.android.libalfa.LibMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.test.android.libalfa;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class LibMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lib_activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lib_main, menu);
return true;
}
}
@@ -1,19 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lib
fun libUtil(): String = "libUtil"
@@ -1,16 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".LibMainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
@@ -1,6 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
@@ -1,4 +0,0 @@
<resources>
<!-- Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here. -->
</resources>
@@ -1,5 +0,0 @@
<resources>
<!-- Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. -->
<dimen name="activity_horizontal_margin">128dp</dimen>
</resources>
@@ -1,11 +0,0 @@
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
@@ -1,12 +0,0 @@
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
@@ -1,5 +0,0 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LibAlfa</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
@@ -1,20 +0,0 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
@@ -1,18 +0,0 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.android.tools.build:gradle:$android_tools_version"
}
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}
@@ -40,11 +40,6 @@ android {
}
publishNonDefault true
kotlinOptions {
noJdk = true
}
lintOptions.abortOnError = false
}
@@ -5,6 +5,7 @@ plugins {
repositories {
mavenLocal()
google()
mavenCentral()
}
@@ -406,6 +406,17 @@ abstract class KotlinAndroidProjectExtension(project: Project) : KotlinSingleTar
internal set
open fun target(body: KotlinAndroidTarget.() -> Unit) = target.run(body)
val compilerOptions: KotlinJvmCompilerOptions =
project.objects.newInstance(KotlinJvmCompilerOptionsDefault::class.java)
fun compilerOptions(configure: Action<KotlinJvmCompilerOptions>) {
configure.execute(compilerOptions)
}
fun compilerOptions(configure: KotlinJvmCompilerOptions.() -> Unit) {
configure(compilerOptions)
}
}
enum class NativeCacheKind(val produce: String?, val outputKind: CompilerOutputKind?) {
@@ -5,14 +5,18 @@
package org.jetbrains.kotlin.gradle.plugin
import com.android.build.gradle.BaseExtension
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.internal.customizeKotlinDependencies
import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
import org.jetbrains.kotlin.gradle.plugin.KotlinJvmPlugin.Companion.configureCompilerOptionsForTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.applyUserDefinedAttributes
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
@@ -32,8 +36,21 @@ internal open class KotlinAndroidPlugin(
KotlinAndroidTarget::class.java,
"",
project
).also {
(project.kotlinExtension as KotlinAndroidProjectExtension).target = it
).also { target ->
val kotlinAndroidExtension = project.kotlinExtension as KotlinAndroidProjectExtension
kotlinAndroidExtension.target = target
project.configureCompilerOptionsForTarget(
kotlinAndroidExtension.compilerOptions,
target.compilations
)
kotlinAndroidExtension.compilerOptions.noJdk.value(true).disallowChanges()
@Suppress("DEPRECATION") val kotlinOptions = object : KotlinJvmOptions {
override val options: KotlinJvmCompilerOptions
get() = kotlinAndroidExtension.compilerOptions
}
val ext = project.extensions.getByName("android") as BaseExtension
ext.addExtension(KOTLIN_OPTIONS_DSL_NAME, kotlinOptions)
}
}
) { androidTarget ->
@@ -5,36 +5,51 @@
package org.jetbrains.kotlin.gradle.plugin
import com.android.build.gradle.*
import org.gradle.api.*
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
import org.jetbrains.kotlin.gradle.targets.js.ir.*
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.configuration.*
import org.jetbrains.kotlin.gradle.utils.*
const val PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinCompilerPluginClasspath"
const val NATIVE_COMPILER_PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinNativeCompilerPluginClasspath"
internal const val COMPILER_CLASSPATH_CONFIGURATION_NAME = "kotlinCompilerClasspath"
internal const val KLIB_COMMONIZER_CLASSPATH_CONFIGURATION_NAME = "kotlinKlibCommonizerClasspath"
val KOTLIN_DSL_NAME = "kotlin"
const val KOTLIN_DSL_NAME = "kotlin"
@Deprecated("Should be removed with 'platform.js' plugin removal")
val KOTLIN_JS_DSL_NAME = "kotlin2js"
val KOTLIN_OPTIONS_DSL_NAME = "kotlinOptions"
const val KOTLIN_JS_DSL_NAME = "kotlin2js"
const val KOTLIN_OPTIONS_DSL_NAME = "kotlinOptions"
internal open class KotlinJvmPlugin(
registry: ToolingModelBuilderRegistry
) : AbstractKotlinPlugin(KotlinTasksProvider(), registry) {
companion object {
internal companion object {
private const val targetName = "" // use empty suffix for the task names
internal fun Project.configureCompilerOptionsForTarget(
extensionCompilerOptions: KotlinJvmCompilerOptions,
@Suppress("DEPRECATION") compilationsContainer: NamedDomainObjectContainer<out AbstractKotlinCompilation<KotlinJvmOptions>>
) {
extensionCompilerOptions.verbose.convention(logger.isDebugEnabled)
compilationsContainer.configureEach {
val jvmCompilerOptions = it.compilerOptions.options as KotlinJvmCompilerOptions
KotlinJvmCompilerOptionsHelper.syncOptionsAsConvention(
from = extensionCompilerOptions,
into = jvmCompilerOptions
)
jvmCompilerOptions.moduleName.convention(
extensionCompilerOptions.moduleName.orElse(
@Suppress("DEPRECATION")
project.providers.provider { it.moduleName }
)
)
}
}
}
override fun buildSourceSetProcessor(project: Project, compilation: KotlinCompilation<*>) =
@@ -65,19 +80,10 @@ internal open class KotlinJvmPlugin(
val kotlinExtension = project.kotlinExtension as KotlinJvmProjectExtension
kotlinExtension.target = target
kotlinExtension.compilerOptions.verbose.convention(project.logger.isDebugEnabled)
target.compilations.configureEach {
KotlinJvmCompilerOptionsHelper.syncOptionsAsConvention(
from = kotlinExtension.compilerOptions,
into = it.compilerOptions.options
)
it.compilerOptions.options.moduleName.convention(
kotlinExtension.compilerOptions.moduleName.orElse(
@Suppress("DEPRECATION")
project.providers.provider { it.moduleName }
)
)
}
project.configureCompilerOptionsForTarget(
kotlinExtension.compilerOptions,
target.compilations
)
super.apply(project)
@@ -59,14 +59,6 @@ internal class AndroidProjectHandler(
applyKotlinAndroidSourceSetLayout(kotlinAndroidTarget)
val androidExtensionCompilerOptions = project.objects.newInstance<KotlinJvmCompilerOptionsDefault>()
androidExtensionCompilerOptions.noJdk.value(true).finalizeValueOnRead()
@Suppress("DEPRECATION") val kotlinOptions = object : KotlinJvmOptions {
override val options: KotlinJvmCompilerOptions
get() = androidExtensionCompilerOptions
}
ext.addExtension(KOTLIN_OPTIONS_DSL_NAME, kotlinOptions)
val plugin = androidPluginIds
.asSequence()
.mapNotNull { project.plugins.findPlugin(it) as? BasePlugin }
@@ -84,11 +76,6 @@ internal class AndroidProjectHandler(
// handlers might break when fired on a compilation that is not yet properly configured (e.g. KT-29964):
compilationFactory.create(variantName).let { compilation ->
setUpDependencyResolution(variant, compilation)
project.wireExtensionOptionsToCompilation(
androidExtensionCompilerOptions,
compilation.compilerOptions.options as KotlinJvmCompilerOptions
)
preprocessVariant(variant, compilation, project, kotlinTasksProvider)
@Suppress("UNCHECKED_CAST")
@@ -115,38 +102,6 @@ internal class AndroidProjectHandler(
addAndroidUnitTestTasksAsDependenciesToAllTest(project)
}
private fun Project.wireExtensionOptionsToCompilation(
extensionCompilerOptions: KotlinJvmCompilerOptions,
compilationCompilerOptions: KotlinJvmCompilerOptions
) {
// CompilerCommonToolOptions
compilationCompilerOptions.allWarningsAsErrors.convention(extensionCompilerOptions.allWarningsAsErrors)
compilationCompilerOptions.suppressWarnings.convention(extensionCompilerOptions.suppressWarnings)
compilationCompilerOptions.verbose.convention(extensionCompilerOptions.verbose)
compilationCompilerOptions.freeCompilerArgs.addAll(extensionCompilerOptions.freeCompilerArgs)
// CompilerCommonOptions
compilationCompilerOptions.apiVersion.convention(extensionCompilerOptions.apiVersion)
compilationCompilerOptions.languageVersion.convention(extensionCompilerOptions.languageVersion)
compilationCompilerOptions.useK2.convention(extensionCompilerOptions.useK2)
// CompilerJvmOptions
compilationCompilerOptions.javaParameters.convention(extensionCompilerOptions.javaParameters)
compilationCompilerOptions.noJdk.value(extensionCompilerOptions.noJdk).finalizeValue()
// Special handling of jvmTarget to correctly override convention set by DefaultJavaToolchainSetter
// plus for 'moduleName' which could be overriden either by compilation or by task itself
// TODO: fix it once proper extension DSL will be available
afterEvaluate {
if (extensionCompilerOptions.jvmTarget.isPresent) {
compilationCompilerOptions.jvmTarget.set(extensionCompilerOptions.jvmTarget)
}
if (extensionCompilerOptions.moduleName.isPresent) {
compilationCompilerOptions.moduleName.set(extensionCompilerOptions.moduleName)
}
}
}
/**
* The Android variants have their configurations extendsFrom relation set up in a way that only some of the configurations of the
* variants propagate the dependencies from production variants to test ones. To make this dependency propagation work for the Kotlin
@@ -43,6 +43,7 @@ internal class KaptGenerateStubsConfig : BaseKotlinCompileConfig<KaptGenerateStu
task.libraries.from({ kotlinCompileTask.libraries - project.files(kaptClassesDir) })
task.compileKotlinArgumentsContributor.set(providers.provider { kotlinCompileTask.compilerArgumentsContributor })
task.pluginOptions.addAll(kotlinCompileTask.pluginOptions)
task.compilerOptions.moduleName.convention(kotlinCompileTask.compilerOptions.moduleName)
task.compilerOptions.freeCompilerArgs.convention(kotlinCompileTask.compilerOptions.freeCompilerArgs)
// KotlinCompile will also have as input output from KaptGenerateStubTask and KaptTask
// We are filtering them to avoid failed UP-TO-DATE checks