Thi*_*sel 2 android gradle kotlin
我正在寻找一种在build.gradle我的Android应用程序项目的文件中配置Kotlin编译器参数的方法.
我在Kotlin官方文档中看到,可以为每个构建版本配置编译器参数(例如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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
在构建项目时,我收到以下错误:
Could not find method compileReleaseKotlin() for arguments [build_7b4e2sfm3830f9z4br95gfme2$_run_closure2@7ed96f28] on project ':app' of type org.gradle.api.Project.
Run Code Online (Sandbox Code Playgroud)
是compileReleaseKotlin块错位,或拼写错误?不过,Android Studio会向我推荐这种方法.
Rus*_*lan 13
对于任何在 2020 年寻找有关如何向 Kotlin Android 项目添加编译器参数的快速信息的人(我需要它来启用实验性序列化 API),我是这样做的:
(应用程序 build.gradle 文件)
android {
...
compileOptions {
...
kotlin {
kotlinOptions {
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Android Studio 不提供这些建议。
经过几天的搜索和实验,我终于找到了一种基于构建变体配置编译器的方法.
这对我有用:
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'
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎Gradle Kotlin Android插件的文档不正确:虽然它说可以通过添加,例如,compileReleaseKotlin发布版本的闭包来配置编译器,但对于Android,你必须放入一个kotlinOptions块release,如上所示.
请注意,对于常规Kotlin项目(没有Android),compileKotlin文档中描述的块工作有意.
希望能帮助到你 !
编辑-参数的添加前缀,否则编译器会默默忽略它们.
所有构建类型的 Kotlin 编译器选项都应在闭包中指定android。kotlinOptions { jvmTarget = '1.8' }这是 Android Studio 4.1默认放置的位置:
android {
...
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs += ['-Xno-param-assertions']
}
}
Run Code Online (Sandbox Code Playgroud)
特定构建类型的编译器选项应在根级别配置,包装在 中afterEvaluate,因为只有在 Android Gradle 插件创建特定构建变体时才会注册任务,这仅在阶段afterEvaluate完成:
afterEvaluate {
compileReleaseKotlin {
kotlinOptions {
freeCompilerArgs += ['-Xno-param-assertions']
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |