Switch to Kotlin 1.3.30 breaks hashCode only on Android API 21 - kotlin

I switched over my Android project from 1.3.20 to 1.3.30 and I am suddenly getting following exceptions from some of the classes when they call hashCode() method. Note, I am getting this exception only on Android platform API 21 & 22, beyond Android API 24, everything works fine.
Caused by: java.lang.NoSuchMethodError: No static method hashCode(J)I
in class Ljava/lang/Long; or its super classes (declaration of
'java.lang.Long' appears in /system/framework/core-libart.jar)
There is this thread that mentions similar symptoms but that was for Kotlin 1.2, I am wondering if anybody is encountering similar situation and if there is a workaround for this?

As an alternative, you may set a JVM target for Kotlin compilation to "1.6", as Kotlin 1.3.30 has started to infer the JVM target from the Java version in android.compileOptions and chooses "1.8" if both sourceCompatibility and targetCompatibility are set to that or higher.
In the module's build.gradle, add:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = '1.6'
}
}
There will be no need to do this once the issue in D8 desugaring is fixed.
This issue is tracked in the Kotlin issue tracker as KT-31027.

This is related to this issue - https://issuetracker.google.com/issues/129730297 so solution seems to be to upgrade to latest AGP!

Related

gradle nativeBinaries fails to satisfy dependencies

So, I wanted to generate native Kotlin binary for my app. I've came up with this build.gradle.kts:
plugins {
application
kotlin("multiplatform") version "1.3.70"
}
version = "1.0.2"
group = "org.gradle.sample"
repositories {
mavenCentral()
}
kotlin {
linuxX64("native") {
binaries {
executable()
}
}
}
dependencies {
implementation(kotlin("stdlib"))
implementation("io.javalin:javalin:3.8.0")
}
The code itself is simple:
package org.gradle.sample
import io.javalin.Javalin
fun main() {
val app = Javalin.create().start(7000)
app.get("/") { ctx -> ctx.result("Hello World") }
}
Problem here is that it fails when compiling, like if the dependencies were not satisfied:
> Task :compileKotlinNative FAILED
Caching disabled for task ':compileKotlinNative' because:
Build cache is disabled
Task ':compileKotlinNative' is not up-to-date because:
Task has failed previously.
file or directory '/home/keddad/Documents/samplekotlinapi/src/commonMain/kotlin', not found
file or directory '/home/keddad/Documents/samplekotlinapi/src/commonMain/kotlin', not found
Run tool: konanc with args: -g -ea -target linux_x64 -p library -o /home/keddad/Documents/samplekotlinapi/build/classes/kotlin/native/main/basic-api.klib -Xmulti-platform -no-endorsed-libs /home/keddad/Documents/samplekotlinapi/src/nativeMain/kotlin/org/gradle/sample/Main.kt
e: /home/keddad/Documents/samplekotlinapi/src/nativeMain/kotlin/org/gradle/sample/Main.kt: (3, 8): Unresolved reference: io
e: /home/keddad/Documents/samplekotlinapi/src/nativeMain/kotlin/org/gradle/sample/Main.kt: (6, 15): Unresolved reference: Javalin
e: /home/keddad/Documents/samplekotlinapi/src/nativeMain/kotlin/org/gradle/sample/Main.kt: (7, 20): Cannot infer a type for this parameter. Please specify it explicitly.
:compileKotlinNative (Thread[Execution worker for ':',5,main]) completed. Took 0.201 secs.
FAILURE: Build failed with an exception.
Same thing worked when compiling for JVM, but for Native it breaks. What am I doing wrong?
Unfortunately, you will not be able to compile this code at the moment. It seems like the Javalin framework is not targeting Kotlin/Native, it publishes only for Kotlin/JVM. The only libraries published with K/N in mind will be available to use.
In fact, the problem is that Kotlin flavors are not equivalent internally. They can share pure Kotlin code via common... source sets, but one cannot take a Kotlin/JVM project and just change the target. Kotlin/Native differs from the Kotlin/JVM, both of them are not the same as the Kotlin/JS. To make them work together, an approach named Kotlin/Multiplatform is recommended, see this article.
If you are interested in adapting this code to become multiplatform, consider looking at the Ktor. It provides support of the Kotlin/Native, and you'll be able to partially share code between platforms.

Kapt not processing micronaut-predator-processor

I am trying to use the new Predator JPA/JDBC library from Micronaut.
I wish to use the JDBC integration rather than JPA.
I am having a couple of issues however, the documentation says to include:
annotationProcessor 'io.micronaut.data:micronaut-predator-processor:1.0.0.BUILD-SNAPSHOT'
Which I have done so, but the kapt section of the build fails with the following error:
Caused by: java.lang.NoSuchMethodError: io.micronaut.core.annotation.AnnotationMetadata.enumValue(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/Class;)Ljava/util/Optional;
I am using micronaut 1.1.4 on JVM 12.0.1
If I replace kapt with annotationProcessor, the build completes fine, however I get a runtime issue:
Caused by: java.lang.ClassNotFoundException: io.micronaut.data.exceptions.DataAccessException
Which is odd as I seem to be able to use kapt/annotationProcessor interchangeable for micronaut security.
I faced the same problem when running tests from Intellij Idea. But they passed when run through gradle.
If you are using Intellij Idea and facing this issue, try following options in the sequence:
Invalidate Idea cache and restart
Enable Annotation Processing
Delegate IDE build/run actions to gradle. This setting had got reset to Idea when I upgraded my Intellij version.
I can get this to work for the micronaut 1.2.0 RC2 by updating the BOM entry:
dependencyManagement {
imports {
mavenBom 'io.micronaut:micronaut-bom:1.2.0.RC2'
}
}
This seems to allow all the annotation processing etc to work correctly.

Error building a request and initializing client for OkHttp3 on Kotlin

New to OkHttp, however I was able to track the following errors:
val url = "https://api.letsbuildthatapp.com/youtube/home_feed"
try {
val request = Request.Builder().url(url).build()
} catch(err: Error){
println("Request Error: $err")
}
This generates: "Request Error: java.lang.BootstrapMethodError: Exception from call site #5 bootstrap method"
try {
OkHttpClient()
} catch(err: Error){
println("Client Error: $err")
}
This returns an error "Client Error: java.lang.NoClassDefFoundError: okhttp3.internal.Util"
Any help with these will be greatly appreciated. TIA
It looks like you have an issue with the dependency and/or JVM. The java.lang.BootstrapMethodError may mean there is something work with the Java Runtime Environment. Please consider upgrading Java to the stable version (e.g. 11 or 1.8)
The NoClassDefFound error means you miss some classes from the OkHttp library in your project. There are likely to be several transitive dependencies from the .jar that you have in your project. Probably Gradle or Maven project will help to have all dependencies resolved correctly.

Cannot resolve symbol kotlinOptions (in Gradle build)

I'm trying to get the Kotlin compiler to generate Java 1.8 bytecode instead of 1.6 bytecode. Both the official docs and this StackOverflow question say you need to use code like this:
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
However, when I add this to my Gradle configuration, IntelliJ tells me that the symbol kotlinOptions cannot be resolved. Why is this? I'm using Kotlin v1.2.0, and it can compile code just fine, but it can't set this option.
As #Mikezx6r explained, the option is there otherwise it wouldn't compile. It's just that IntelliJ does not see this. Note that the 'Cannot resolve symbol' inspection does not generate any error, warning or even typo, it only greys out the option.

New Gradle version creates conflict with depedency

I have a simple build script (shorted for clarity) which uses Guava as a dependency
group 'test'
version '0.1.0'
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = 'Test'
repositories {mavenCentral()}
task wrapper(type: Wrapper) {gradleVersion = '3.5'}
dependencies {compile 'com.google.guava:guava:21.0'}
When running this code:
public class Test {
public static void main(String[] args) {
LoadingCache<Long, String> applicantCache = CacheBuilder.newBuilder()
.maximumSize(30000)
.expireAfterAccess(31, TimeUnit.DAYS)
.build(new CacheLoader<Long, String>() {
#Override
public String load(Long key) {
return "";
}
});
}
}
I get this error:
java.lang.NoClassDefFoundError: com/google/common/cache/CacheLoader
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.google.common.cache.CacheLoader
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"
When downgrading the Gradle Wrapper version to 3.3 the problem is fixed but, I need version 3.5 for something else that is not feasible in version 3.3. As far as I can understand there is some dependency conflict between this version of Gradle and Guava but according to this post - it is not possible.
I know the jar is in place + using gradle dependencyInsight --dependency com.google.guava shows that the dependency exists:
:dependencyInsight
com.google.guava:guava:21.0
\--- compile
Thanks for any help
EDIT:
After testing on other computers it seems the problem only occurs when I update the wrapper task, execute it and then refresh the gradle project by clicking on the Refresh all Gradle projects button (see attached image). When running the executing distribution script every thing works fine - so it only happens in Intellij. I'm guessing I'm missing something in how Gradle Wrapper works or when to execute it...
EDIT
Tested on Gradle version 3.4 and the problem occurres.
if anyone else encounters similar issues the answer is here.
A bug in my Intellij version (2016.2.5) caused compile scoped dependencies to transform into provided scoped dependencies by Intellij. This, in turn, makes dependencies to appear only in Intellij's compile classpath but not in it's runtime classpath. That's why when running the ./gradlew run command the issue could not be reproduced as there was no issue on Gradle's part.
Upgrading the Intellij version (2017.1) solved the issue.