kotlinOptions in kotlin multiplatform project - android

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>()

Related

Gradle Kotlin DSL and freeCompilerArgs

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")
}

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?

Android Studio Lambda expressions are not allowed at this language level

I am working on a chat application. I got a demo app from github, the chat app that is working. In some classes they used lambda expressions but it's working fine, but when I copy those code mine is giving this error " Lambda expressions are not allowed at this language level ". Some people said that android studio does not support lambda expressions but the demo app is working on my phone.
in build.gradle there should be
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
And you should probably use RetroLambda. Look at the demo project's build.gradle file
In their Android project they are using RetroLambda -
a plugin that allows lambdas in lower versions of Java,
which includes Android (see the plugin at the top --> https://github.com/NaikSoftware/StompProtocolAndroid/blob/master/example-client/build.gradle)
Follow the setup here: https://github.com/evant/gradle-retrolambda

Error:Data Binding does not support Jack builds yet

I am implementing DataBinding, it is working perfect, but it is not allowing me to use jackOptions. It throws error Data Binding does not support Jack builds yet while build.
Here is my build.gradle
android {
defaultConfig {
...
dataBinding {
enabled true
}
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
From yigit's comment it's clear that this functionality is still under development as he and George are project member of DataBinding.
we are working on it... yigit
I have also found that issue is already reported Issue 210615: Databinding with Jack compiler
UPDATE
2.3 will allow you to use data binding with jack but it still has limitations
Google will add support for Java 8 language features directly into the
current javac and dx set of tools, and deprecate the Jack toolchain.
Check this out , Future of Java 8 Language Feature Support on Android
https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html
Updates:
Java 8 language features are now supported by the Android build system
Yesterday, we released Android Studio 2.4 Preview 6. Java 8 language
features are now supported by the Android build system in the javac/dx
compilation path. Android Studio's Gradle plugin now desugars Java 8
class files to Java 7-compatible class files, so you can use lambdas,
method references and other features of Java 8.
Source : https://android-developers.googleblog.com/2017/04/java-8-language-features-support-update.html
As of gradle:2.2.0-alpha5, Jack builds are not yet compatible with DataBinding.
If you want to use data binding with Java 8, you can use retrolambda.

Categories

Resources