In my test sources I want to use some experimental Kotlin compiler feature to not see the lint warnings and errors for them in Android Studio. For example, I want to apply the following Kotlin options to all test sources.
compileTestKotlin {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.time.ExperimentalTime',
'-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi',
]
}
}
(source)
Out of the box this doesn't work in an Android project, because there is no compileTestKotlin method. According to the kotlin-android Gradle plugin docs, it should be possible to do this for compileVariantNameKotlin in an afterEvaluate block, e.g.:
afterEvaluate {
compileDebugUnitTestKotlin {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.time.ExperimentalTime',
'-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi',
]
}
}
}
(and similar for compileReleaseUnitTestKotlin)
Although this works (builds and test run and pass just fine), there seems to be no effect in Android Studio: usages of the experimental compiler features are still marked with warnings/errors.
How do I enable these compiler features for my test sources in such a way that AS recognizes and uses them?
This worked for me on Android Studio 4.2.2, AGP version 4.2.0, Kotlin version 1.5.10:
android {
testOptions {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.time.ExperimentalTime',
'-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi',
]
}
}
}
Related
Despite enabling data object support as per the Kotlin 1.7.20 document
kotlinOptions {
jvmTarget = '1.8'
languageVersion = '1.9' // data objects
}
the project does not build in release mode.
The feature "data objects" is only available since language version 1.9
What to do?
Just setting the language version is insufficient, you must also enable the language feature explicitly using the following code:
kotlin.sourceSets.all {
it.languageSettings.enableLanguageFeature("DataObjects")
}
Similarly to inline classes support.
Hi everyone I use appDynamics library and the documentation only explains with groovy Gradle, I have a problem with converting the groovy Gradle script to kotlin Gradle DSL and I have tried several ways and several syntaxes and I even used converting tools from groovy to Kotlin Gradle also didn't solve the problem following script with groovy Gradle
adeum {
account {
name 'xxx'
licenseKey 'yyyy'
}
proguardMappingFileUpload {
failBuildOnUploadFailure true //should build fail if upload fails? Defaults to false.
enabled true //enables automatic uploads. Defaults to true.
}
}
[Error][1]
[1]: https://i.stack.imgur.com/tet7q.png
and also i have to mention that the groovy is working fine
The problem is in some plugins in kts you have to use closure to determine the plugin packages
adeum {
account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
this.name ="xxx"
this.licenseKey ="yyy"
})
proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
this.failBuildOnUploadFailure = true
this.enabled = true
})
}
It's possible to change adeum to configure<ADPluginExtension>, like this:
configure<ADPluginExtension> {
account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
this.name ="xxx"
this.licenseKey ="yyy"
})
proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
this.failBuildOnUploadFailure = true
this.enabled = true
})
}
I had some issues, when used adeum plugin with kotlin-dsl, where gradle sync not generated extensions accessors to android, dependencies, implementation, etc... provided from kotlin-dsl plugin.
I have Android library module with enabled explicitApi kotlin feature in gradle
android {
kotlinOptions {
freeCompilerArgs += '-Xexplicit-api=warning'
}
}
Everything is fine, but the problem is that warnings are also reported for test classes in packages src/test and src/androidTest.
How to exclude test classes from explicit-api control?
Thanks
As far as I know, you can't! I was thinking of opening a bug report just this week but never got to it. In the meantime, I suggest you add something like this to your build script, which will at least fix it for the Kotlin compiler (but you'll still see the IDE warnings):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
if (!it.name.contains("Test")) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
If you're using Gradle Kotlin DSL:
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
if ("UnitTest" !in name) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
And don't set the compiler argument in android.kotlinOptions, only in that block, so that it is applied only to the non-test source sets.
EDIT: I just checked and the warnings bug was fixed in IntelliJ, so it should be fixed in Android Studio in a few months to a year.
I followed the Kotlin documentation here and here to opt into kotlin.ExperimentalUnsignedTypes for the whole module. My module build.gradle looks like this now:
android {
...
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += "-Xopt-in=kotlin.ExperimentalUnsignedTypes"
}
Lint is still complaining about ExperimentalUnsignedTypes and when compiling I get this warning:
> Task :myModule:compileDebugKotlin
w: Flag is not supported by this version of the compiler: -Xopt-in=kotlin.ExperimentalUnsignedTypes
My kotlin version is: 1.3.61
As mentioned in the docs here
#RequireOptIn and #OptIn annotations were introduced in 1.3.70 to replace previously used #Experimental and #UseExperimental; at the same time, -Xopt-in compiler option replaced -Xuse-experimental.
#OptIn(...::class) and use of -Xopt-in=... were introduced in Kotlin 1.3.70, but the old and gold -Xuse-experimental is still available in future versions (tested in Kotlin 1.4-M1).
I'm trying to use 3 of the dagger2 compiler options in my android project.
but it seems none of them actually work.
I have pasted the code from here to my gradle.properties and even compiler options of AS settings.
the 3 that I'm interested in are:
-Adagger.fastInit=enabled
-Adagger.formatGeneratedSource=disabled
-Adagger.gradle.incremental
the fastinit and codeformatting just don't work (judging by the code that is generated) but the incremental cause a compile error saying:
no compiler option found.
the versions that I'm using are:
dagger : 2.18
gradle : 5.2.1
kotlin : 1.3.21
androidPlugin : 3.3.1
For projects with multiple modules, the top build.gradle can be updated with this
allprojects {
repositories {
...
}
afterEvaluate {
extensions.findByName('kapt')?.arguments {
arg( "dagger.formatGeneratedSource", "disabled" )
}
}
}
Perhaps you should try without "A"
dagger.fastInit=enabled
dagger.formatGeneratedSource=disabled
dagger.gradle.incremental=enabled
Also can try directly in build.gradle, but this should be done for each project.
kapt {
arguments {
arg('dagger.fastInit', 'enabled')
arg('dagger.formatGeneratedSource', 'disabled')
arg('dagger.gradle.incremental', 'enabled')
}
}