配置编译器参数 [英] Configure compiler arguments
问题描述
我正在寻找一种在Android应用程序项目的 build.gradle
文件中配置Kotlin编译器参数的方法.
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).
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
}
应用程序级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.
compileReleaseKotlin
块放错了位置或拼写了吗?不过,Android Studio建议我使用这种方法.
Is the compileReleaseKotlin
block misplaced, or mispelled ? Android Studio suggests me this method, though.
推荐答案
经过几天的搜索和试验,我终于找到了一种基于build变体配置编译器的方法.
After a few days of search and experimentation, I finally found a way to configure the compiler based on the build variant.
这对我有用:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// Configure Kotlin compiler optimisations for releases
kotlinOptions {
freeCompilerArgs += [
'-Xno-param-assertions',
'-Xno-call-assertions',
'-Xno-receiver-assertions'
]
}
}
}
}
Gradle Kotlin Android插件的文档似乎是不正确的:虽然它表示可以通过添加用于发布版本的 compileReleaseKotlin
闭包来配置编译器,但对于Android来说,如上所示,必须在 release
中放置一个 kotlinOptions
块.
It seems like the documentation for the Gradle Kotlin Android plugin is not correct: while it says that the compiler can be configured by adding, for instance, the compileReleaseKotlin
closure for release builds, for Android you have to put a kotlinOptions
block in release
, as shown above.
请注意,对于常规的Kotlin项目(没有Android),文档中所述的 compileKotlin
块按预期工作.
Note that for a regular Kotlin project (without Android), the compileKotlin
block described in the documentation works as intented.
希望有帮助!
编辑为参数添加了-
前缀,否则编译器会默默地忽略它们.
Edit Added -
prefix for arguments, as the compiler would silently ignore them otherwise.
编辑2 将 =
更改为 + =
,以避免覆盖其他配置的编译器选项.
Edit 2 Changed =
to +=
to avoid overwriting compiler options from other configurations.
这篇关于配置编译器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!