https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Sylvain Patenaude

11/27/2019, 7:52 PM
Is there a multiplatform way in Kotlin to figure out the endianness of bytes? I have to know to reverse the order if ncessary.
i

ian.shaun.thomas

11/27/2019, 7:54 PM
you mean endianness of the host system?
s

Sylvain Patenaude

11/27/2019, 7:54 PM
Yep, that's what I meant.
i

ian.shaun.thomas

11/27/2019, 7:56 PM
seems an approach like this would be sufficient https://stackoverflow.com/a/3877387/1286667
s

Sylvain Patenaude

11/27/2019, 8:01 PM
I guess it would work. Too bad it isn't built-in like in C#. Thank you Ian!
i

ian.shaun.thomas

11/27/2019, 8:01 PM
I feel like I remember seeing this talked about so it may exist
s

Sylvain Patenaude

11/27/2019, 8:16 PM
Mmm, maybe I could try it. How do I use ktor? I need to use a specific plugin in build.gradle I imagine?
i

ian.shaun.thomas

11/27/2019, 8:20 PM
no extra plugin it's just a dependency I think all you really need is the io library rather than full ktor https://github.com/Kotlin/kotlinx-io that has buffers and functions you'll probably want in regards to dealing with bytes
s

Sylvain Patenaude

11/27/2019, 8:23 PM
So I would only have to add something like this in my build.gradle?
implementation kotlin('kotlinx-io')
i

ian.shaun.thomas

11/27/2019, 8:23 PM
yeah you should be able to add that to your commonMain sourceset and then assuming you have enabled metadata it should pick up the platform specific dependencies automagically
s

Sylvain Patenaude

11/27/2019, 8:24 PM
You mean this in settings.gradle?
enableFeaturePreview('GRADLE_METADATA')
i

ian.shaun.thomas

11/27/2019, 8:25 PM
aye, that's the one
r

r4zzz4k

11/27/2019, 8:26 PM
Authors say kotlinx-io is quite unstable for general public use, so take it at your own risk. Maybe for the task of checking host endianness it's simpler to have own implementations for each platform? You can probably copy them from the library if you found it there already.
s

Sylvain Patenaude

11/27/2019, 8:29 PM
Mmm. I think I'll use the workaround suggested by @ian.shaun.thomas (stackoverflow link) and go from there. It seems simple enough to implement and I only need to have the info at runtime anyway. Thanks for the input Andrew.
r

r4zzz4k

11/27/2019, 8:30 PM
Oh, yeah, I've opened the link. Sure, that's the way to go :D
s

Sylvain Patenaude

11/27/2019, 9:09 PM
Well, the example is simple when written in C/C++. Not that simple when you're not very familiar with the bytes/bits manipulation functions in Kotlin, he he, 🙄
r

r4zzz4k

11/27/2019, 9:21 PM
Yeah, I forgot that reinterpretation from integer to byte array is platform-specific in Kotlin. Here are relevant parts of kotlinx-io, which you can copy: common simply has global variable: https://github.com/Kotlin/kotlinx-io/blob/master/core/commonMain/src/kotlinx/io/ByteOrder.kt jvm uses java.nio functionality: https://github.com/Kotlin/kotlinx-io/blob/master/core/jvmMain/src/kotlinx/io/JvmByteOrder.kt native does reinterpretation and checks manually: https://github.com/Kotlin/kotlinx-io/blob/master/core/nativeMain/src/kotlinx/io/NativeByteOrder.kt js uses it's own arrays which also allow to view memory block as different primitive arrays: https://github.com/Kotlin/kotlinx-io/blob/master/core/jsMain/src/kotlinx/io/JsByteOrder.kt
s

Sylvain Patenaude

11/27/2019, 9:46 PM
Well, in that case, I might try to use the library as is, even if it's experimental (so maybe not very stable). However, after adding
implementation kotlin('kotlinx-io')
to my gradle file, I got this error when rebuilding my root project:
Copy code
Could not resolve all files for configuration ':jvmCompileClasspath'.
 > Could not find org.jetbrains.kotlin:kotlin-kotlinx-io:1.3.60.
Here is my whole build.gradle file. The only line that I added is line 23.
r

r4zzz4k

11/28/2019, 3:34 PM
I'm actually not sure there are any publicly available artifacts for it.
s

Sylvain Patenaude

11/28/2019, 4:11 PM
I'm not sure I understand what you're saying. 🤔
r

r4zzz4k

11/28/2019, 4:12 PM
I mean, there is high chance that it's not even compiled and published to use from other projects in any public repository yet.
👍 1
d

Dico

12/01/2019, 7:41 AM
org.jetbrains.kotlinkotlin kotlinx io1.3.60 This is an incorrect reference. Please google the library and use the correct groupId & artifactId.
s

Sylvain Patenaude

12/03/2019, 3:12 PM
So if I understand well, simply adding
implementation kotlin('kotlinx-io')
won't cut it because it seems that under the hood it appends the same version to this library as the one for the 'kotlin multiplatform plugin' (which is 1.3.60 in my case)?
Well, I googled it and tried a few things with
org.jetbrains.kotlinx.kotlinx-io
and
version '0.1.16'
but it doesn't seem to work. Does someone know the correct groupId & artifactId for kotlinx-io? Also, I'm not sure if I have to 1. declare a
plugin
for this specific library 2. add a specific
classpath
for this library 3. use
implementation
for this library 4. use
compile
for this library or a subset of these options.
d

Dico

12/03/2019, 7:59 PM
The notation is
groupId:artifactId:version
. Once you use the notation correctly, take a look at the page where you found the 3 values to see on what maven repository it is hosted. Make sure the repository URL is known by your build by registering it under
repositories {}
You should use
api
instead of
implementation
if you're writing a library and consumers of its api should have the dependency too.
Although I personally still use the old
compile
instead which is the predecessor to
api
4 Views