Android kotlin plugin 1.5.0 doesn'r recognize newest Duration API - android

In our Android app, I just upgraded the kotlin plugin to 1.5.0.
In top level build.gradle.kts:
buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
And in app build.gradle.kts:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.0")
Now, when building, I get a bunch of deprecation warnings about the Duration API used in such statements:
#ExperimentalTime
val myVal = 2.seconds
Warning says: 'seconds: Duration' is deprecated. Use Duration.seconds() function instead.
OK, so I proceed with the replacement like asked:
val myVal = Duration.seconds(2)
Now, the seconds() method appears in red in Android Studio with the error message: Expression 'seconds' cannot be invoked as a function. The function 'invoke()' is not found
It seems somehow that Android Studio is mixing Kotlin 1.5 with 1.4 I guess. Is there somewhere else than the two mentioned libraries on top that should be changed to point to kotlin 1.5?
I've tried invalidating the cache and remove all build folders without success.

According to Jetbrains on the Kotlin reddit, despite some 1.5.0 artifacts being available on maven central repo allready, Kotlin 1.5.0 is not yet officially released. Some bits and pieces are missing among others the IDE plugins, which explains the problem.
https://www.reddit.com/r/Kotlin/comments/n0gt0d/is_kotlin_150_released/

Related

Getting weird bugs when trying to update to Kotlin 1.4.0. How to make it work with Gradle and IntelliJ IDEA 2020.2.1?

Kotlin 1.4.0 is stable now. Therefor, I wanted to update my multi module Android project to use it. I set IDEA to use Kotlin plugin 1.4.0-release-IJ2020.2-1 and in my buildSrc build.gradle.kts using Kotlin DSL, I'm loading Kotlin for the jvm like this:
plugins {
kotlin("jvm") version "1.4.0"
}
My app level plugins block looks like this
plugins {
id("com.android.application")
id("com.google.gms.google-services")
kotlin("android")
kotlin("kapt")
id("kotlin-android-extensions")
id("androidx.navigation.safeargs.kotlin")
}
I have also added the Kotlin stdlib to my app level build.gradle.kts dependencies
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.0")
When trying to build my project now, I get multiple Errors like the following:
'let((T) -> R): R' is only available since Kotlin 1.3.50 and cannot be used in Kotlin 1.3
I don't understand. How is gradle trying to use Kotlin 1.3 here? Any idea on how to fix this? Its working fine when using Kotlin v1.3.72 instead.
What I tried so far:
Clean Project
Invalidate caches and restart
Delete .gradle folder and restart
Fix broken classes paths
UPDATE
Forgot to mention that I'm also getting the following warning. How is it unsupported when it is stable?
> Configure project :buildSrc
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.3.72` that might work differently than in the requested version `1.4.0`.
Maybe you should add the Kotlin standard lib to your dependencies explicitly?
dependencies {
implementation(kotlin("stdlib"))
}
With Kotlin 1.4 it is no longer required it add this dependency. The plugin applies the Stdlib by default to the projects it is applied to.
From the Kotlin 1.4 release notes:
You no longer need to declare a dependency on the stdlib library in any Kotlin Gradle project, including a multiplatform one. The dependency is added by default.
The automatically added standard library will be the same version of the Kotlin Gradle plugin, since they have the same versioning.

Gradle Kotlin DSL: Inspection "Newer Library Versions Available" not working

I have converted my Gradle build scripts to the Kotlin DSL. Since I was starting with a small new project, everything went according to plan. When referencing more and more dependencies I wanted to put their version numbers in the script as constants, especially for those versions which are used in several places.
In my app/build.gradle.kts I have basically the following:
dependencies {
implementation("androidx.appcompat:appcompat:1.0.0")
...
}
Android Studio inspections tell me, that I should upgrade to 1.1.0. I changed it to
val appCompat = "1.0.0"
dependencies {
implementation("androidx.appcompat:appcompat:$appCompat")
...
}
but now I do not get that inspection hint anymore.
I compared my Kotlin script to what I find in the Sunflower reference project and found it to be working there. So, I experimented with defining extra-values with
extra.apply {
set("appCompat", "1.0.0")
}
implementation("androidx.appcompat:appcompat:${extra["appCompat"]}")
but got no inspection hint either.
To me, it seems that the inspection is broken using the Kotlin DSL. Do you agree or do you have a working setup for this?
In my environment inspection is broken also. So I used from third-party plugin and run it task in some interval for checking available update and manage it.
More details: https://github.com/jmfayard/gradle-dependencies-plugins

Android Studio and Kotlin code inspection is unusable when there are multiple errors in file

In Kotlin files when I have multiple errors, Android Studio becomes unusable.
When I make any change everything freezes for a few seconds, when I disable code inspection then it works normally.
This does not happen with java
Is this an issue with my pc or Kotlin or android studio, and is there any kind of a solution
Did you update your kotlin version?
I passed over a similar problem because of updating some implements or kotlin,google-services version etc. in the middle of a project!
After i returned the previous version; it is fixed...
So try using previous version for example kotlin_version = '1.3.40' in the project's build.gradle.
buildscript {
ext.kotlin_version = '1.3.40'
...
...
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
...
It might not be an issue with Kotlin.
Ensure that both the Kotlin plugin and Android Studio plugins are up to date.
The IDE can hang if it does not have enough memory allocated.
To change the memory allocation, go to Edit -> Edit Custom VM option and update the memory allocation as shown in the image below. This might help.
If you need additional assistance finding this setting, please refer to the official Android Studio Configuration guide.

Android grade syntax and build tools compatibility issues

I didn’t work full-time with Android in the last couple of years, and now whenever I try to fork someone code on GitHub I get a lot of errors since android tools and Gradle syntax are changing frequently.
I wonder what is the best way to handle these changes, and be able to upgrade other GitHub projects and some of my old projects to work with the latest Android tools. Here are some of the things that I struggle with:
I noticed some of the issues are related to changes in the Gradle syntax. How can I know what Gradle version the build.grade syntax was written with? and then how to upgrade it to the current version (is there a migration guide for Gradle versions?).
Sometimes I get issues related to tools that are not compatible with others, how can I know which version are compatible with which? and whats the easy way to manage that? here are some of these tools:
Gradle
Android Plugin for Gradle
Build Tools
Android Studio
How can I know what Gradle version the build.grade syntax was written with?
If the project contains a gradle/wrapper/gradle-wrapper.properties file, that is the version of Gradle that the developer of the project is using.
is there a migration guide for Gradle versions?
I am not aware of much Gradle syntax that would have changed that would affect Android developers for garden-variety projects. Most of the changes are from the DSL added via the Gradle for Android plugin. You can find the version of the plugin that the project developer was using via the classpath statement in the project's top-level build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
The above snippet is requesting version 1.3.0 of the Gradle for Android plugin.
Migration documentation for the Gradle for Android plugin is minimal. What there is can be found up on http://tools.android.com.
how can I know which version are compatible with which?
Here is Google's statement on the issue, though this has not been updated in a few months.
and whats the easy way to manage that?
If the tools complain, change them to a set that you know is good (e.g., by copying values from a known-working project). If you need something that was introduced in a newer version of the tools, change them to a set that you know is good. Otherwise, don't worry about them.

Android Gradle Plugin 1.1.0 - getNdkFolder() not found anymore, any replacement?

I just updated android Gradle plugin to 1.1.0 from 1.0.0, and I got this error:
No signature of method:
com.android.build.gradle.LibraryPlugin.getNdkFolder() is applicable
for argument types: () values: []
I tried to find diff of gradle plugin 1.0.0 to 1.1.0 but couldn't find anywhere online.
Seems like getNdkFolder method has been either changed or removed.
Is there a replacement ? Or any other way to get NdkFolder ?
I'm using that method like this,
def ndkDir = project.plugins.findPlugin ( 'com.android.library'
).getNdkFolder ()
I have also filed an issue against tools team in Android issue tracker: https://code.google.com/p/android/issues/detail?id=152810
Thank you !
You can get it like this:
plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
The correct way - android.ndkDirectory
The change from android.plugin.ndkFolder to android.ndkDirectory was documented in the technical doc Migrating Gradle Projects to version 1.0.0. In addition you should update your android plugin to 2.3.0.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
The sdkFolder is internal and will likely stop working just like ndkFolder stopped working.
Regarding experimental branches
Search around, the plugin is mature enough you'll have trouble finding reasons to use experimental branches with the NDK.
There is some discussion about an Experimental version of the plugin breaking ndkDirectory which is the documented way to access this value after 1.1. If that changes in a released version I'll update my answer, in the mean time if running experimental plugins is your thing you can hack your way around this bug/feature using #Alex Cohn's answer.
I am using gradle 2.2.3 version. The think that worked for me:
def ndkDir = project.android.ndkDirectory
Ref. Link: https://code.google.com/p/android/issues/detail?id=152810
If in the build.gradle file you can probably use this now that the bug referenced has been marked fixed.
project.android.ndkDirectory
If you're in a custom Task or Plugin you'll want to use this
def androidPluginExtension = project.getExtensions().getByName("android");
// Use this for easily viewing the properties available in the extension.
androidPluginExtension.properties.each { Object key, Object value ->
logger.info("Extensionprop: ${key} ${value}")
}
String ndkDir = androidPluginExtension.getProperties().get("ndkDirectory");
System.out.println("Using ndk dir: ${ndkDir}");
But remember that the Android Plugin would have had to have been initialized, so this will always work in a Task's execute method, but it may not work if Gradle hasn't parsed the Android DSL yet during the Gradle Configuration phase.
As Michael Pardo mentioned, you can get it by
*
plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
*
, In case you receive the following error:
Error:(9, 0) Plugin with id com.android.library has not been used.
You can use the applied plugin : 'com.android.application', therefore the final code will be :
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.ndkFolder

Categories

Resources