In many android projects, I observed this kotin stdlib dependency being added in the Gradle files. I also observed kotlin and kotlin-android plugin also adds it by default.
I am assuming adding it manually can be avoided? Or is there a downside to it?
org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}
Also, I believe
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
compileOptions can be safely dropped from 100% kotlin application.
Using kotlinOptions.jvmTarget="11" is there a downside to it, in terms of byte-code generation and byte-code compatibility with other jdk versions.
I am assuming adding it manually can be avoided? Or is there a
downside to it?
https://kotlinlang.org/docs/gradle.html#dependency-on-the-standard-library
There's no downside to it, if you want to add it manually and donot want it to be downloaded by the plugin - set this plugin kotlin.stdlib.default.dependency=false property false in gradle.properties file.
Using kotlinOptions.jvmTarget="11" is there a downside to it, in terms
of byte-code generation and byte-code compatibility with other jdk
versions.
It require AGP 7.0+ to desugar apps for API levels 29 and earlier, while targeting jvmVersion to 8 only requires AGP 3.0+.
Otherwise, byte-code is same.
compileOptions can be safely dropped from 100% kotlin application.
Android projects are not 100% Kotlin, there are generated files like BuildConfig.java.
compileOptions should be there, it can be avoided in 100% Kotlin module.
Related
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?
I'm having some great code which I would like to share with others. I thought it would be a good idea to wrap the sharable code into an Android Library.
I'm using some foreign code, which requires me to specify this in my build.gradle for the library:
compileOptions {
sourceCompatibility '1.8'
targetCompatibility = '1.8'
}
I had a lot of problems to make some functions run, which ran successfully while they were embedded in my old MainActivity. Now, when they were in the lib it always crashed for unrelated and unclear reasons.
Until - and I admit, I don't really understand, what difference this makes - I specified exactly the same compileOptions regarding source and targetCompatibility in my app's build.gradle.
From that moment on all problems where gone.
Does that make any sense to somebody?
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
After adding a third party library I got the PARSE ERROR: unsupported class file version 52.0 after including the dependency. I have found similar questions like this one. The solution in the link provide suggest to include in the gradle file the following:
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
The problem is that the dependency is using 1.8. So, I have tried to use 1.8 for the entire project by adding Jack as it is suggested here. However, I am also using Realm that can't be used with java 8 as it is explained here.
Is there any way to use 1.8 only for the dependency I want to include and use 1.7 in the rest of the project?
I am quite new and I don't know if this is possible. I have checked around, but I haven't found anything.
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.