What is bytecode transformation? - jvm

While reading a documentation of dagger-hilt, I came across such a term "bytecode transformation". What is bytecode transformation ?
the complete sentence
The Hilt Gradle plugin runs a bytecode transformation to make the APIs
easier to use.
What is the benefit of using bytecode transformation ?

Bytecode transformation happens after compilation and before dexing. Its input is bytecode and output is bytecode.
Android tools team has privately announced that the existing bytecode transformation API is going to be deprecated and replaced with new ones. Hopefully they will publish bytecode transformations soon.
Readmore:
My blogpost for one simpler usecase of bytecode transformation https://chao2zhang.medium.com/debugging-livedata-changes-made-easy-d3aa16b81b41
Dagger Hilt implementation https://github.com/google/dagger/blob/master/java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt

Related

How to use Kotlin in a library without forcing consumers to take Kotlin Stdlib as a dependency?

I am working on a Java-based library, and I'd like to start using Kotlin. However, there are over 10k known direct consumers, and I don't want to introduce a new dependency to all of the consumers.
For consumers that are already using Kotlin, I especially don't want to force them to upgrade or downgrade their Kotlin Stdlib version in order to use the latest version of the library, or introduce a classpath conflict of any sort.
Is it possible to use Kotlin in a way that doesn't "export" a new dependency to all of the library consumers?
My question is similar to
Using Kotlin libraries as dependencies in a Java project, but it (a) does not have an accepted answer, and (b) is trying to completely hide the existence of Kotlin types from Java consumers whereas I'm just trying to avoid causing any conflicting dependency requirements.
I'm currently using Maven, but wanting to switch to Gradle. A solution that works for Maven or Gradle is acceptable since using Kotlin is of greater importance than using a particular build system.
I ask the above question to avoid posing an XY Problem. My theoretical solution is to shade/relocate a specific version of the Kotlin Stdlib—but is it even possible, given that the Kotlin Stdlib seems to have some special treatment in the Kotlin compiler?
There is almost an exact match of this question on the Kotlinlang discussion board (What is the proper way to repackage/shade Kotlin dependencies), but it also does not have an answer.

Differences in writing Kotlin/JVM and Kotlin/JS?

I read the Big Nerd Ranch guide to Kotlin and it talked in several places about Kotlin/Java interop, but never JS or native. I already had a solid background in Java, so I have gotten used to using Java classes in my Kotlin code. I am trying to write a Kotlin program which will be run on a site where most - if not all - functionality is written in JavaScript, and I am trying to understand how to write my code to make sure that it is interoperable. Will I be able to continue using Java classes in my Kotlin/JS code? What are the differences between writing Kotlin/JVM code and Kotlin/JS code? What should a (ex-) java programmer know when learning to interop with JS using Kotlin? If there are a few chapters on this in any good books written in the recent past, that would be helpful also.
As Steve already mentioned, you can't utilise java classes in Kotlin/JS.
Think of Kotlin/JS as Typescript.
It provides a different syntax to write code that ultimately compiles to JS.
Here are the notable differences of writing Kotlin/JS code vs Kotlin/JVM code
Kotlin/JS internally uses yarn for dependency management. This enables you to depend on any javascript module available on npmjs etc (see note below)
In addition to standard library, you can also leverage other kotlin-first frameworks such as kotlinx-serialization, ktor etc
Testing libraries will be JS specific. So instead of mockito / mockk / junit family, you'll need to get familiar with karma / mocha family.
There will be a difference in Coroutine capabilities - both in terms of the way one writes code and performance expectations.
I found reading about Kotlin Multiplatform helped clarify a lot about the capabilities of kotlin.
I know this was not specifically asked, but giving my 2cents to people considering Kotlin/JS (as of Sep'20)
Great if you're familiar with Kotlin and don't foresee too many third party dependencies apart from http i/o (ktor) , React ( kotlin-react) and basic html / css (covered by kotlin-styled).
Using JS modules as dependencies is not as straight forward as using JVM dependencies since there is no ready-made interop. One has to define javascript functions/classes in kotlin before using them (see here). So if you foresee leveraging a lot of existing javascript modules, it won't be an ideal way forward.
Great if you have a typical backend-frontend model where backend compiles to JVM and Frontend compiles to JS. You can leverage a common data model and http i/o framework across Backend and Frontend code (via Kotlin Multiplatform). I've found this to be a tremendous productivity boost!
Kotlin/JS compiles Kotlin code, including its own standard library, into Javascript code. At the end, that's all you have is Javascript. What you don't have is any connection to the Java Virtual Machine. Kotlin's standard library provides no magic to bridge Javascript code to the JVM so that it can utilize Java classes. So NO, you can't utilize Java classes in standard Kotlin/JS.

How is Kotlin code compiled into native code?

The official Kotlin/Native documentation states that Kotlin/Native
.. is an LLVM based backend for the Kotlin compiler.
As far as I understand:
The Kotlin compiler (kotlinc) produces .class files (with Java bytecode) from your Kotlin source files.
Generic LLVM backends (that are not related to Kotlin) take LLVM IR and turn it into binary code.
Therefore, is Kotlin/Native converting Java bytecode into LLVM IR?
If so, is it correct to state that Kotlin/Native is a LLVM backend? Does Kotlin code get compiled into LLVM IR?
If not, what is the input and output of each compilation step? (e.g.: Kotlin -(kotlinc)-> Java bytecode -(LLVM backend-> native binary)
This blog post states that the Kotlin frontend compiler (I think it is referring to kotlinc) produces Kotlin IR, which I have never read of.
Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).
Is this quote correct?
It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).
Does the final platform specific binary include the native Kotlin standard library or is it linked dynamically?
Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).
Is this quote correct?
Yes, it's quite correct. The intermediate representation (IR) is a form that universally represents a Kotlin program, regardless of the platform. Then a platform-specific back-end translates the IR to the final binary form – the JVM class files for Kotlin/JVM, LLVM bitcode and other metadata packed into a *.klib for Kotlin/Native.
So, Kotlin/Native doesn't deal with the JVM bytecode in any way.
The intermediate representation is an implementation detail of the Kotlin compiler, and it's not used by any other consumer than the Kotlin compiler itself, so it's not a thing that a developer should care about. For the JVM, it's even kept in memory and never written to binaries. For Kotlin/Native, it's actually serialized and written into the *.klib to be reused.
It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).
Actually, a part of the compilation process is the same, namely, the front-end language analysis, which includes parsing, call resolution, type inference, diagnostics and other steps that don't require any code transformations that are platform-specific. Even so, the front-end part is tuned for each of the platforms, as those allow different sets of language features and provide different sets of diagnostics.
And the last question does the final platform specific binary include the native Kotlin standard library? Or is it linked dynamically?
Currently, the standard library is statically linked into the resulting Kotlin/Native binaries.

What's the difference between Kotlin JVM and Kotlin Native?

So I know Kotlin Native is obviously Native and Kotlin JVM isn't but is the code between Kotlin JVM and Kotlin Native:
1. Different Compiler and Different Code
2. Different Compiler and Similar Code
3. Different Compiler and Same Code
4. None of the above (please explain)
The Kotlin/JVM and Kotlin/Native compilers share the front-end (the part that performs code parsing, name resolution, type inference etc.), but the compiler back-ends that translate the internal program representation to the target code (the JVM bytecode and LLVM bitcode, respectively) are different.
The Kotlin language accepted by the two compilers is the same, but some of the features and checks are platform-specific. Also, the standard libraries for Kotlin/JVM and Kotlin/Native are sufficiently different, see the APIs available on each platform here: Kotlin Standard Library.
Another big difference is the memory model: Kotlin/JVM uses the Java memory model, while Kotlin/Native offers its own concurrency and memory model.
Also, the dependencies that one can use in Kotlin/JVM and Kotlin/Native projects are different. In addition to the projects built using the same Kotlin target:
Kotlin/JVM can also use any libraries built for the JVM (written in Java, Scala etc.)
Kotlin/Native can also interoperate with native libraries written in C (or at least having C headers) using the C interop tools.
Both Kotlin/JVM and Kotlin/Native can use Kotlin Multiplatform libraries. Given that a dependency is a multiplatform library, one can entirely reuse the code working with it between Kotlin/JVM and Kotlin/Native.

Can javascript generated from Kotlin sources be used back in JVM?

Is it possible to use js generated from Kotlin sources in JVM to manipulate java objects and its own js objects?
Kinda create dynamic environment for development in distributed environment.
So new versions of classes can be dynamically loaded/modified on running remote servers while development. And then after it's done release versions to be compiled statically for max performance.
How hard would it be to implement?
The current implementation of Kotlin does not have any support for invoking Java methods from JS-generated code. This could in theory be accomplished using Nashorn, but we (the Kotlin team) do not currently have any plans to work on that. If you're interested, you're welcome to contribute, but this would be a fairly long and difficult project.