'Configure compiler arguments
I'm looking for a way to configure Kotlin compiler arguments in the build.gradle file of my Android Application project.
I've seen on the Kotlin official documentation that it is possible to configure compiler arguments for each build flavor (such as debug, release).
Project-level build.gradle
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-rc1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
App-level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.myapp.myapplication"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
// The interesting part : configure the compileReleaseKotlin task
// to include compiler arguments when building releases
compileReleaseKotlin {
kotlinOptions {
freeCompilerArgs = [
'Xno-param-assertions',
'Xno-call-assertions',
'Xno-receiver-assertions'
]
}
}
dependencies {
// The usual Android dependencies, omitted for brievety
}
When building the project, I get the following error :
Could not find method compileReleaseKotlin() for arguments [build_7b4e2sfm3830f9z4br95gfme2$_run_closure2@7ed96f28] on project ':app' of type org.gradle.api.Project.
Is the compileReleaseKotlin block misplaced, or mispelled ? Android Studio suggests me this method, though.
Solution 1:[1]
For anyone looking for quick info in 2020 on how to add a compiler arg to a Kotlin Android project (I needed it to enable the experimental serialization API), this is how I did it:
(app build.gradle file)
android {
...
compileOptions {
...
kotlin {
kotlinOptions {
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
}
}
}
Android Studio doesn't provide those as suggestions.
Solution 2:[2]
Kotlin compiler options for all build types should be specified in the android closure. This is where Android Studio 4.1 places kotlinOptions { jvmTarget = '1.8' } by default:
android {
...
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs += ['-Xno-param-assertions']
}
}
Compiler options for specific build type should be configured at the root level, wrapped in afterEvaluate, because the task is only registered once the Android Gradle plugin creates specific build variant, which is only done at the afterEvaluate phase:
afterEvaluate {
compileReleaseKotlin {
kotlinOptions {
freeCompilerArgs += ['-Xno-param-assertions']
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ruslan |
| Solution 2 | gmk57 |
