Slide 1

Slide 1 text

Ok Multiplatform! @jessewilson @egorand

Slide 2

Slide 2 text

What’s Okio?

Slide 3

Slide 3 text

• Complements java.io and java.nio • Better API • Better performance • Started as a part of OkHttp • Basis for many Square OSS libraries What’s Okio?

Slide 4

Slide 4 text

Okio OkHttp Moshi Wire Retrofit

Slide 5

Slide 5 text

Why Kotlin?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Kotlinizing Okio

Slide 10

Slide 10 text

Attempt #1: Koio

Slide 11

Slide 11 text

• Build a library like Okio but for Kotlin • Multiplatform by design • JDK6, JDK7, JDK8 and JS support FAILED Attempt #1: Koio

Slide 12

Slide 12 text

What went wrong?

Slide 13

Slide 13 text

• Too ambitious! • Missing primitives (UTF-8 encode/decode, synchronization, etc.) • Early days of multiplatform Kotlin

Slide 14

Slide 14 text

Attempt #2: okio-ktx

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

• New okio-kotlin module • Extension functions for frequently used factory methods NEVER SHIPPED Attempt #2: okio-ktx

Slide 17

Slide 17 text

What went wrong?

Slide 18

Slide 18 text

• No path to multiplatform • Redundant APIs for Kotlin callers • We didn’t get to use Kotlin features in Okio!

Slide 19

Slide 19 text

Attempt #3: Okio 2

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Goals

Slide 22

Slide 22 text

Multiplatform • Okio 2 will start as a JVM-only library • Okio 2 will be written in 100% Kotlin • Okio 2 will gradually get support for JS and Native

Slide 23

Slide 23 text

JVM options for I/O java.io straightforward blocking API good performance max ~2000 threads doing I/O java.nio clumsy non-blocking API (callbacks!) good performance no thread count ceiling

Slide 24

Slide 24 text

JVM options for I/O java.io straightforward blocking API good performance max ~2000 threads doing I/O java.nio clumsy non-blocking API (callbacks!) good performance no thread count ceiling kotlin.coroutines straightforward blocking API good performance no thread count ceiling

Slide 25

Slide 25 text

Idiomatic Kotlin API • We want good experience for both Kotlin and Java users • e.g. extension functions that can be used as static methods on Java

Slide 26

Slide 26 text

Okio 1.x val source = Okio.buffer(Okio.source(File("README.md"))) Okio 2.x val source = File("README.md").source().buffer()

Slide 27

Slide 27 text

Okio 1.x public byte getByte(int pos) val bytes = ByteString.decodeHex(”cafebabe”) val byte = bytes.getByte(0) Okio 2.x @JvmName(“getByte") operator fun get(index: Int): Byte val bytes = "cafebabe".decodeHex() val byte = bytes[0]

Slide 28

Slide 28 text

public int getSize() val size = str.getSize() Okio 1.x val bytes = ByteString.decodeHex(”cafebabe”) val size: Int val size = str.size Okio 2.x val bytes = "cafebabe".decodeHex()

Slide 29

Slide 29 text

Costs

Slide 30

Slide 30 text

Costs • Everyone who’s using Okio (or OkHttp, or Retrofit) now has a dependency on Kotlin standard library… • …which is 939 KiB… • …but it shrinks to 7 KiB with R8 or ProGuard

Slide 31

Slide 31 text

The Journey to Okio 2

Slide 32

Slide 32 text

Step #1: Gradle

Slide 33

Slide 33 text

Project Structure

Slide 34

Slide 34 text

dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}" classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:${versions.kotlinNative}" } Project level

Slide 35

Slide 35 text

apply plugin: 'org.jetbrains.kotlin.platform.common' apply plugin: 'org.jetbrains.kotlin.platform.jvm' apply plugin: 'org.jetbrains.kotlin.platform.js' apply plugin: 'org.jetbrains.kotlin.platform.native'

Slide 36

Slide 36 text

'kotlin': [ 'gradlePlugin': "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}", 'stdLib': [ 'common': "org.jetbrains.kotlin:kotlin-stdlib-common", 'jdk8': "org.jetbrains.kotlin:kotlin-stdlib-jdk8", 'jdk7': "org.jetbrains.kotlin:kotlin-stdlib-jdk7", 'jdk6': "org.jetbrains.kotlin:kotlin-stdlib", 'js': "org.jetbrains.kotlin:kotlin-stdlib-js", ], 'test': [ 'common': "org.jetbrains.kotlin:kotlin-test-common", 'annotations': "org.jetbrains.kotlin:kotlin-test-annotations-common", 'jdk': "org.jetbrains.kotlin:kotlin-test-junit", 'js': "org.jetbrains.kotlin:kotlin-test-js", ], 'native': [ 'gradlePlugin': "org.jetbrains.kotlin:kotlin-native-gradle-plugin:${versions.kotlinNative}", ] ], Dependencies

Slide 37

Slide 37 text

Publishing

Slide 38

Slide 38 text

Milestone #1 • We’ve got a fully working Kotlin multiplatform project! • Albeit with zero Kotlin files and JVM-only support

Slide 39

Slide 39 text

Step #2: ⌥⇧⌘K

Slide 40

Slide 40 text

• Convert Java files to Kotlin one-by-one • Rely on existing Java unit tests! Step #2: ⌥⇧⌘K

Slide 41

Slide 41 text

Milestone #2 • 100% Kotlin! • Still JVM-only

Slide 42

Slide 42 text

Step #3: Backfill

Slide 43

Slide 43 text

okio/jvm/src class PureKotlin { ... } Kotlin-only files okio/src class PureKotlin { ... }

Slide 44

Slide 44 text

Java → Kotlin override fun available() = Math.min(size, Integer.MAX_VALUE).toInt() override fun available() = minOf(size, Int.MAX_VALUE).toInt()

Slide 45

Slide 45 text

Platform-specific helpers okio/jvm/src System.arraycopy(src, srcPos, dest, destPos, length) okio/src expect fun arraycopy(src, srcPos, dest, destPos, length) okio/jvm/src actual fun arraycopy(src, srcPos, dest, destPos, length) { System.arraycopy(src, srcPos, dest, destPos, length) }

Slide 46

Slide 46 text

Java only /** Writes the contents of this byte string to `out`. */ @Throws(IOException::class) open fun write(out: OutputStream) { out.write(data) }

Slide 47

Slide 47 text

Step #4: JS/Native

Slide 48

Slide 48 text

• Create actual classes • Implement platform-specific helpers Step #4: JS/Native

Slide 49

Slide 49 text

Milestone #3 • 100% Kotlin! • JS/Native proof of concept

Slide 50

Slide 50 text

Compatibility

Slide 51

Slide 51 text

Binary NoSuchMethodError app.jar MyActivity.java library-v1.jar app.jar library-v2.jar javac java

Slide 52

Slide 52 text

Source

Slide 53

Slide 53 text

// Okio.java public Sink sink(File file) { return new OutputStreamSink(new FileOutputStream(file)); } Okio 1.x public static void main(String[] args) { Sink sink = Okio.sink(new File("README.md")); } fun main(args: Array) { val sink = Okio.sink(File("README.md")) } Kotlin Java

Slide 54

Slide 54 text

Okio 2.x public static void main(String[] args) { Sink sink = Okio.sink(new File("README.md")); } fun main(args: Array) { val sink = File(“README.md”).sink() } Kotlin Java // Okio.kt fun File.sink(): Sink = FileOutputStream(this).sink()

Slide 55

Slide 55 text

Okio 2.x public static void main(String[] args) { Sink sink = Okio.sink(new File("README.md")); } fun main(args: Array) { val sink = File(“README.md”).sink() } Kotlin Java // Okio.kt @file:JvmName(“Okio") fun File.sink(): Sink = FileOutputStream(this).sink()

Slide 56

Slide 56 text

Migrating

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Okio 2 • Binary-compatible with Okio 1.x • Source-compatible with Okio 1.x for Java users • Source-incompatible with Okio 1.x for Kotlin users • But better! Kotlin-friendly API

Slide 61

Slide 61 text

JApicmp

Slide 62

Slide 62 text

// @JvmOverloads open fun substring(beginIndex: Int = 0, endIndex: Int = size): ByteString ./gradlew :okio:jvm:japicmp > Task :okio:jvm:japicmp FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':okio:jvm:japicmp'. > A failure occurred while executing Comparing [jvm-2.0.0-SNAPSHOT.jar] with [okio-1.14.1.jar] > Detected binary changes between jvm-2.0.0-SNAPSHOT.jar and okio-1.14.1.jar. See failure report at file:// /okio/okio/jvm/build/reports/japi.txt

Slide 63

Slide 63 text

Quirks to watch out for • Internal classes are still public in bytecode • Classes and methods are final by default

Slide 64

Slide 64 text

Testing

Slide 65

Slide 65 text

• Keep your Java tests! • They’re calling your API from a Java user’s perspective • Add Kotlin tests on top • They’re testing Kotlin API • Try to promote your Kotlin tests to common Testing

Slide 66

Slide 66 text

ByteString.kt

Slide 67

Slide 67 text

expect class ByteString // Trusted internal constructor doesn't clone data. internal constructor(data: ByteArray) : Comparable { internal val data: ByteArray internal var hashCode: Int internal var utf8: String? /** Constructs a new `String` by decoding the bytes as `UTF-8`. */ fun utf8(): String /** * Returns this byte string encoded as [Base64](http://www.ietf.org/rfc/rfc2045.txt). In violation * of the RFC, the returned string does not wrap lines at 76 columns. */ fun base64(): String /** Returns this byte string encoded as [URL-safe Base64](http://www.ietf.org/rfc/rfc4648.txt). */ fun base64Url(): String /** Returns this byte string encoded in hexadecimal. */ fun hex(): String ... } okio/src/main/okio/ByteString.kt

Slide 68

Slide 68 text

actual open class ByteString internal actual constructor( internal actual val data: ByteArray ) : Serializable, Comparable { /** Writes the contents of this byte string to `out`. */ @Throws(IOException::class) open fun write(out: OutputStream) { out.write(data) } ... } okio/jvm/src/main/okio/ByteString.kt

Slide 69

Slide 69 text

Roadblocks

Slide 70

Slide 70 text

“Allow expect declarations with a default implementation” https://youtrack.jetbrains.com/issue/KT-20427

Slide 71

Slide 71 text

“Annotate relevant standard library annotations with @OptionalExpectation” https://youtrack.jetbrains.com/issue/KT-24478 (@OptionalExpectation has been introduced in 1.2.60)

Slide 72

Slide 72 text

“Class delegation should retain checked exceptions in JVM bytecode” https://youtrack.jetbrains.com/issue/KT-23935

Slide 73

Slide 73 text

“expect function with default parameters can't be annotated with JvmOverloads” https://youtrack.jetbrains.com/issue/KT-24357 FIXED

Slide 74

Slide 74 text

“Multiplatform: docs don't contain KDoc attached to expect members” https://github.com/Kotlin/dokka/issues/307

Slide 75

Slide 75 text

Benchmarks

Slide 76

Slide 76 text

1.x 2.x

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

1.x 2.x

Slide 79

Slide 79 text

1.x 2.x

Slide 80

Slide 80 text

Results

Slide 81

Slide 81 text

OKIO2 OKIO2 OKIO2 OKIO2 Released August 27, 2018

Slide 82

Slide 82 text

Placeholder for the blog post screenshot

Slide 83

Slide 83 text

Future Plans

Slide 84

Slide 84 text

• Continue backfilling interfaces into common • Coroutines! • Kotlin Native! Future Plans

Slide 85

Slide 85 text

• OkHttp - someday • Retrofit - someday • Moshi - someday soon • Wire - someday really soon

Slide 86

Slide 86 text

Recommendations

Slide 87

Slide 87 text

• Start with the JVM target first • Rely on unit tests • Use JApicmp • Backfill • Report Kotlin multiplatform issues! Recommendations

Slide 88

Slide 88 text

Thanks! @jessewilson @egorand