配置编译器参数
Posted
技术标签:
【中文标题】配置编译器参数【英文标题】:Configure compiler arguments 【发布时间】:2018-03-30 18:29:26 【问题描述】:我正在寻找一种在我的 android 应用程序项目的 build.gradle
文件中配置 Kotlin 编译器参数的方法。
我在Kotlin official documentation 上看到可以为每种构建风格(例如调试、发布)。
项目级 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
应用级 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
在构建项目时,我收到以下错误:
Could not find method compileReleaseKotlin() for arguments [build_7b4e2sfm3830f9z4br95gfme2$_run_closure2@7ed96f28] on project ':app' of type org.gradle.api.Project.
compileReleaseKotlin
块是放错地方还是拼写错误?不过,Android Studio 建议我使用这种方法。
【问题讨论】:
这并不一定意味着该方法根本没有定义。这也可能意味着这些论点有问题。此外,您可能过早调用此方法 - 在创建适当的任务之前。 我已向Google 和JetBrains 提出问题 【参考方案1】:编辑 2022
自从我发布此答案以来,Android 和 Kotlin 发生了很多变化。
Kotlin Gradle 脚本现在是配置 Gradle 构建的更安全的方式。
这是下面 sn-p 的等效 *.gradle.kts
,还有其他修复因为它一直都是错误的!
android
// Options are applied to all build types
kotlinOptions
freeCompilerArgs += listOf(
"-Xno-param-assertions",
"-Xno-call-assertions",
"-Xno-receiver-assertions",
)
@gmk57 指出,您无法从 buildTypes
块中配置特定于给定构建类型的特定编译器参数。 kotlinOptions
应直接在 android
块中定义,并且适用于所有构建类型。
在撰写本文时,拥有特定于构建类型的配置的唯一方法是在 afterEvaluate
中配置 compileReleaseKotlin
任务(以及风味变体,如果有的话):
afterEvaluate
tasks.compileReleaseKotlin
kotlinOptions
freeCompilerArgs += listOf(
"-Xno-param-assertions",
"-Xno-call-assertions",
"-Xno-receiver-assertions",
)
// For flavor dimension full + release
tasks.named<KotlinCompile>("compileFullReleaseKotlin")
kotlinOptions
// Same options as above, omitted for brevity
请注意,从 Android Gradle 插件 7.0.0 开始,插件创建时有有趣的new extensions points allowing to configure build variants。 DslExtension 可能允许 Kotlin Android 插件与构建类型无缝集成。
原答案
经过几天的搜索和实验,我终于找到了一种基于构建变体配置编译器的方法。
这对我有用:
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 你必须放一个 @ 987654333@ 块在release
,如上图。
请注意,对于常规 Kotlin 项目(没有 Android),文档中描述的 compileKotlin
块按预期工作。
希望对你有帮助!
编辑为参数添加了-
前缀,否则编译器会默默地忽略它们。
编辑 2 将 =
更改为 +=
以避免覆盖其他配置的编译器选项。
【讨论】:
请注意,这不能按预期工作:指定的选项应用于所有构建类型。【参考方案2】:对于在 2020 年寻找有关如何将编译器 arg 添加到 Kotlin Android 项目(我需要它来启用实验性序列化 API)的快速信息的任何人,我就是这样做的:
(app build.gradle 文件)
android
...
compileOptions
...
kotlin
kotlinOptions
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
Android Studio 不提供这些建议。
【讨论】:
【参考方案3】:所有构建类型的 Kotlin 编译器选项应在 android
闭包中指定。这是 Android Studio 4.1 默认放置 kotlinOptions jvmTarget = '1.8'
的位置:
android
...
kotlinOptions
jvmTarget = '1.8'
freeCompilerArgs += ['-Xno-param-assertions']
特定构建类型的编译器选项应该在根级别配置,用afterEvaluate
包裹,因为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']
【讨论】:
以上是关于配置编译器参数的主要内容,如果未能解决你的问题,请参考以下文章
使用自定义容器扩展/编译器传递中声明的容器参数配置 Symfony 3rd 方包