![]() ![]() In the runtime module, the adds the extra srcDir to androidMain/ jvmMain and the src directory has the shared code in a commonAndroidJvm folder. It is built to work across multiple Kotlin platforms. ![]() Want a bigger example of this principle in action? Have a look at the awesome PbandK project, that provides a Kotlin code generator and runtime for Protocol Buffers. Kotlin īecause there is no new sourceSet, Intellij will implicitly use the Android or Jvm one while browsing code in commonAndroidJvm, and hence autocomplete will work! Real-life example make androidMain and jvmMain depend on this new SourceSetĪnd the resulting file will be:.create a new SourceSet called commonJvmAndroid.create a new folder commonJvmAndroid under src.Inspired by an example from Sergey Igushkin, let’s add a new sourceSet commonJvmAndroid that will have the shared actual implementations across Android and Jvm. Visually speaking we would like to accomplish the following: So the challenge at hand is to share most actual implementations across Android and Jvm, but provide platform-specific implementations for some functions. (Though API level 26 and higher also support 64) Base64) are different.Īs such Android has defined its own 64 class which is different from the Jvm 64 class. Take for instance Android and Jvm, they have a lot of common API’s but some (e.g. While this mechanism works great, it doesn’t allow to share code between specific SourceSets. In a typical Kotlin multiplatform project, there is a commonMain SourceSet (blue) which defines both common code and expected code for specific SourceSets (orange):īesides making use of the common code, the specific SourceSets also must provide actual implementations for the expected code. Read on to learn how to share part of your code between Android and Java (Jvm), while still providing platform-specific implementations of some classes. On the other hand, Kotlin multiplatform only allows sharing code across all targets (commonMain), not a subset of targets (commonJvm). While Android supports most Java language features, it doesn’t support every API that Java provides. Share code between Android and Jvm in Kotlin multiplatform
0 Comments
Leave a Reply. |