Gradle Kotlin DSL and freeCompilerArgs - android

On one project I have used sucesfully argument -XXLanguage:-ProperCheckAnnotationsTargetInTypeUsePositions in kotlinOptions to disable "typeUsage" check on annotations. This project has regular build.gradle in Groovy. However when I have used exactly same approach on same version of Kotlin (1.6.0) in fresh project with Kotlin DSL gradle files (build.gradle.kts) it seems like this argument is ignored totally.
Summarizing:
project with good-old Groovy gradle -> argument works and disable error on "wrong usage of annotations"
project with new Kotlin DSL gradle -> argument seems to be ignored and still produces error
Maybe someone has same issue?
This is connected with Compatibility of android annotations with Kotlin 1.6.0

kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-XXLanguage:-ProperCheckAnnotationsTargetInTypeUsePositions")
}

Related

Android Native with Jetpack Compose Kotlin and Java Dependency Problems

All I wanted to do in my Android Native app using Jetpack Compose was to use some of the newer versions of some of the software. I guess I should have known better, because this has turned into the usual dependency nightmare. I have wasted all afternoon trying to get this to compile and run.
I’ll start with the error message, and work backwards:
> Task :app:compileDebugKotlin FAILED
> 'compileDebugJavaWithJavac' task (current target is 17) and 'compileDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
> e: This version (1.2.0-alpha05) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.7.10 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).
First, I have no idea why the compileDebugKotlin thinks I am targeting JVM 1.8 instead of 17. My JAVA_HOME is set to Java 17, my IntelliJ Project Structure JDK is set to 17, and my app:build.gradle has the following:
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
I do not see 1.8 being set anywhere.
The next issue is the Compose Compiler version that the error message is showing, 1.2.0-alpha05. I am setting the following in my root build.gradle file:
buildscript {
ext {
compose_version = '1.3.0'
…
And then referring to it in my app:build.gradle file:
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.compose.foundation:foundation:$compose_version"
I have no idea where it’s coming up with 1.2.0-alpha05 as the version.
The next issue is that it thinks I'm using Kotlin 1.7.10. I have set the Kotlin version to 1.6.10 in the IntelliJ preferences for Kotlin compiler, as well as 17 for the JVM. I tried the following in build.gradle, but it seems to be ignored, it is making no difference:
kotlin {
version("1.6.10")
}
In a nutshell, I have no idea where any of these version numbers are coming from, and how to set them so that they reflect the reality that I thought I was configuring correctly. I have cleared the IntelliJ cache and restarted numerous times.
the two task shown in the error have different functions during a build
compileDebugJavaWithJavac is responsible for generating the bytecode for the Java Code and making it available for further processing. which in your case is targeted to java 17 but compileDebugKotlin is responsible for making bytecode for kotlin code in your case it is targeted to 1.8 ,that is not supported so you should add
kotlinOptions {
jvmTarget = '17'
}
composeCompiler is not Compatible with all versions of Kotlin. Each compose compiler has corresponding compatible Kotlin Version (Compose to Kotlin Compatibility Map). what you shown is compose_version which do not represent Compose Compiler, they are declared in
composeOptions {
kotlinCompilerExtensionVersion = "1.x.x"
}

Incompatible Compose runtime version

I am trying to implement compose in multi module project and i am constantly getting following error:
androidx.compose.compiler.plugins.kotlin.IncompatibleComposeRuntimeVersionException: You are using an outdated version of Compose Runtime that is not compatible with the version of the Compose Compiler plugin you have installed. The compose compiler plugin you are using (version 1.0.3) expects a minimum runtime version of 1.0.0. at androidx.compose.compiler.plugins.kotlin.VersionChecker.outdatedRuntimeWithUnknownVersionNumber(VersionChecker.kt:119)
I only have on version specified (1.0.3) for all Compose dependencies, i also tried adding compose compiler to root build.gradle:
dependencies {
implementation "androidx.compose.compiler:compiler:1.0.3"
}
At the root file i also added configuration for compose:
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.3'
}
kotlinOptions {
jvmTarget = '1.8'
}
And then in module build.gradle i added compose dependencies (compose ui, material, ui toolign preview and uitooling), i also upgraded gradle to latest 7.0.2 and still same error poping, i checked gradle dependency tree no sign on 1.0.0 compiler anywhere, any idea what else i could try or what am i doing wrong?
Okay so the issue was i had to include activity-compose dependency on root gradle, and with that it works normally (if i try to include it per module it does not work)

Why kotlin.collections is not implicitly imported after upgrading to jetpack compose 1.0.0-beta01?

After upgrading to jetpack compose 1.0.0-beta01, I tried to use arrayListOf, listOf from kotlin.collections but they seemed to not implicitly imported.
Your problem is probably related to what Kotlin version you are using.
I guess I went through a similar process as
you did when I was updating to the new version of Jetpack Compose library, where
as a "side effect" I was forced to update kotlin and kotlin-gradle-plugin version what then
indirectly caused your (and mine) problem. Following workaround should fix it.
Most likely you are using Kotlin 1.4.30 after updating Jetpack Compose to 1.0.0-beta01. Update Kotlin to fixed 1.4.31 version and your problem will be ALMOST "resolved".
I think the whole problem is somehow related to the following bug in 1.4.30 if
you are more interested https://youtrack.jetbrains.com/issue/KT-44845
Now after trying to build your project you will get a nice error saying This version (1.0.0-alpha13) of the Compose Compiler requires Kotlin version 1.4.30 but you appear to be using Kotlin version 1.4.31 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).
suppressKotlinVersionCompatibilityCheck is a compile argument which in my case I have set in module build.gradle file under android -> kotlinOptions this way:
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
useIR = true
//here -->
freeCompilerArgs += ["-P", "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"]
}
It also depends on the type of build.gradle files.
Read more about how to set those compile arguments at
Configure compiler arguments
where are described different ways for groovy and also for kotlin based gradle files.
Now you should be fine but bear in mind that you want to get rid of the suppressKotlinVersionCompatibilityCheck argument as soon as there is a new version of Jetpack Compose relying on a newer version of Kotlin.

What is the difference and performance hit of jvmTarget = 1.8 and kotlin-stdlib-jdk8 in kotlin

We have multi module project and in most cases we can't use android ktx library extension functions (like viewModelScope) without changing jvmTarget to 1.8. I would like to create base library module with jvm target 1.8 which later we can apply for all modules. So its important to understand performance hit of changing jvmTarget to 1.8 from default (1.6) on android for module.
Already read question about jvm target, article about stdlibs, documentation
Could you please explain what is the difference between them? What happens if we change jvmTarget to 1.8 but continue using kotlin-stdlib-jdk7 ?
In documentation says that we need configure modules which uses only java8 features, but what is the performance hit if we configure java8 features for all modules?
Also in article about stdlibs says that from changing kotlin-library to kotlin-stdlib-jdk8 we won't benefit, but then why we need it?

kotlinOptions in kotlin multiplatform project

I am trying to use the Android dependency androidx.fragment:fragment-ktx:1.2.2 to be able to load ViewModels in fragments but I am getting an error when trying to use viewModels() saying
Cannot inline bytecode built with JVM target 1.8 into bytecode that is
being built with JVM target 1.6. Please specify proper '-jvm-target'
option
Searching that I found that in the android section of the build.gradle you need to put in the kotlinOptions
kotlinOptions {jvmTarget = '1.8'}
but when building I get an error
Could not find method kotlinOptions() for arguments
When I do this in a normal Android project it works fine because I assume its part of the kotlin-android plugin.
How do I use this in kotlin multiplatform?
Ended up I my imports were wrong, I needed import
import org.koin.androidx.viewmodel.ext.android.viewModel
then all I had to do was
val viewModel: MyViewModel by viewModel<MyViewModel>()

Categories

Resources